2

在上一个问题中,我得到了从 Maven 存储库下载工件的答案。这对我来说效果很好,但我需要阅读 MavenProject 以获取下载的工件。

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

4

2 回答 2

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);
        }
    }
}
于 2009-09-17T18:20:59.263 回答
2

此处提供了一些示例:http: //docs.codehaus.org/display/MAVENUSER/Mojo+Developer+Cookbook

于 2009-09-17T18:27:26.560 回答