How to read POM dependencies from file using Maven Resolver

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to read POM dependencies from file using Maven Resolver

Vincent Massol-5
Hi guys,

I’ve been trying to find how to programmatically resolve dependencies from a pom.xml file by using the Maven Resolver API (https://github.com/apache/maven-resolver).

So far I’ve succeeded in resolving dependencies from an Artifact but I haven’t found how to do by reading from a pom.xml file.

The closest I have founds is the following but it’s not working & it’s not using the Resolver API (and thus the passed parameters):

private void getArtifactDependenciesFromPOMFile(List<RemoteRepository> remoteRepositories, RepositorySystem repoSystem,
    RepositorySystemSession session) throws Exception
{
    DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest()
        .setPomFile(new File("./pom.xml”));
    ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
    ModelBuildingResult modelBuildingResult = modelBuilder.build(modelBuildingRequest);
    Model model = modelBuildingResult.getEffectiveModel();
}

It’s failing with:

java.lang.NullPointerException: request.modelResolver cannot be null (parent POM org.xwiki.platform:xwiki-platform-test:10.6-SNAPSHOT and POM org.xwiki.platform:xwiki-platform-test-war:[unknown-version] (/Users/vmassol/dev/xwiki/xwiki-platform/xwiki-platform-core/xwiki-platform-test/xwiki-platform-test-war/./pom.xml))

        at org.apache.commons.lang3.Validate.notNull(Validate.java:225)
        at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:994)

I assume that’s because it needs some MavenResolver at:

    DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest()
        .setPomFile(new File("./pom.xml”))
        .setResolver(“… what to put here?…”)

Is there another approach?

Thanks
-Vincent

PS: I’ve tried using ShrinkWrap but it’s not working for me, see https://massol.myxwiki.org/xwiki/bin/view/Blog/ResolveMavenArtifactsShrinkWrap


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to read POM dependencies from file using Maven Resolver

vmassol
Ok I’ve found a way but it looks complex… Please let me know if there’s a simpler way.

So I’ve copied the DefaultModelResolver.java class from maven sources (since it’s package private I couldn’t reuse it!) into my own sources, and then used what I mentioned below:

private void getArtifactDependenciesFromPOMFile(List<RemoteRepository> remoteRepositories,
    RepositorySystem repoSystem, RepositorySystemSession session) throws Exception
{
    ModelResolver resolver = new DefaultModelResolver(session, null, null, this.artifactResolver,
        this.versionRangeResolver, this.remoteRepositoryManager, remoteRepositories);

    DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest()
        .setPomFile(new File("./pom.xml"))
        .setModelResolver(resolver);

    ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
    ModelBuildingResult modelBuildingResult = modelBuilder.build(modelBuildingRequest);
    Model model = modelBuildingResult.getEffectiveModel();

}

Is that the only way?

Thanks
-Vincent

> On 20 Jul 2018, at 16:56, Vincent Massol <[hidden email]> wrote:
>
> Hi guys,
>
> I’ve been trying to find how to programmatically resolve dependencies from a pom.xml file by using the Maven Resolver API (https://github.com/apache/maven-resolver).
>
> So far I’ve succeeded in resolving dependencies from an Artifact but I haven’t found how to do by reading from a pom.xml file.
>
> The closest I have founds is the following but it’s not working & it’s not using the Resolver API (and thus the passed parameters):
>
> private void getArtifactDependenciesFromPOMFile(List<RemoteRepository> remoteRepositories, RepositorySystem repoSystem,
>    RepositorySystemSession session) throws Exception
> {
>    DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest()
>        .setPomFile(new File("./pom.xml”));
>    ModelBuilder modelBuilder = new DefaultModelBuilderFactory().newInstance();
>    ModelBuildingResult modelBuildingResult = modelBuilder.build(modelBuildingRequest);
>    Model model = modelBuildingResult.getEffectiveModel();
> }
>
> It’s failing with:
>
> java.lang.NullPointerException: request.modelResolver cannot be null (parent POM org.xwiki.platform:xwiki-platform-test:10.6-SNAPSHOT and POM org.xwiki.platform:xwiki-platform-test-war:[unknown-version] (/Users/vmassol/dev/xwiki/xwiki-platform/xwiki-platform-core/xwiki-platform-test/xwiki-platform-test-war/./pom.xml))
>
> at org.apache.commons.lang3.Validate.notNull(Validate.java:225)
> at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:994)
>
> I assume that’s because it needs some MavenResolver at:
>
>    DefaultModelBuildingRequest modelBuildingRequest = new DefaultModelBuildingRequest()
>        .setPomFile(new File("./pom.xml”))
>        .setResolver(“… what to put here?…”)
>
> Is there another approach?
>
> Thanks
> -Vincent
>
> PS: I’ve tried using ShrinkWrap but it’s not working for me, see https://massol.myxwiki.org/xwiki/bin/view/Blog/ResolveMavenArtifactsShrinkWrap
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]