Using Aether in Maven Plugins

Skip to end of metadata
Go to start of metadata
Aether has moved to Eclipse
This wiki is obsolete and no longer maintained. Visit http://www.eclipse.org/aether/support/ instead.









































Apache Maven 3.x uses Aether for repository tasks and Maven plugins that target Maven 3.x can do so as well. To start, you likely want to add the following dependencies to your plugin POM:

<project>
  ...
  <dependencies>
    <dependency>
      <!-- required in all cases -->
      <groupId>org.sonatype.aether</groupId>
      <artifactId>aether-api</artifactId>
      <version>1.8</version>
    </dependency>
    <dependency>
      <!-- might be superfluous depending on your use case -->
      <groupId>org.sonatype.aether</groupId>
      <artifactId>aether-util</artifactId>
      <version>1.8</version>
    </dependency>
    ...
  </dependencies>
  ...
</project>

Note: At runtime, the actual version of aether-api being used is enforced by the Maven core, just like other Maven APIs. So be sure to compile/test your plugin against the version of aether-api that is used by the mininum version of Maven that your plugin wants to support.

Next, in your mojo source, you would need to grab the repository related components and parameters:

import org.sonatype.aether.RepositorySystem;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.repository.RemoteRepository;
...

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 default-value="${repositorySystemSession}"
     * @readonly
     */
    private RepositorySystemSession repoSession;
    
    /**
     * The project's remote repositories to use for the resolution of project dependencies.
     * 
     * @parameter default-value="${project.remoteProjectRepositories}"
     * @readonly
     */
    private List<RemoteRepository> projectRepos;
    
    /**
     * The project's remote repositories to use for the resolution of plugins and their dependencies.
     * 
     * @parameter default-value="${project.remotePluginRepositories}"
     * @readonly
     */
    private List<RemoteRepository> pluginRepos;

    // Your code here
}

Usually, you need only projectRepos or pluginRepos depending on the nature of artifacts your plugin is dealing with, so the other plugin parameter would be superfluous in that case. But in general, the bits shown above should give you all handles that you need to work with Aether from within a Maven plugin.

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Jul 05, 2011

    Anonymous says:

    How do you translate Aether Artifact instance to Maven Core Artifact instance?

    How do you translate Aether Artifact instance to Maven Core Artifact instance?

    1. Jul 05, 2011

      Benjamin Bentmann says:

      Via org.apache.maven.artifact.factory.ArtifactFactory.

      Via org.apache.maven.artifact.factory.ArtifactFactory.

      1. Aug 09, 2011

        Johan Lindquist says:

        In the 3.0.3 version, this class is deprecated - is the intention to replace als...

        In the 3.0.3 version, this class is deprecated - is the intention to replace also the core instances with aether artifacts?

  2. Jul 23, 2011

    Anonymous says:

    Benjamin, Can I make it work with Maven 2? It seems like Maven 2 core uses older...

    Benjamin, Can I make it work with Maven 2? It seems like Maven 2 core uses older version of plexus-container-default, thereby I am getting this exception "Caused by: org.codehaus.plexus.component.repository.exception.ComponentLookupException: Component descriptor cannot be found in the component repository: org.codehaus.plexus.logging.Logger."

    [DEBUG]  The following artifacts were filtered out for plugin: some-plugin:1.0-SNAPSHOT because they're already in the core of Maven:

    org.codehaus.plexus:plexus-container-default:jar:1.5.5:runtime

    org.apache.maven:maven-plugin-api:jar:2.0:runtime

    org.apache.maven:maven-project:jar:2.0.9:runtime

    These will use the artifact files already in the core ClassRealm instead, to allow them to be included in PluginDescriptor.getArtifacts().

    [DEBUG]  The following artifacts were filtered out for plugin: com.jivesoftware.maven:maven-duplicate-finder-plugin:1.0-SNAPSHOT because they're already in the core of Maven:
    org.codehaus.plexus:plexus-container-default:jar:1.5.5:runtime
    org.apache.maven:maven-plugin-api:jar:2.0:runtime
    org.apache.maven:maven-project:jar:2.0.9:runtime
    These will use the artifact files already in the core ClassRealm instead, to allow them to be included in PluginDescriptor.getArtifacts().

    1. Jul 24, 2011

      Benjamin Bentmann says:

      Can I make it work with Maven 2? Frankly, I never tried hard enough myself to k...

      Can I make it work with Maven 2?

      Frankly, I never tried hard enough myself to know the definitive answer to this. There are a couple of challenges one has to solve:

      1. The logger issue you mentioned. I assume this is due to o.s.a.i.i.PlexusLogger using @Requirement Logger instead of the old school extends AbstractLogEnabled. One should be able to workaround this by copying this class to your plugin and changing it appropriately for Plexus 1.x.
      1. Once past the logger issue, the DefaultArtifactDescriptorReader and/or DefaultModelBuilder is likely the next component failing as the maven-model module in Maven 2.x misses some methods. It might be possible to workaround this by using a shaded&relocated copy of maven-model:3.x but this is far from trivial.
      1. All three mojo fields from the above example annotated with @parameter will show up as null when run in Maven 2.x. While a plugin could detect this, creating those objects themselves isn't fun either.

      So unless you are really into hacking, I would consider the answer to be a "no".

      1. Jul 30, 2011

        Anonymous says:

        Thanks for the reply. Looks like my easier option is to convince my lead to swit...

        Thanks for the reply. Looks like my easier option is to convince my lead to switch to Maven 3.

        I appreciate your feedback.

  3. Feb 22, 2012

    Anonymous says:

    I've been searching for a while, and I don't know if it's just me, but it seems ...

    I've been searching for a while, and I don't know if it's just me, but it seems like no one has really explained how to use this new Aether API in Maven plugins. What has happened to the MavenProject object? Do I convert the project's Artifact object into a string and then resolve the artifact? Gogle-ing doesn't give me an answer and this way seems wrong.

  4. Apr 08, 2013

    Anonymous says:

    Is this a dead API? I cannot get this to work for the life of me. It seems that ...

    Is this a dead API? I cannot get this to work for the life of me. It seems that Sonatype took over the repository handling in Maven 3.0 and then dropped the ball on maintaining it. Are there any good articles on the subject. I have found three that look excellent, but none that work. 

    I just want to deploy an artifact through my custom Maven plugin. 

Add Comment