Aether adapter for Maven plugins
Aether is a dependency management toolkit for Maven repositories. It is very convenient to use Aether in your Maven plugins, when it's necessary to find a location of certain artifact or find out what transitive dependencies it contains. This jcabi-aether module contains Aether class, an adapter between your plugin and Aether.
This is how you find out the location of a junit:junit-dep:4.10 artifact:
import com.jcabi.aether.Aether;
import java.io.File;
import org.apache.maven.plugin.AbstractMojo;
import org.sonatype.aether.RepositorySystemSession;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.util.artifact.DefaultArtifact;
import org.sonatype.aether.util.artifact.JavaScopes;
public class MyMojo extends AbstractMojo {
/**
* @parameter default-value="${repositorySystemSession}"
* @readonly
*/
private RepositorySystemSession session;
@Override
public void execute() {
File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.COMPILE
);
// Now you have a full set of artifacts that include junit-dep.jar
// and all its dependencies in "runtime" scope. The first
// element in the collection is junit-dep.jar itself. You can use
// Artifact#getFile() method to get its absolute path
}
}Otherwise, if you're outside of Maven infrastructure:
import com.jcabi.aether.Aether;
import java.io.File;
import java.util.Arrays;
import org.apache.maven.project.MavenProject;
import org.sonatype.aether.artifact.Artifact;
import org.sonatype.aether.repository.RemoteRepository;
import org.sonatype.aether.util.artifact.DefaultArtifact;
public class Main {
public static void main(String[] args) {
File local = new File("/tmp/local-repository");
Collection<RemoteRepository> remotes = Arrays.asList(
new RemoteRepository(
"maven-central",
"default",
"http://repo1.maven.org/maven2/"
)
);
Collection<Artifact> deps = new Aether(remotes, local).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
"runtime"
);
}
}More examples you can find in AetherTest.
Classpath can help you to collect and filter dependencies in a Maven project.
The only dependency you need (you can also download jcabi-aether-0.10.1.jar and add it to the classpath):
<dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-aether</artifactId> <version>0.10.1</version> </dependency>
Cutting Edge Version
If you want to use current version of the product, you can do it with this configuration in your pom.xml:
<repositories>
<repository>
<id>oss.sonatype.org</id>
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.jcabi</groupId>
<artifactId>jcabi-aether</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>