问题
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:
- I have to specify the location of the remote Maven repository
- I like to fetch an artifact based on it's groupId + artifactId + version
- The API have to provide the current remote version of the artifact (think about SNAPSHOT artifacts which are regularly built so they have a generated part in their versions)
- Return the location of the artifact, a HTTP URL is preferred (i'll fetch it on my own with eg. Apache HTTP Client)
- Optionally retreive the artifact's which are the dependants of the requested one.
回答1:
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:
- Local repo location, as a directory name
- List of repote repositories (
MavenProject#getRemoteRepositories()
) - Maven coordinates of the artifact
- Maven scope to look for
Absolute paths of every dependency found can be obtained as Artifact#getPath()
回答2:
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;
}
回答3:
Aether jest actually pretty simple and elegant way of doing this. It's one of major enhancements in Maven 3 and many was looking for it. Look at this for some initial code to work with. I don't remember a method to get exact URL of the artifact, but other requirements are supported AFAIR.
回答4:
Have you tried using Apache Ivy? It supports fetching maven dependencies.
The documentation for this use case is a little bit sparse, but I found some information on programmatic use of Ivy here.
来源:https://stackoverflow.com/questions/10536221/fetching-maven-artifacts-programmatically