Search code examples
javamavenmaven-2

How to get list of dependencies of Maven project in java class


As we are migrating our legacy project to Maven, we are facing following issue.

We have one core project say, xyz and subprojects p1, p2. To add subproject into core project we just put subproject's jar into core project. Now when this core project gets started, it try to find if there are any subprojects by checking MANIFEST file of all jars in directory WEB-INF/lib of core project.

Now in Maven project structure, there is no lib directory, so can how I can get list of all jars getting used by core project(with their path)?

Any help is appreciated.


Solution

  • Maven dependencies plugin shows all dependencies of your project, even transitive ones. Please try:

    $ mvn dependency:tree
    

    If you need to figure out dependencies at runtime, it should be possible to inspect the classpath (which Maven sets up for you, when your project is started by Maven exec). For Java SE application without custom classloader the following should work:

        ClassLoader cl = ClassLoader.getSystemClassLoader();
    
        URL[] urls = ((URLClassLoader) cl).getURLs();
        for (URL url : urls) {
            System.out.println(url.getFile());
        }