如何递归地解决Maven 2插件中的依赖项

kay*_*ahr 4 maven-2 maven-plugin maven

我正在编写一个Maven 2插件,它必须遍历所有项目依赖项,并递归地覆盖这些依赖项的所有依赖项.到目前为止,我只使用此代码设法解决了直接依赖关系:

for (Dependency dependency : this.project.getModel().getDependencies())
{
    Artifact artifact = this.artifactFactory.createArtifact(
        dependency.getGroupId(),
        dependency.getArtifactId(),
        dependency.getVersion(),
        dependency.getScope(),
        dependency.getType());
    this.artifactResolver.resolve(
         artifact,
         this.remoteRepositories,
         this.localRepository);

    ....
}
Run Code Online (Sandbox Code Playgroud)

我怎么能递归地做同样的事情所以我也找到了依赖关系的依赖性等等?

Sea*_*oyd 13

A)请勿使用 project.getModel().getDependencies(),请project.getArtifacts() 改用.这样你就可以自动获得传递依赖.要启用它:将你的魔力标记为

  • @requiresDependencyResolution compile 要么
  • @requiresDependencyCollection compile

(有关参考,请参阅Mojo API规范).

B)您真的想使用遗留依赖API吗?为什么不使用新的Maven 3 Aether API