如何从插件中的Maven存储库中解析工件?

时间:2009-09-17 18:17:08

标签: java maven-2 maven-plugin

在上一个问题中,我从Maven资源库下载了一个工件answer。这对我很有用,但我需要阅读下载工件的MavenProject。

在我的插件中为下载的工件读取MavenProject的最佳方式是什么?

2 个答案:

答案 0 :(得分:4)

您可以使用MavenProjectBuilder来解析工件并将下载的pom读入MavenProject。 buildFromRepository()方法将从远程存储库获取工件(如果需要),因此在读取之前无需下载它。

这些是前一个答案解决maven项目所需的更改:

//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);
        }
    }
}

答案 1 :(得分:2)