当前位置:  开发笔记 > 编程语言 > 正文

如何在插件中下载Maven工件?

如何解决《如何在插件中下载Maven工件?》经验,为你挑选了1个好方法。

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

我希望能够从远程存储库下载该工件并将该文件复制到项目中.我无法弄清楚如何下载工件.

我知道我可以使用依赖插件解决依赖关系,但我需要它在我的插件中发生.我怎样才能做到这一点?



1> Rich Seller..:

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

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

在另一个答案中,我展示了如何做到这一点.在您的情况下,您需要一个具有稍微不同参数的缩减版本来读取groupId,artifactId和version.在下面的插件中,各种组件被声明为plexus组件,以及声明groupId,artifactId,version和packaging类型的属性.

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):



  4.0.0
  name.seller.rich
  bootstrap-test
  1.0.0
    
      
        
          name.seller.rich
          maven-bootstrap-plugin
          
            
              package
              
                bootstrap
              
              
                org.aspectj
                aspectjrt
                1.6.4
                pom
              
            
          
        
    
  


你知道如何在Maven 3 API中使用Aether和Guice组件吗?Maven缺乏这方面的文档.
不推荐使用ArtifactFactory,并将ArtifactResolver移至Aether软件包。
推荐阅读
mylvfamily
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有