Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programatically getting an effective POM using Maven Resolver Provider

Tags:

java

maven

aether

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?
like image 481
Sahil Muthoo Avatar asked Apr 05 '19 03:04

Sahil Muthoo


1 Answers

You can create (in your project) a public class under the package: org.apache.maven.repository.internal that extends the package-accessibility class. Just use a class name that is not possible to be used in the furutre by the vendor.

package org.apache.maven.repository.internal;
public class VisibleDefaultModelResolver extends DefaultModelResolver{

    public VisibleDefaultModelResolver(RepositorySystemSession session, RequestTrace trace, String context, ArtifactResolver resolver, VersionRangeResolver versionRangeResolver, RemoteRepositoryManager remoteRepositoryManager, List<RemoteRepository> repositories) {
        super(session, trace, context, resolver, versionRangeResolver, remoteRepositoryManager, repositories);
    }

}

Then your code becomes:

ModelResolver modelResolver = new VisibleDefaultModelResolver(session, requestTrace, "context", artifactResolver, versionRangeResolver, remoteRepositoryManager, repos);
like image 116
Marinos An Avatar answered Sep 28 '22 21:09

Marinos An