【问题标题】:How can I resolve an artifact from the Maven repository within a plugin?如何从插件中的 Maven 存储库中解析工件?
【发布时间】:2009-09-17 18:17:07
【问题描述】:

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

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

【问题讨论】:

    标签: java maven-2 maven-plugin


    【解决方案1】:

    您可以使用 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);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-01
        • 2016-04-04
        • 2016-10-08
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        • 2012-10-20
        相关资源
        最近更新 更多