1

There is a java project which has too many jar files and it has become difficult to manage all of them. I know there is a way to convert this project to a maven project but I don't want to manually add the dependency for each jar in my project to the pom file.

Is there any way to create the pom file using the local jar files such that the pom will contain the dependency tag for each jar.

Edit : I want to clarify that I do not want to add the jars from/to the local repository. The local repository approach will not work for me as the project will be used by multiple users across different systems.

I wanted to create regular pom dependency entries along with groupId, artifactId and version for the jar files which I already have so that I can copy-paste it into the pom file when I convert my project to a Maven project. As I have a large number of JARs in my project, I would have to do it all manually.

Solution provided by @Milen Dyankov worked for me. I was able to get the information I needed from most of the JAR files.

Jatin Vasnani
  • 43
  • 1
  • 5
  • 3
    Possible duplicate of [How to add local jar files to a Maven project?](https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project) – GameDroids Sep 24 '19 at 06:48
  • I think the answer you are looking for is [here](https://stackoverflow.com/q/4955635/896249) (duplicate). – GameDroids Sep 24 '19 at 06:49
  • @GameDroids No, op doesn't want to enumerate the jars in the pom; much less add the jars manually to a local `.m2`. There are tools like sbt and gradle that make writing large numbers of dependencies easier. – Elliott Frisch Sep 24 '19 at 06:49
  • 2
    If I understand correctly, you want to import all your local jar files into your pom automatically? If this is what you want, I don't think it's possible – AKMMM Sep 24 '19 at 06:50
  • 1
    If @AKMMM is right, I wouldn't try even if it was possible... – deHaar Sep 24 '19 at 06:51
  • @AKMMM That was my interpretation as well, and it might be possible to hack something together to do it; but it would be so *fragile* I don't want to attempt it either. Assuming all of the dependent jars contain pom files then you could write a script to extract the tags (but again, it would be *brittle*). – Elliott Frisch Sep 24 '19 at 06:52
  • But you can also let your IDE do the job. Keep a list of your dependencies (jar) libraries, their names and versions (if you know them) then in your IDE you can most likely go to some "maven" menu and "add dependency" somewhere which should provide you with a search option to search for the name and version of your dependency. I think it is worth while to take the time and search for the dependency in maven - in some cases you might even be able to get newer versions or resolve dependency problems. – GameDroids Sep 24 '19 at 06:53
  • @GameDroids I do not want to add the jars from/to the local repository. I wanted to create regular pom entries along with groupId, artifactId and version for the jar which I already have. The local repository approach will not work for me. – Jatin Vasnani Sep 25 '19 at 06:39

4 Answers4

3

This code

    public static void main(String[] args) throws IOException {
        Set<String> missingMavenData = new HashSet<String>();
        String FOLDER = "/path/to/your/folder/with/jars";
        
        Files
         .walk(Paths.get(FOLDER), FileVisitOption.FOLLOW_LINKS)
         .map(Path::toFile)
         .filter(f -> f.isFile())
         .filter(f -> f.getName().endsWith(".jar"))
         .map(f -> {
            try {
                return new JarFile(f);
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
         })
         .filter(Objects::nonNull)
         .map(jar -> {
             Properties properties = null;
             Enumeration<JarEntry> entries = jar.entries();
             while (entries.hasMoreElements()) {
                 JarEntry jarEntry = entries.nextElement();
                 if (jarEntry.getName().matches("^META-INF/maven/.*/pom\\.properties$")) {
                     try {
                         properties = new Properties();
                         properties.load(jar.getInputStream(jarEntry));
                         break;
                     } catch (IOException e) {
                         e.printStackTrace();
                     }
                 };
             } 
             if (properties == null) {
                 missingMavenData.add(jar.getName());
             }
             return properties;
         })
         .filter(Objects::nonNull)
         .forEach(properties -> {
            System.out.println("< dependency>");
            System.out.println("    <groupId>" + properties.getProperty("groupId")+ "</groupId>");
            System.out.println("    <artifactId>" + properties.getProperty("artifactId")+ "</artifactId>");
            System.out.println("    <version>" + properties.getProperty("version")+ "</version>");
            System.out.println("</dependency>");
         });
        
        System.out.println("Those JAR files do not contain Maven metadata:");
        missingMavenData.forEach(System.out::println);
    }

will iterate over your jar files and try to find the Maven metadata in them. It will generate the POM entries for those who have it and will list those that don't have it so you can add it manually. I hope this helps.

UPDATE:

I added bom-helper:fromJars goal to BOM Helper Maven Plugin which does more or less the same thing as the above code. One can now simply add the plugin

<plugin>
    <groupId>com.commsen.maven</groupId>
    <artifactId>bom-helper-maven-plugin</artifactId>
    <version>0.2.0</version>
</plugin>

and configure it to call fromJars goal. It can also be called form command line. For example:

mvn bom-helper:fromJars \
 -Dbom-helper.jarsFolder=/path/to/folder/with/jars \
 -Dbom-helper.inplace=true \
 -Dbom-helper.recursive

will update current pom with entries for all jars that have Maven metadata in them.

Milen Dyankov
  • 2,972
  • 14
  • 25
2

You can Install manually the JAR into your local Maven repository

First Way

Follow the steps here

mvn install:install-file -Dfile=<path-to-file>

And in here you can be able to add these information in command line

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

<path-to-file> mentioned path to the JAR to install

<group-id> mentioned group id of the JAR to install

<artifact-id> mentioned artifact id of the JAR to install

<version> mentioned version of the JAR

Second Way

You can create a different local Maven repository

Follow the Steps

we can consider the new local Maven repository is named maven-repository and is located in ${basedir} (the directory containing pom.xml).

Deploying the local JARs in the new local maven repository as below i am following

mvn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./maven-repository/ -DrepositoryId=maven-repository -DupdateReleaseInfo=true

you can see which the maven deploy:deploy-file installs the artifact in a remote repository but in here repository is located in the local machine

After installing the JARs your need to add the repository in your pom.xml file

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>
Dulaj Kulathunga
  • 1,248
  • 2
  • 9
  • 19
0

AFAIK there is not Maven way to do this.

You could write a script that computes the SHA1 value of each jar and searches for the jar in your Maven repository. It could, alternatively, open the jars and search for the pom.properties to extract the Maven coordinates.

In any way, this is probably too much programming effort unless you need to do it for many projects.

J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
0

What you need is your local maven nexus that is only available in your server. Considering you have jars that are only available to you, I guess that you have sources for them. As a result, you should be able to convert them into maven projects and deploy them into your local nexus. After fixing the structure, maven will download your dependencies and the dependencies' dependencies when resolving a project.

Or you could use <scope>system</scope>, but that requires absolute paths and is generally not suggested.

Dragas
  • 1,140
  • 13
  • 29