Is there a way to create maven dependency in pom file from a local jar

Jatin Vasnani :

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.

Milen Dyankov :

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("<depencency>");
            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("</depencency>");
         });

        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.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=327036&siteId=1