Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the right version of the right JAR in a maven repository

Tags:

maven-2

I'm converting a build that has 71 .jar files in its global lib/ directory to use Maven. Of course, these have been pulled from the web by lots of developers over the past ten years of this project's history, and weren't always added to VCS with all the necessary version info, etc.

Is there an easy, automated way to go from that set of .jar files to the corresponding <dependency/> elements for use in my pom.xml files? I'm hoping for a web page where I can submit the checksum of a jar file and get back an XML snippet. The google hits for 'maven repository search' are basically just finding name-based searches. And http://repo1.maven.org/ has no search whatsoever, as far as I can see.

Update: GrepCode looks like it can find projects given an MD5 checksum. But it doesn't provide the particular details (groupId, artifactId) that Maven needs.

Here's the script I came up with based on the accepted answer:

#!/bin/bash

for f in *.jar; do
    s=`md5sum $f | cut -d ' ' -f 1`;
    p=`wget -q -O - "http://www.jarvana.com/jarvana/search?search_type=content&content=${s}&filterContent=digest" | grep inspect-pom | cut -d \" -f 4`;
    pj="http://www.jarvana.com${p}";
    rm -f tmp;
    wget -q -O tmp "$pj";

    g=`grep groupId tmp | head -n 1 | cut -d \> -f 3 | cut -d \< -f 1`;
    a=`grep artifactId tmp | head -n 1 | cut -d \> -f 3 | cut -d \< -f 1`;
    v=`grep version tmp | head -n 1 | cut -d \> -f 3 | cut -d \< -f 1`;
    rm -f tmp;

    echo '<dependency> <!--' $f $s $pj '-->';
    echo "  <groupId>$g</groupId>";
    echo "  <artifactId>$a</artifactId>";
    echo "  <version>$v</version>";
    echo "</dependency>";
    echo;
done
like image 357
Matt McHenry Avatar asked Jun 17 '10 15:06

Matt McHenry


People also ask

Where is Maven repository jar?

repositories in your local maven repository (typically ~/. m2/repositories ) for each artifact. The file is located right beside the corresponding artifacts in the local maven repository. This file will typically list the repository / plugin-repository that the artifact was downloaded from.

What is jar Maven repository?

A Maven Repository is a location, generally on a filesystem (either remote or local), where maven artifacts are stored and managed. Once artifacts have been stored in a maven repository, they are available for retrieval and inclusion in other maven projects.

What is version and updates in Maven repository?

It tells you whether there's an updated version of a particular dependency available and if so what the latest version is. If there's a check mark it means the library in question already uses the latest version of dependency X.

What will happen if the Maven version number of POM xml file does not match with the machine used to install?

Maven won't allow any other either. Build will fail if version is not found.


1 Answers

I was in the same situation as OP, but as mentioned in later answers Jarvana is no longer up.

I used the search by checksum functionality of Maven Central Search and their search api to achieve the same results.

First create a file with the sha1sums

sha1sum *.jar > jar-sha1sums.txt

then use the following python script to check if there is any information on the jars in question

import json
import urllib2

f = open('./jar-sha1sums.txt','r')
pom = open('./pom.xml','w')
for line in f.readlines():
    sha = line.split("  ")[0]
    jar = line.split("  ")[1]
    print("Looking up "+jar)
    searchurl = 'http://search.maven.org/solrsearch/select?q=1:%22'+sha+'%22&rows=20&wt=json'
    page = urllib2.urlopen(searchurl)
    data = json.loads("".join(page.readlines()))
    if data["response"] and data["response"]["numFound"] == 1:
        print("Found info for "+jar)
        jarinfo = data["response"]["docs"][0]
        pom.write('<dependency>\n')
        pom.write('\t<groupId>'+jarinfo["g"]+'</groupId>\n')
        pom.write('\t<artifactId>'+jarinfo["a"]+'</artifactId>\n')
        pom.write('\t<version>'+jarinfo["v"]+'</version>\n')
        pom.write('</dependency>\n')
    else:
        print "No info found for "+jar
        pom.write('<!-- TODO Find information on this jar file--->\n')
        pom.write('<dependency>\n')
        pom.write('\t<groupId></groupId>\n')
        pom.write('\t<artifactId>'+jar.replace(".jar\n","")+'</artifactId>\n')
        pom.write('\t<version></version>\n')
        pom.write('</dependency>\n')
pom.close()
f.close()

YMMV

like image 97
Karl Tryggvason Avatar answered Oct 23 '22 17:10

Karl Tryggvason