You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@maven.apache.org by juliangeo <ju...@gmail.com> on 2014/01/07 23:30:05 UTC

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

Hi! 

I'm new to Maven and I've been struggling with this for a while. I just need
to be able to programmatically build a project from a pom file in order to
retrieve its relevant information. I am not using, nor want to, a maven
plugin, just need this functionality as part of my application.

I've tried the code you successfully got to work but no luck so far. I still
get the "mavenTools: null" from the repositoryManager being null. Am I
missing something? Should I run this on some kind of special environment?

I'd appreciate any help as I'm really lost here.

Thanks in advance.


Max Spring wrote
> 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&lt;

> m2spring@

> &gt;:
>>> 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&lt;

> deusaquilus@

> &gt;
>>>>   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...
>>>>
>>>> ---------------------------------------------------------------------





--
View this message in context: http://maven.40175.n5.nabble.com/Programmatically-get-maven-properties-tp4912280p5780906.html
Sent from the Maven - Users mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org


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

Posted by Barrie Treloar <ba...@gmail.com>.
On 8 January 2014 09:00, juliangeo <ju...@gmail.com> wrote:
> Hi!
>
> I'm new to Maven and I've been struggling with this for a while. I just need
> to be able to programmatically build a project from a pom file in order to
> retrieve its relevant information. I am not using, nor want to, a maven
> plugin, just need this functionality as part of my application.
>
> I've tried the code you successfully got to work but no luck so far. I still
> get the "mavenTools: null" from the repositoryManager being null. Am I
> missing something? Should I run this on some kind of special environment?
>
> I'd appreciate any help as I'm really lost here.
[del]
>>>>> 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.

Your answer is already here.

You just can't use the classes outside of the Maven environment
without also setting up all the other dependencies that are needed by
that class.
And as noted, this is done by Dependency Injection (via Plexus).
You will need to look at MavenArtifactRepository and see what other
objects need to be instantiated and set into it for it to work
properly.

Your use case is not something others are really trying to do, so you
are unlikely to find someone with the knowledge to help.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@maven.apache.org
For additional commands, e-mail: users-help@maven.apache.org