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<
> m2spring@
> >:
>>> 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<
> 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(),
>>>>> 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: [email protected]
For additional commands, e-mail: [email protected]