1

Let say I have a folder including all jar files needed in a maven project.

I would like to fill/write the dependencies in pom.xml section automatically from the jar files in the folder. Is there an existing automated way to do it ?

If there is a log4j-core-2.11.1.jar file in the folder, i would like to get :

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>

Thank you

3 Answers3

2

@Jens : Thank you, your code is definitely helpful (cannot vote you up; low rep)

As I wanted a quick (at least for me) way, i ended up with a few python lines : Here they are (in case they could help)

import sys
import json
from urllib.request import urlopen
import hashlib
from string import Template
from collections import namedtuple
from os import listdir

path = 'your path to jar folder'
files = listdir(path)


def hashfile(filepath):
    f = open(filepath, 'rb')
    readFile = f.read()
    sha1Hash = hashlib.sha1(readFile)
    sha1Hashed = sha1Hash.hexdigest()
    return sha1Hashed

def request( hash ):
    url = 'https://search.maven.org/solrsearch/select?q=1:' + \
        hash+'&wt=json&rows=1'
    response = urlopen(url).read()
    return json.loads(response.decode('utf-8'));

dep = '''
<dependency>
    <groupId> $g </groupId>
    <artifactId> $a </artifactId>
    <version> $v </version>
</dependency>
'''

deps= '''
<dependencies>
    $d
</dependencies>
'''

deb_tpl = Template(dep)
debs_tpl = Template(deps)
Jar = namedtuple('Jar',[ 'g', 'a', 'v'])

dependencies = [None]*len(files)
for i, filename in enumerate(files):
    sha1=hashfile( "%s/%s" %(path, filename))
    print("File : %i : sha1 : %s" % (i, sha1))
    obj = request( str(sha1 ))
    if obj['response']['numFound'] == 1:
        jar = Jar(obj['response']['docs'][0]['g'],
                 obj['response']['docs'][0]['a'],
                 obj['response']['docs'][0]['v'])
        dependencies[i] = jar

#         print(obj['response']['docs'][0]['a'])
#         print(obj['response']['docs'][0]['g'])
#         print(obj['response']['docs'][0]['v'])

    else :
        print('Cannot find %s' % filename)
        dependencies[i] = None
deps_all = '\r\n'.join([ deb_tpl.substitute(f._asdict())for f in dependencies if f is not None ])
debs_tpl.substitute(d=deps_all)
print(res)

Final res gives me all the dependencies found on search.maven. For missing jars, you might use this answer

1

Assuming the jar files are result of a maven build you can start with this code:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class CheckMe {

  public static void main(String args[]) throws IOException {

    String fileZip =
        "yourjar.jar";
    ZipInputStream zis = new ZipInputStream(new FileInputStream(fileZip));
    ZipEntry zipEntry = zis.getNextEntry();
    while (zipEntry != null) {
      if (zipEntry.getName().endsWith("pom.xml")) {
        final StringBuilder pom = new StringBuilder();
        final byte[] buffer = new byte[1024];

        while (zis.read(buffer, 0, buffer.length) != -1) {
          pom.append(new String(buffer));
        }

        System.out.println("<dependency>");
        Scanner scanner = new Scanner(pom.toString());
        boolean groupDone = false, artifactDone = false, versionDone = false;
        while (scanner.hasNextLine()) {
          String line = scanner.nextLine();
          if (line.contains("groupId") && !groupDone) {
            System.out.println(line);
            groupDone = true;
          }
          if (line.contains("artifactId") && !artifactDone) {
            System.out.println(line);
            artifactDone = true;
          }
          if (line.contains("version") && !versionDone) {
            System.out.println(line);
            versionDone = true;
          }
        }
        scanner.close();
        System.out.println("</dependency>");
      }
      zipEntry = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
  }
}

It is a quick hack and you have to add your directory scanner to get alle the jar file names, but it should do the trick.

Jens Dibbern
  • 1,434
  • 2
  • 13
  • 20
1

I ran the python script and it's not fully fleshed out wrt nested folders etc. I found a good alternative script at How to know groupid and artifactid of any external jars in maven android project

Zubin Kavarana
  • 190
  • 2
  • 11