2011-02-25 154 views
4

我正在编写一个Maven 2插件,它必须遍历所有项目依赖关系并递归遍历这些依赖关系的所有依赖关系。到现在为止我只设法与此代码来解决直接依赖:如何递归解决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); 

    .... 
} 

我如何可以做同样的递归于是我也找到依赖的依赖等等?

回答

13

A)不要使用 project.getModel().getDependencies(), 使用project.getArtifacts() 代替。这样你就可以自动获得传递依赖关系。要启用:标记您的魔力作为

  • @requiresDependencyResolution compile
  • @requiresDependencyCollection compile

(见Mojo API Specification供参考)。

B)你真的想使用遗留依赖API吗?为什么不是use the new Maven 3 Aether API

+1

A)我已经尝试getArtifacts(),但它总是返回一个空集。所以我只是错过了这个注释。非常好,谢谢! B)目前我必须支持Maven 2.这将很快有望改变。 – kayahr 2011-02-25 13:17:54

+0

亲爱的不知名的downvoter,或许你想指定你不喜欢这个答案? – 2012-02-20 11:59:28

+0

+1这是最后的触摸http://stackoverflow.com/questions/11341783/accessing-classes-in-custom-maven-reporting-plugin – MikePatel 2012-07-06 10:32:25