views:

74

answers:

2

In a previous question I got an answer for downloading an artifact from the Maven repository. This works well for me, but I need to read the MavenProject for the downloaded artifact.

What is the best way for me to read the MavenProject for the downloaded artifact in my plugin?

+2  A: 

You can use the MavenProjectBuilder to resolve the artifact and read the downloaded pom into a MavenProject. The buildFromRepository() method will obtain the artifact (if needed) from the remote repositories so there is no need to download it before reading.

These are the changes needed to the previous answer resolve the maven project:

//other imports same as previous answer
import org.apache.maven.project.MavenProject;
import org.apache.maven.project.MavenProjectBuilder;
import org.apache.maven.project.ProjectBuildingException;

/**
 * Obtain the artifact defined by the groupId, artifactId, and version from the
 * remote repository.
 * 
 * @goal bootstrap
 */
public class BootstrapAppMojo extends AbstractMojo {

    /**
     * Used to resolve the maven project.
     * 
     * @parameter expression=
     *            "${component.org.apache.maven.project.MavenProjectBuilder}"
     * @required
     * @readonly
     */
    private MavenProjectBuilder mavenProjectBuilder;

    //rest of properties same as before.

    /**
     * The target pom's version
     * 
     * @parameter expression="${bootstrapVersion}"
     * @required
     */
    private String bootstrapVersion;

    public void execute() throws MojoExecutionException, MojoFailureException {
        try {
            Artifact pomArtifact = this.factory.createArtifact(
                bootstrapGroupId, bootstrapArtifactId, bootstrapVersion,
                "", bootstrapType);

            MavenProject project = mavenProjectBuilder.buildFromRepository(
                pomArtifact, this.remoteRepositories, this.localRepository);

            //do something with the project...
        } catch (ProjectBuildingException e) {
            getLog().error("can't build bootstrapped pom", e);
        }
    }
}
Rich Seller
+2  A: 

Some examples are available here: http://docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook.

Superfilin
+1 nice reference
Rich Seller