Quantcast

Programmatically get maven properties

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Programmatically get maven properties

deusaquilus
I'm trying to use the maven programmatic APIs to get the properties of the default profile (i.e. the stuff in Project->Properties->XYZ) and return them as a Java 'Properties' object, so far with no luck. First I thought of trying to use the maven invoker e.g:

InvocationRequest invocationRequest = new DefaultInvocationRequest();
invocationRequest.setPomFile( new File( "pom.xml" ) );
Properties properties = invocationRequest.getProperties();

That just seems to return null. The other way I tried was using the maven builder APIs like so:

File pom = new File("pom.xml");
DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
DefaultProjectBuilder builder = new DefaultProjectBuilder();

String mavenHome = System.getenv("M2_HOME");
ArtifactRepository localRepository = new MavenArtifactRepository(
        "local",
        new File(mavenHome + "/repository").toURI().toURL().toString(),
        new DefaultRepositoryLayout(),
        new ArtifactRepositoryPolicy(),
        new ArtifactRepositoryPolicy());
request.setLocalRepository(localRepository);
MavenProject project = builder.build(pom, request).getProject();
Properties properties = project.getProperties();

That causes a "mavenTools: null" exception to be thrown from the MavenProject class.
How do I get this to work?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Programmatically get maven properties

Brian Topping
The code you put in your original email didn't make it to the list, maybe it was formatted somehow.  

What I think you are looking for is:

> /** @parameter default-value="${project}" */
> private org.apache.maven.project.MavenProject mavenProject;

This will give you the MavenProject object, which you can dereference to find the properties.

HTH, Brian

On Oct 17, 2011, at 11:27 PM, deusaquilus wrote:

> I'm trying to use the maven programmatic APIs to get the properties of the
> default profile (i.e. the stuff in Project->Properties->XYZ) and return them
> as a Java 'Properties' object, so far with no luck. First I thought of
> trying to use the maven invoker e.g:
>
>
>
> That just seems to return null. The other way I tried was using the maven
> builder APIs like so:
>
>
>
> That causes a "mavenTools: null" exception to be thrown from the
> MavenProject class.
> How do I get this to work?
>
> --
> View this message in context: http://maven.40175.n5.nabble.com/Programmatically-get-maven-properties-tp4912280p4912280.html
> Sent from the Maven - Users mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> 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
star

Re: Programmatically get maven properties

Barrie Treloar
On Tue, Oct 18, 2011 at 2:22 PM, Brian Topping <[hidden email]> wrote:
> The code you put in your original email didn't make it to the list, maybe it was formatted somehow.
>
> What I think you are looking for is:
>
>> /** @parameter default-value="${project}" */
>> private org.apache.maven.project.MavenProject mavenProject;
>
> This will give you the MavenProject object, which you can dereference to find the properties.

Also check out the articles on Plugin development at
http://maven.apache.org/guides/index.html - Section "Plugins"

---------------------------------------------------------------------
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
star

Re: Programmatically get maven properties

deusaquilus
In reply to this post by Brian Topping
Here's what I'm doing:

File pom = new File("pom.xml");
DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
DefaultProjectBuilder builder = new DefaultProjectBuilder();

String mavenHome = System.getenv("M2_HOME");
ArtifactRepository localRepository = new MavenArtifactRepository(
        "local",
        new File(mavenHome + "/repository").toURI().toURL().toString(),
        new DefaultRepositoryLayout(),
        new ArtifactRepositoryPolicy(),
        new ArtifactRepositoryPolicy());
request.setLocalRepository(localRepository);
MavenProject project = builder.build(pom, request).getProject();
Properties properties = project.getProperties();

Trouble is it's giving me errors with "mavenTools: null"
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Programmatically get maven properties

Barrie Treloar
On Tue, Oct 18, 2011 at 10:02 PM, deusaquilus <[hidden email]> wrote:

> Here's what I'm doing:
>
> File pom = new File("pom.xml");
> DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
> DefaultProjectBuilder builder = new DefaultProjectBuilder();
>
> String mavenHome = System.getenv("M2_HOME");
> ArtifactRepository localRepository = new MavenArtifactRepository(
>        "local",
>        new File(mavenHome + "/repository").toURI().toURL().toString(),
>        new DefaultRepositoryLayout(),
>        new ArtifactRepositoryPolicy(),
>        new ArtifactRepositoryPolicy());
> request.setLocalRepository(localRepository);
> MavenProject project = builder.build(pom, request).getProject();
> Properties properties = project.getProperties();
>
> Trouble is it's giving me errors with "mavenTools: null"

I'd be surprised if that worked.

Maven makes heavy use of dependency injection via Plexus and chances
are you haven't set up some component that is being used.

What is your actual use case.  I dont see why you would want to expose
Maven properties as a Java properties object...

---------------------------------------------------------------------
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
star

Re: Programmatically get maven properties

Jörg Schaible-3
In reply to this post by deusaquilus
deusaquilus wrote:

> Here's what I'm doing:
>
> File pom = new File("pom.xml");
> DefaultProjectBuildingRequest request = new
> DefaultProjectBuildingRequest(); DefaultProjectBuilder builder = new
> DefaultProjectBuilder();
>
> String mavenHome = System.getenv("M2_HOME");
> ArtifactRepository localRepository = new MavenArtifactRepository(
>         "local",
>         new File(mavenHome + "/repository").toURI().toURL().toString(),


BTW: *My* repository is not at this location and most of my colleagues
configured it to be somewhere else also ...


>         new DefaultRepositoryLayout(),
>         new ArtifactRepositoryPolicy(),
>         new ArtifactRepositoryPolicy());
> request.setLocalRepository(localRepository);
> MavenProject project = builder.build(pom, request).getProject();
> Properties properties = project.getProperties();
>
> Trouble is it's giving me errors with "mavenTools: null"
>
> --
> View this message in context:
> http://maven.40175.n5.nabble.com/Programmatically-get-maven-properties-
tp4912280p4913348.html
> Sent from the Maven - Users mailing list archive at Nabble.com.



---------------------------------------------------------------------
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
star

DefaultProjectBuilder ... mavenTools: null [Was: Re: Programmatically get maven properties]

Max Spring
In reply to this post by Barrie Treloar
I'm running into the same "mavenTools: null" problem, but my code sits in a Maven 3.0.3 mojo:

   public class MyMojo extends AbstractMojo{

     /** @parameter default-value="${repositorySystemSession}" */
     private RepositorySystemSession repoSession;

     public void execute() throws MojoExecutionException{
       DefaultProjectBuilder builder = new DefaultProjectBuilder();
       DefaultProjectBuildingRequest req = new DefaultProjectBuildingRequest();
       req.setRepositorySession(repoSession);
       builder.build(new File("my-pom.xml"),req);

       MavenProject prj = req.getProject();
       Model model = prj.getModel();
     }
   }

It barfs with

   Caused by: java.lang.IllegalArgumentException: mavenTools: null
     at org.apache.maven.project.MavenProject.<init>(MavenProject.java:249)
     at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:128)
     at org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:102)
     at MyMojo.execute(MyMojo.java:59)

This is because repositorySystem in DefaultProjectBuilder is null, i.e. hasn't been injected:

     @Requirement
     private RepositorySystem repositorySystem;

How can I get the right repositorySystem injected?

Context:
I'm essentially trying to do the M2E "Import Project" use case, but completely outside of Eclipse.
I want to populate a workspace using the SCM pointer given by a POM fetched from the group repository.

Thanks!
-Max


On 10/18/2011 04:59 AM, Barrie Treloar wrote:

> On Tue, Oct 18, 2011 at 10:02 PM, deusaquilus<[hidden email]>  wrote:
>> Here's what I'm doing:
>>
>> File pom = new File("pom.xml");
>> DefaultProjectBuildingRequest request = new DefaultProjectBuildingRequest();
>> DefaultProjectBuilder builder = new DefaultProjectBuilder();
>>
>> String mavenHome = System.getenv("M2_HOME");
>> ArtifactRepository localRepository = new MavenArtifactRepository(
>>         "local",
>>         new File(mavenHome + "/repository").toURI().toURL().toString(),
>>         new DefaultRepositoryLayout(),
>>         new ArtifactRepositoryPolicy(),
>>         new ArtifactRepositoryPolicy());
>> request.setLocalRepository(localRepository);
>> MavenProject project = builder.build(pom, request).getProject();
>> Properties properties = project.getProperties();
>>
>> Trouble is it's giving me errors with "mavenTools: null"
>
> I'd be surprised if that worked.
>
> Maven makes heavy use of dependency injection via Plexus and chances
> are you haven't set up some component that is being used.
>
> What is your actual use case.  I dont see why you would want to expose
> Maven properties as a Java properties object...
>
> ---------------------------------------------------------------------
> 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
star

Re: DefaultProjectBuilder ... mavenTools: null [Was: Re: Programmatically get maven properties]

Olivier Lamy
Hello,

You must probably use:

    /**
     * @component
     */
    private ProjectBuilder projectBuilder;

2012/2/10 Max Spring <[hidden email]>:

> I'm running into the same "mavenTools: null" problem, but my code sits in a
> Maven 3.0.3 mojo:
>
>  public class MyMojo extends AbstractMojo{
>
>    /** @parameter default-value="${repositorySystemSession}" */
>    private RepositorySystemSession repoSession;
>
>    public void execute() throws MojoExecutionException{
>      DefaultProjectBuilder builder = new DefaultProjectBuilder();
>      DefaultProjectBuildingRequest req = new
> DefaultProjectBuildingRequest();
>      req.setRepositorySession(repoSession);
>      builder.build(new File("my-pom.xml"),req);
>
>      MavenProject prj = req.getProject();
>      Model model = prj.getModel();
>    }
>  }
>
> It barfs with
>
>  Caused by: java.lang.IllegalArgumentException: mavenTools: null
>    at org.apache.maven.project.MavenProject.<init>(MavenProject.java:249)
>    at
> org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:128)
>    at
> org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:102)
>    at MyMojo.execute(MyMojo.java:59)
>
> This is because repositorySystem in DefaultProjectBuilder is null, i.e.
> hasn't been injected:
>
>    @Requirement
>    private RepositorySystem repositorySystem;
>
> How can I get the right repositorySystem injected?
>
> Context:
> I'm essentially trying to do the M2E "Import Project" use case, but
> completely outside of Eclipse.
> I want to populate a workspace using the SCM pointer given by a POM fetched
> from the group repository.
>
> Thanks!
> -Max
>
>
> On 10/18/2011 04:59 AM, Barrie Treloar wrote:
>>
>> On Tue, Oct 18, 2011 at 10:02 PM, deusaquilus<[hidden email]>
>>  wrote:
>>>
>>> Here's what I'm doing:
>>>
>>> File pom = new File("pom.xml");
>>> DefaultProjectBuildingRequest request = new
>>> DefaultProjectBuildingRequest();
>>> DefaultProjectBuilder builder = new DefaultProjectBuilder();
>>>
>>> String mavenHome = System.getenv("M2_HOME");
>>> ArtifactRepository localRepository = new MavenArtifactRepository(
>>>        "local",
>>>        new File(mavenHome + "/repository").toURI().toURL().toString(),
>>>        new DefaultRepositoryLayout(),
>>>        new ArtifactRepositoryPolicy(),
>>>        new ArtifactRepositoryPolicy());
>>> request.setLocalRepository(localRepository);
>>> MavenProject project = builder.build(pom, request).getProject();
>>> Properties properties = project.getProperties();
>>>
>>> Trouble is it's giving me errors with "mavenTools: null"
>>
>>
>> I'd be surprised if that worked.
>>
>> Maven makes heavy use of dependency injection via Plexus and chances
>> are you haven't set up some component that is being used.
>>
>> What is your actual use case.  I dont see why you would want to expose
>> Maven properties as a Java properties object...
>>
>> ---------------------------------------------------------------------
>> 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]
>



--
Olivier Lamy
Talend: http://coders.talend.com
http://twitter.com/olamy | http://linkedin.com/in/olamy

---------------------------------------------------------------------
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
star

Re: DefaultProjectBuilder ... mavenTools: null [Was: Re: Programmatically get maven properties]

Max Spring
Yes, having a projectBuilder in the Mojo did the trick.
Thank you!
The complete working example is down below.
-Max

package org.example;

import java.io.File;

import org.apache.maven.model.Model;
import org.apache.maven.model.building.ModelBuildingRequest;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.DefaultProjectBuildingRequest;
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.ProjectBuilder;
import org.apache.maven.project.ProjectBuildingResult;
import org.sonatype.aether.RepositorySystemSession;

/**
  * run with: mvn $groupId:$artifactId:$version:fetch-pom -DpomFile=$pomFile
  *
  * @goal fetch-pom
  */
public class FetchModelMojo extends AbstractMojo{

   /**
    * Current repository/network configuration of Maven.
    * @parameter default-value="${repositorySystemSession}"
    * @readonly
    */
   private RepositorySystemSession repoSession;

   /**
    * @component
    */
   private ProjectBuilder projectBuilder;

   /**
    * @parameter expression="${pomFile}" default-value=""
    */
   private String pomFile;

   public void execute() throws MojoExecutionException {
     try {
       DefaultProjectBuildingRequest req = new DefaultProjectBuildingRequest();
       req.setRepositorySession(repoSession);
       req.setValidationLevel(ModelBuildingRequest.VALIDATION_LEVEL_STRICT);
       ProjectBuildingResult res = projectBuilder.build(new File(pomFile),req);

       // do something with the project
       MavenProject prj = res.getProject();
       System.out.println("prj=" + prj);
       Model model = prj.getModel();
       System.out.println("id="+model.getId());
     } catch (Exception e) {
       throw new MojoExecutionException(e.getMessage(), e);
     }
   }
}


On 02/10/2012 01:19 PM, Olivier Lamy wrote:

> Hello,
>
> You must probably use:
>
>      /**
>       * @component
>       */
>      private ProjectBuilder projectBuilder;
>
> 2012/2/10 Max Spring<[hidden email]>:
>> I'm running into the same "mavenTools: null" problem, but my code sits in a
>> Maven 3.0.3 mojo:
>>
>>   public class MyMojo extends AbstractMojo{
>>
>>     /** @parameter default-value="${repositorySystemSession}" */
>>     private RepositorySystemSession repoSession;
>>
>>     public void execute() throws MojoExecutionException{
>>       DefaultProjectBuilder builder = new DefaultProjectBuilder();
>>       DefaultProjectBuildingRequest req = new
>> DefaultProjectBuildingRequest();
>>       req.setRepositorySession(repoSession);
>>       builder.build(new File("my-pom.xml"),req);
>>
>>       MavenProject prj = req.getProject();
>>       Model model = prj.getModel();
>>     }
>>   }
>>
>> It barfs with
>>
>>   Caused by: java.lang.IllegalArgumentException: mavenTools: null
>>     at org.apache.maven.project.MavenProject.<init>(MavenProject.java:249)
>>     at
>> org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:128)
>>     at
>> org.apache.maven.project.DefaultProjectBuilder.build(DefaultProjectBuilder.java:102)
>>     at MyMojo.execute(MyMojo.java:59)
>>
>> This is because repositorySystem in DefaultProjectBuilder is null, i.e.
>> hasn't been injected:
>>
>>     @Requirement
>>     private RepositorySystem repositorySystem;
>>
>> How can I get the right repositorySystem injected?
>>
>> Context:
>> I'm essentially trying to do the M2E "Import Project" use case, but
>> completely outside of Eclipse.
>> I want to populate a workspace using the SCM pointer given by a POM fetched
>> from the group repository.
>>
>> Thanks!
>> -Max
>>
>>
>> On 10/18/2011 04:59 AM, Barrie Treloar wrote:
>>>
>>> On Tue, Oct 18, 2011 at 10:02 PM, deusaquilus<[hidden email]>
>>>   wrote:
>>>>
>>>> Here's what I'm doing:
>>>>
>>>> File pom = new File("pom.xml");
>>>> DefaultProjectBuildingRequest request = new
>>>> DefaultProjectBuildingRequest();
>>>> DefaultProjectBuilder builder = new DefaultProjectBuilder();
>>>>
>>>> String mavenHome = System.getenv("M2_HOME");
>>>> ArtifactRepository localRepository = new MavenArtifactRepository(
>>>>         "local",
>>>>         new File(mavenHome + "/repository").toURI().toURL().toString(),
>>>>         new DefaultRepositoryLayout(),
>>>>         new ArtifactRepositoryPolicy(),
>>>>         new ArtifactRepositoryPolicy());
>>>> request.setLocalRepository(localRepository);
>>>> MavenProject project = builder.build(pom, request).getProject();
>>>> Properties properties = project.getProperties();
>>>>
>>>> Trouble is it's giving me errors with "mavenTools: null"
>>>
>>>
>>> I'd be surprised if that worked.
>>>
>>> Maven makes heavy use of dependency injection via Plexus and chances
>>> are you haven't set up some component that is being used.
>>>
>>> What is your actual use case.  I dont see why you would want to expose
>>> Maven properties as a Java properties object...
>>>
>>> ---------------------------------------------------------------------
>>> 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...