【问题标题】:How can I download Maven artifacts within a plugin?如何在插件中下载 Maven 工件?
【发布时间】:2010-11-29 05:45:50
【问题描述】:

我有一个 Maven 插件,它在其配置中采用 groupId、artifactId 和 version。

我希望能够从远程存储库下载该工件并将文件复制到项目中。我不知道如何下载该工件。

我知道我可以使用依赖插件解决依赖关系,但我需要它在我的插件中发生。我该怎么做?

【问题讨论】:

    标签: java maven-2 maven-plugin


    【解决方案1】:

    您的插件需要使用 ArtifactFactory 以及要引导的工件的 groupId、artifactId 和版本创建工件,然后将该工件传递给 ArtifactResolver 以处理发现/下载。

    解析器需要访问本地存储库和远程存储库。好消息是,所有这些都是 plexus 组件,您可以在 Mojo 中声明为依赖项,并让 Plexus 为您连接它们。

    another answer 中,我展示了如何做到这一点。在您的情况下,您需要一个参数略有不同的精简版本来读取 groupId、artifactId 和版本。在下面的插件中,各种组件声明为plexus组件,属性声明groupId、artifactId、version和打包类型。

    package name.seller.rich.maven.plugins.bootstrap;
    
    import java.util.List;
    
    import org.apache.maven.artifact.Artifact;
    import org.apache.maven.artifact.factory.ArtifactFactory;
    import org.apache.maven.artifact.repository.ArtifactRepository;
    import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
    import org.apache.maven.artifact.resolver.ArtifactResolutionException;
    import org.apache.maven.artifact.resolver.ArtifactResolver;
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugin.MojoFailureException;
    
    /**
     * Obtain the artifact defined by the groupId, artifactId, and version
     * from the remote repository.
     * 
     * @goal bootstrap
     */
    public class BootstrapAppMojo extends AbstractMojo {
    
        /**
         * Used to look up Artifacts in the remote repository.
         * 
         * @parameter expression=
         *  "${component.org.apache.maven.artifact.factory.ArtifactFactory}"
         * @required
         * @readonly
         */
        protected ArtifactFactory factory;
    
        /**
         * Used to look up Artifacts in the remote repository.
         * 
         * @parameter expression=
         *  "${component.org.apache.maven.artifact.resolver.ArtifactResolver}"
         * @required
         * @readonly
         */
        protected ArtifactResolver artifactResolver;
    
        /**
         * List of Remote Repositories used by the resolver
         * 
         * @parameter expression="${project.remoteArtifactRepositories}"
         * @readonly
         * @required
         */
        protected List remoteRepositories;
    
        /**
         * Location of the local repository.
         * 
         * @parameter expression="${localRepository}"
         * @readonly
         * @required
         */
        protected ArtifactRepository localRepository;
    
        /**
         * The target pom's artifactId
         * 
         * @parameter expression="${bootstrapArtifactId}"
         * @required
         */
        private String bootstrapArtifactId;
    
        /**
         * The target pom's groupId
         * 
         * @parameter expression="${bootstrapGroupId}"
         * @required
         */
        private String bootstrapGroupId;
    
        /**
         * The target pom's type
         * 
         * @parameter expression="${bootstrapType}"
         * @required
         */
        private String bootstrapType;
    
        /**
         * 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);
    
                artifactResolver.resolve(pomArtifact, this.remoteRepositories,
                    this.localRepository);
            } catch (ArtifactResolutionException e) {
                getLog().error("can't resolve parent pom", e);
            } catch (ArtifactNotFoundException e) {
                getLog().error("can't resolve parent pom", e);
            }
        }
    }
    

    这是一个配置为使用插件的 pom 示例(并下载 aspectjrt 1.6.4 pom):

    <?xml version="1.0" encoding="UTF-8"?>
    <project>
      <modelVersion>4.0.0</modelVersion>
      <groupId>name.seller.rich</groupId>
      <artifactId>bootstrap-test</artifactId>
      <version>1.0.0</version>
        <build>
          <plugins>
            <plugin>
              <groupId>name.seller.rich</groupId>
              <artifactId>maven-bootstrap-plugin</artifactId>
              <executions>
                <execution>
                  <phase>package</phase>
                  <goals>
                    <goal>bootstrap</goal>
                  </goals>
                  <configuration>
                    <bootstrapGroupId>org.aspectj</bootstrapGroupId>
                    <bootstrapArtifactId>aspectjrt</bootstrapArtifactId>
                    <bootstrapVersion>1.6.4</bootstrapVersion>
                    <bootstrapType>pom</bootstrapType>
                  </configuration>
                </execution>
              </executions>
            </plugin>
        </plugins>
      </build>
    </project>
    

    【讨论】:

    • 再次感谢,它运行良好。为下载的文件获取 maven 项目的好方法是什么?
    • @Brian,正如 OP 所说,他们知道在依赖插件中是可能的,但想在他们自己的插件中做到这一点,我已经向他们展示了如何。你知道一种更有效地做到这一点的方法吗?
    • 你知道如何在 Maven 3 API 中使用 Aether 和 Guice 组件来实现吗? Maven 缺少这方面的文档。
    • 附带说明:在属性中同时指定版本会使项目难以(干净地)发布和维护,因为它形成了对构建系统有效“隐藏”的依赖项。使用符号 not 指定版本并在 maven 依赖项中查找版本可能会更好/更安全(这也意味着它必须存在)。例如,这是 maven-antrun-plugin 中使用的方法。请注意,查找必须匹配所有 4 个剩余坐标 (GACT)
    • ArtifactFactory 已弃用,ArtifactResolver 已移至 Aether 包。
    猜你喜欢
    • 2014-02-20
    • 2013-01-11
    • 2020-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    相关资源
    最近更新 更多