2011-02-25 157 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