Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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.

Thanks

Michael

like image 501
Michael Baylis Avatar asked Sep 29 '22 19:09

Michael Baylis


People also ask

What does Mvn dependency resolve do?

Description: Goal that resolves the project dependencies from the repository. When using this goal while running on Java 9 the module names will be visible as well.

What is the correct syntax for executing a Maven plugin?

Usage of a Maven Plugin xml you can use the shorthand notation to execute the plugin: mvn <prefix>:<goal> , commonly the “prefix” is the artifact ID minus the “-maven-plugin”. For example mvn example:version .

What is pluginManagement in Maven?

From Maven documentation: pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.


1 Answers

There's a blog from sonatype on exactly this:

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.

like image 71
DB5 Avatar answered Oct 06 '22 09:10

DB5