Search code examples
javamavenaether

Programatically getting an effective POM using Maven Resolver Provider


What do I want to do?

  • Given a POM file on the local filesystem.
  • I want to programmatically obtain the effective POM of that POM file. Specifically I want to do the following:

    • Resolve the POMs dependencies
    • Ensure that all parent POMs are processed
    • Obtain the list of dependencies of the fully resolved POM
    • And so on...
  • I don't need to obtain transitive dependencies.

What works?

I'm using Maven Resolver Provider which sort of works. However I have to use a package private class org.apache.maven.repository.internal.DefaultModelResolver

Here a GitHub link to a sample Maven project that you can run: https://github.com/sahilm/maven-resolver-test

The example program does the following:

  • Downloads the latest spring boot POM from Maven Central.
  • Prints out it's direct dependencies (with parent deps included)

You can run the the program with: mvn exec:java -Dexec.mainClass="com.sahilm.maven_resolver_test.Test"

What I need help with?

  • I need help with understanding why I have to use a package private class to get stuff to work.
  • Is there another way to get the information I need?

Solution

  • Maybe you can use ProjectModelResolver. Here's a code snippet,

        DefaultRepositorySystem repositorySystem =
                new DefaultRepositorySystem();
        repositorySystem.initService(locator);
    
        ModelResolver modelResolver =
                new ProjectModelResolver(session, requestTrace,
                        repositorySystem, remoteRepositoryManager, repos,
                        ProjectBuildingRequest.RepositoryMerging.POM_DOMINANT,
                        null);
    

    I've included a working code here.