简体   繁体   English

对于Maven 3插件,解决工件的最新方法是什么

[英]For a Maven 3 plugin what is the latest way to resolve a artifact

What is the latest way of resolving an Artifact within a Maven 3.2.5 plugin. 什么是在Maven 3.2.5插件中解析工件的最新方法。 ArtifactResolver and ArtifactFactory(depreciated) are in the compat library which implies that there is a newer/better way of resolution, but I can not find any examples, docs or searches that do not use the above. ArtifactResolver和ArtifactFactory(折旧)位于compat库中,这意味着有更新/更好的解决方法,但我找不到任何不使用上述内容的示例,文档或搜索。

Thanks 谢谢

Michael 迈克尔

There's a blog from sonatype on exactly this: 这是一个来自sonatype的博客:

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

This is the code from the blog entry (full details are obviously described there): 这是博客条目中的代码(显然有详细描述):

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 );
    } 

} }

You can then use result.getArtifact() to get the artifact and result.getArtifact().getFile() to get the file of the artifact if you need it. 然后,您可以使用result.getArtifact()来获取工件和result.getArtifact().getFile()以在需要时获取工件的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM