【问题标题】:For a Maven 3 plugin what is the latest way to resolve a artifact对于 Maven 3 插件,解决工件的最新方法是什么
【发布时间】:2015-02-06 08:14:53
【问题描述】:

在 Maven 3.2.5 插件中解决工件的最新方法是什么。 ArtifactResolver 和 ArtifactFactory(depreciated) 在 compat 库中,这意味着存在更新/更好的解决方法,但我找不到任何不使用上述方法的示例、文档或搜索。

谢谢

迈克尔

【问题讨论】:

    标签: maven maven-3 maven-plugin


    【解决方案1】:

    有一个来自 sonatype 的博客就是关于这个的:

    http://blog.sonatype.com/2011/01/how-to-use-aether-in-maven-plugins

    这是来自博客条目的代码(那里清楚地描述了完整的细节):

    public MyMojo extends AbstractMojo {
    
        /**
         * The entry point to Aether, i.e. the component doing all the work.
         */
        @Component
        private RepositorySystem repoSystem;
    
        /**
         * The current repository/network configuration of Maven.
         */
        @Parameter(defaultValue = "${repositorySystemSession}", readonly = true)
        private RepositorySystemSession repoSession;
    
        /**
         * The project's remote repositories to use for the resolution of plugins and their dependencies.
         */
        @Parameter(defaultValue = "${project.remotePluginRepositories}", readonly = true)
        private List<RemoteRepository> remoteRepos;
    
        public void execute() throws MojoExecutionException, MojoFailureException {
            ArtifactRequest request = new ArtifactRequest();
            request.setArtifact(new DefaultArtifact( "org.apache.maven:maven-model:3.0" ) );
            request.setRepositories( remoteRepos );
    
            ArtifactResult result = repoSystem.resolveArtifact( repoSession, request );
        } 
    

    }

    如果需要,您可以使用result.getArtifact() 获取工件并使用result.getArtifact().getFile() 获取工件的文件。

    【讨论】:

    • 我一直在 3.0.5 使用这个示例代码,它运行良好。如果我使用 3.2.5 运行,我会遇到各种类问题,其中之一是 java.lang.NoClassDefFoundError: org.sonatype.aether.artifact.Artifact 我确实在某处读过,不确定在哪里,该 maven 与 eclipse aether 一起运行而不是 sonatype 以太。不确定这对我的依赖项和导入有什么影响。
    • 好的,找到答案了,使用 org.eclipse.aether 依赖:- org.eclipse.aetheraether-api1.0.2.v20150114org.eclipse.aetheraether-util1.0.2.v20150114 但这意味着插件不会在 3.2 以下运行。?这很好。
    • 因此,@DB5 回答了这个问题,但修改为使用 org.eclipse.aether 库。塔
    猜你喜欢
    • 2010-11-08
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2010-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多