One of the requirements for a program I am working on is that I need to be able to look through the Maven dependencies of several artifacts in a repository so I can create dependency graphs for them. While it is obvious that Maven and Eclipse Aether can do this (as a huge part of Maven is getting dependencies), I'm having a really tough time figuring out how to do it in a Java program.
Any suggestions?
It seems that Aether can help, according to the documentation . There is even an example that demonstrates how to use Aether to collect the transitive dependencies of an artifact . Combining that with the Maven API example here , I think you can get where you want.
有几种方法可以做到这一点,或者使用像Eclipse这样的IDE……或者您可以使用仅打印到控制台中的maven-dependency-plugin来实现。
mvn dependency:tree
After looking around at various different examples and code, I cobbled together this, which seems to work:
public List<Artifact> fetchArtifactDependencies(final RepositorySystemSession session,
final Artifact artifact,
final DependencyFilter dependencyFilter)
throws RepositoryException {
final CollectRequest collectRequest = new CollectRequest();
collectRequest.setRoot(new Dependency(artifact, "compile"));
collectRequest.addRepository([repository]);
final DependencyNode node = repositorySystem.collectDependencies(session, collectRequest)
.getRoot();
final DependencyRequest dependencyRequest = new DependencyRequest();
dependencyRequest.setRoot(node);
dependencyRequest.setFilter(dependencyFilter);
final DependencyResult dependencyResult = repositorySystem.resolveDependencies(session,
dependencyRequest);
final List<ArtifactResult> artifactResults = dependencyResult.getArtifactResults();
final List<Artifact> results = new ArrayList<>(artifactResults.size());
CollectionUtils.collect(artifactResults, new Transformer<ArtifactResult, Artifact>() {
@Override
public Artifact transform(final ArtifactResult input) {
return input.getArtifact();
}
}, results);
return results;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.