这类似于其他问题( like this),但我希望能够使用最新的API执行此操作. maven-dependency-plugin:tree verbose选项已被弃用,并且在最新的(2.5.1)代码中没有做任何事情,所以没有很好的例子说明如何
Aether
实用程序类可以帮助您获取任何Maven工件的所有依赖项列表,例如:
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 );
如果您不在Maven插件中:
File repo = new File("/tmp/local-repository"); MavenProject project = new MavenProject(); project.setRemoteProjectRepositories( Arrays.asList( new RemoteRepository( "maven-central", "default", "http://repo1.maven.org/maven2/" ) ) ); Collection<Artifact> deps = new Aether(project, repo).resolve( new DefaultArtifact("junit", "junit-dep", "", "jar", "4.10"), "runtime" );
您需要的唯一依赖是:
<dependency> <groupId>com.jcabi</groupId> <artifactId>jcabi-aether</artifactId> <version>0.7.5</version> </dependency>