I'm looking for a Java API which can be used to retreive Maven artifacts from a remote repository. I've found Eclipse Ather so far but it looks over complicated for my needs so i'm seeking for something more simple.
What i need is:
jcabi-aether may help you (I'm a developer). It's a simple wrapper around Aether, that lets you find all transitive dependencies of a Maven artifact:
File repo = this.session.getLocalRepository().getBasedir();
Collection<Artifact> deps = new Aether(this.getProject(), repo).resolve(
new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"),
JavaScopes.RUNTIME
);
Thus, all you need to provide as an input is:
MavenProject#getRemoteRepositories()
)Absolute paths of every dependency found can be obtained as Artifact#getPath()
public List<Artifact> findDependencies(Artifact artifact) throws DependencyCollectionException {
CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot( new Dependency(artifact, "" ) );
collectRequest.addRepository(repository);
final MavenServiceLocator locator = new MavenServiceLocator();
locator.addService( RepositoryConnectorFactory.class, FileRepositoryConnectorFactory.class );
locator.addService( RepositoryConnectorFactory.class, WagonRepositoryConnectorFactory.class );
locator.setServices( WagonProvider.class, new WagonProvider() {
public Wagon lookup(String roleHint) throws Exception {
if (Arrays.asList("http", "https").contains(roleHint)) {
return new LightweightHttpWagon();
}
return null;
}
public void release(Wagon wagon) {
}
});
final RepositorySystem system = locator.getService(RepositorySystem.class);
MavenRepositorySystemSession session = new MavenRepositorySystemSession();
session.setLocalRepositoryManager( system.newLocalRepositoryManager(localRepository) );
session.setTransferListener( new LoggingTransferListener() );
session.setRepositoryListener( new LoggingRepositoryListener() );
final List<Artifact> artifacts = new ArrayList<Artifact>();
system.collectDependencies(session, collectRequest).getRoot().accept( new DependencyVisitor() {
public boolean visitEnter(DependencyNode dependencyNode) {
artifacts.add(dependencyNode.getDependency().getArtifact());
return true;
}
public boolean visitLeave(DependencyNode dependencyNode) {
return true;
}
});
return artifacts;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With