How is the best way to get an effective POM outside of a Maven Plugin?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How is the best way to get an effective POM outside of a Maven Plugin?

Sandra Parsick
Hello,

currently, I'm working on a small tool, that needs some information of a POM and its parent POM. I didn't want to parse them with a XML reader, so I found that the DefaultModelBuilder can build a complete effective POM for me. Based on a stackoverflow answer (https://stackoverflow.com/questions/27383357/how-do-i-instantiate-mavenresolver-for-modelbuilder-usage)  I build following example:

DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
 final DefaultModelBuilder defaultModelBuilder = factory.newInstance();
 ModelBuildingRequest buildRequest = new DefaultModelBuildingRequest();
 buildRequest.setPomFile(Paths.get("/path/to/a/pom.xml").toFile())
         .setProcessPlugins(false)
         .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
 final ModelBuildingResult modelBuildingResult = defaultModelBuilder.build(buildRequest);

 
It works fine for simple POM (simple means a POM without a parent reference). But if I set a POM with a parent reference in buildRequest.setPomFile, I get following exception:

Exception in thread "main" java.lang.IllegalArgumentException: no model resolver provided, cannot resolve parent POM groupId:artifactId:1 for POM groupId:artifactId:version (/path/to/a/pom.xml)
 at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:890)
 at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:750)
 at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:309)
 at com.github.sparsick.maven.effecitve.pom.maven.model.builder.ApacheMavenModelBuilder.main(ApacheMavenModelBuilder.java:17)

Another example on Stackoverflow (https://stackoverflow.com/questions/4838591/is-there-a-library-for-reading-maven2-3-pom-xml-files) mentions to use RepositoryModelResolver from Apache Archiva as an implementation ModelResolver. But I don't want to introduce a dependency to Apache Archiva in my tool.

So my questions are, is the usage of DefaultModelBuilder the best way to get an effective POM of my project outside of a Maven Plugin? If so which implementation I should use for the ModelResolver? If not what is the best way to get the effective POM outside of a Maven Plugin.

Thank you and best regards,

Sandra Parsick

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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: How is the best way to get an effective POM outside of a Maven Plugin?

Karl Heinz Marbaise-3
Hi Sandra,

first question: Do you need the full resolved pom (effective pom) or
would it be enough just to simply to parse the pom file which can simply
being done using MavenXpp3Reader this would deliver the XML as a Maven
Model class which can simply being used to extract information from the
maven model...

Kind regards
Karl Heinz Marbaise

On 09/08/17 13:00, [hidden email] wrote:

> Hello,
>
> currently, I'm working on a small tool, that needs some information of a POM and its parent POM. I didn't want to parse them with a XML reader, so I found that the DefaultModelBuilder can build a complete effective POM for me. Based on a stackoverflow answer (https://stackoverflow.com/questions/27383357/how-do-i-instantiate-mavenresolver-for-modelbuilder-usage)  I build following example:
>
> DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
>   final DefaultModelBuilder defaultModelBuilder = factory.newInstance();
>   ModelBuildingRequest buildRequest = new DefaultModelBuildingRequest();
>   buildRequest.setPomFile(Paths.get("/path/to/a/pom.xml").toFile())
>           .setProcessPlugins(false)
>           .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
>   final ModelBuildingResult modelBuildingResult = defaultModelBuilder.build(buildRequest);
>
>  
> It works fine for simple POM (simple means a POM without a parent reference). But if I set a POM with a parent reference in buildRequest.setPomFile, I get following exception:
>
> Exception in thread "main" java.lang.IllegalArgumentException: no model resolver provided, cannot resolve parent POM groupId:artifactId:1 for POM groupId:artifactId:version (/path/to/a/pom.xml)
>   at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:890)
>   at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:750)
>   at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:309)
>   at com.github.sparsick.maven.effecitve.pom.maven.model.builder.ApacheMavenModelBuilder.main(ApacheMavenModelBuilder.java:17)
>
> Another example on Stackoverflow (https://stackoverflow.com/questions/4838591/is-there-a-library-for-reading-maven2-3-pom-xml-files) mentions to use RepositoryModelResolver from Apache Archiva as an implementation ModelResolver. But I don't want to introduce a dependency to Apache Archiva in my tool.
>
> So my questions are, is the usage of DefaultModelBuilder the best way to get an effective POM of my project outside of a Maven Plugin? If so which implementation I should use for the ModelResolver? If not what is the best way to get the effective POM outside of a Maven Plugin.
>
> Thank you and best regards,
>
> Sandra Parsick
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aw: Re: How is the best way to get an effective POM outside of a Maven Plugin?

Sandra Parsick
Hi Karl Heinz,

unfortunately, I need the full resolved pom.

Best regards,

Sandra Parsick

> Gesendet: Mittwoch, 09. August 2017 um 19:24 Uhr
> Von: "Karl Heinz Marbaise" <[hidden email]>
> An: "Maven Users List" <[hidden email]>, [hidden email]
> Betreff: Re: How is the best way to get an effective POM outside of a Maven Plugin?
>
> Hi Sandra,
>
> first question: Do you need the full resolved pom (effective pom) or
> would it be enough just to simply to parse the pom file which can simply
> being done using MavenXpp3Reader this would deliver the XML as a Maven
> Model class which can simply being used to extract information from the
> maven model...
>
> Kind regards
> Karl Heinz Marbaise
>
> On 09/08/17 13:00, [hidden email] wrote:
> > Hello,
> >
> > currently, I'm working on a small tool, that needs some information of a POM and its parent POM. I didn't want to parse them with a XML reader, so I found that the DefaultModelBuilder can build a complete effective POM for me. Based on a stackoverflow answer (https://stackoverflow.com/questions/27383357/how-do-i-instantiate-mavenresolver-for-modelbuilder-usage)  I build following example:
> >
> > DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
> >   final DefaultModelBuilder defaultModelBuilder = factory.newInstance();
> >   ModelBuildingRequest buildRequest = new DefaultModelBuildingRequest();
> >   buildRequest.setPomFile(Paths.get("/path/to/a/pom.xml").toFile())
> >           .setProcessPlugins(false)
> >           .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
> >   final ModelBuildingResult modelBuildingResult = defaultModelBuilder.build(buildRequest);
> >
> >  
> > It works fine for simple POM (simple means a POM without a parent reference). But if I set a POM with a parent reference in buildRequest.setPomFile, I get following exception:
> >
> > Exception in thread "main" java.lang.IllegalArgumentException: no model resolver provided, cannot resolve parent POM groupId:artifactId:1 for POM groupId:artifactId:version (/path/to/a/pom.xml)
> >   at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:890)
> >   at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:750)
> >   at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:309)
> >   at com.github.sparsick.maven.effecitve.pom.maven.model.builder.ApacheMavenModelBuilder.main(ApacheMavenModelBuilder.java:17)
> >
> > Another example on Stackoverflow (https://stackoverflow.com/questions/4838591/is-there-a-library-for-reading-maven2-3-pom-xml-files) mentions to use RepositoryModelResolver from Apache Archiva as an implementation ModelResolver. But I don't want to introduce a dependency to Apache Archiva in my tool.
> >
> > So my questions are, is the usage of DefaultModelBuilder the best way to get an effective POM of my project outside of a Maven Plugin? If so which implementation I should use for the ModelResolver? If not what is the best way to get the effective POM outside of a Maven Plugin.
> >
> > Thank you and best regards,
> >
> > Sandra Parsick
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]
> > For additional commands, e-mail: [hidden email]
> >
> >
>

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

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aw: Re: How is the best way to get an effective POM outside of a Maven Plugin?

Sandra Parsick
Hello,

I found a solution for my problem that I'd like to share with you. Maybe it helps others, too.

JBoss's ShrinkWrap Resolver makes it possible to run Maven embedded in your code. I use it to run "help:effective-pom -Doutput=effective-pom.xml". Then I read "effective-pom.xml" with MavenXpp3Reader that was mentioned by Karl Heinz (thank you for that hint). This works only for poms that haven't a multi module list. For poms with multi module list, the Maven Help Plugin creates a effective-pom.xml with several <project> elements wrapped by a <projects> element. This structure isn't known by MavenXpp3Reader.

The code snippet for that:
        EmbeddedMaven.forProject("/path/to/pom.xml")
                     .addProperty("output", "effective-pom.xml")
                     .setGoals("help:effective-pom")
                     .build();
        MavenXpp3Reader pomReader = new MavenXpp3Reader();
        final Model pomModel = pomReader.read(new FileInputStream("/path/to/effective-pom.xml"));

        System.out.println(pomModel.getDistributionManagement().getDownloadUrl());

As dependencies, you need:
    <properties>
        <version.shrinkwrap.resolvers>3.0.0-beta-2</version.shrinkwrap.resolvers>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-api-maven-embedded</artifactId>
            <version>${version.shrinkwrap.resolvers}</version>
        </dependency>
        <dependency>
            <groupId>org.jboss.shrinkwrap.resolver</groupId>
            <artifactId>shrinkwrap-resolver-impl-maven-embedded</artifactId>
            <version>${version.shrinkwrap.resolvers}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-model</artifactId>
            <version>3.5.0</version>
        </dependency>
    </dependencies>


Best regards,

Sandra

> Gesendet: Donnerstag, 10. August 2017 um 07:41 Uhr
> Von: [hidden email]
> An: [hidden email]
> Cc: "Maven Users List" <[hidden email]>
> Betreff: Aw: Re: How is the best way to get an effective POM outside of a Maven Plugin?
>
> Hi Karl Heinz,
>
> unfortunately, I need the full resolved pom.
>
> Best regards,
>
> Sandra Parsick
>
> > Gesendet: Mittwoch, 09. August 2017 um 19:24 Uhr
> > Von: "Karl Heinz Marbaise" <[hidden email]>
> > An: "Maven Users List" <[hidden email]>, [hidden email]
> > Betreff: Re: How is the best way to get an effective POM outside of a Maven Plugin?
> >
> > Hi Sandra,
> >
> > first question: Do you need the full resolved pom (effective pom) or
> > would it be enough just to simply to parse the pom file which can simply
> > being done using MavenXpp3Reader this would deliver the XML as a Maven
> > Model class which can simply being used to extract information from the
> > maven model...
> >
> > Kind regards
> > Karl Heinz Marbaise
> >
> > On 09/08/17 13:00, [hidden email] wrote:
> > > Hello,
> > >
> > > currently, I'm working on a small tool, that needs some information of a POM and its parent POM. I didn't want to parse them with a XML reader, so I found that the DefaultModelBuilder can build a complete effective POM for me. Based on a stackoverflow answer (https://stackoverflow.com/questions/27383357/how-do-i-instantiate-mavenresolver-for-modelbuilder-usage)  I build following example:
> > >
> > > DefaultModelBuilderFactory factory = new DefaultModelBuilderFactory();
> > >   final DefaultModelBuilder defaultModelBuilder = factory.newInstance();
> > >   ModelBuildingRequest buildRequest = new DefaultModelBuildingRequest();
> > >   buildRequest.setPomFile(Paths.get("/path/to/a/pom.xml").toFile())
> > >           .setProcessPlugins(false)
> > >           .setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_MINIMAL);
> > >   final ModelBuildingResult modelBuildingResult = defaultModelBuilder.build(buildRequest);
> > >
> > >  
> > > It works fine for simple POM (simple means a POM without a parent reference). But if I set a POM with a parent reference in buildRequest.setPomFile, I get following exception:
> > >
> > > Exception in thread "main" java.lang.IllegalArgumentException: no model resolver provided, cannot resolve parent POM groupId:artifactId:1 for POM groupId:artifactId:version (/path/to/a/pom.xml)
> > >   at org.apache.maven.model.building.DefaultModelBuilder.readParentExternally(DefaultModelBuilder.java:890)
> > >   at org.apache.maven.model.building.DefaultModelBuilder.readParent(DefaultModelBuilder.java:750)
> > >   at org.apache.maven.model.building.DefaultModelBuilder.build(DefaultModelBuilder.java:309)
> > >   at com.github.sparsick.maven.effecitve.pom.maven.model.builder.ApacheMavenModelBuilder.main(ApacheMavenModelBuilder.java:17)
> > >
> > > Another example on Stackoverflow (https://stackoverflow.com/questions/4838591/is-there-a-library-for-reading-maven2-3-pom-xml-files) mentions to use RepositoryModelResolver from Apache Archiva as an implementation ModelResolver. But I don't want to introduce a dependency to Apache Archiva in my tool.
> > >
> > > So my questions are, is the usage of DefaultModelBuilder the best way to get an effective POM of my project outside of a Maven Plugin? If so which implementation I should use for the ModelResolver? If not what is the best way to get the effective POM outside of a Maven Plugin.
> > >
> > > Thank you and best regards,
> > >
> > > Sandra Parsick
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [hidden email]
> > > For additional commands, e-mail: [hidden email]
> > >
> > >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

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

Loading...