2017-08-10 88 views
0

我在一個大型項目(數百個模塊,每個都有測試)上工作,並且想要使用gradle依賴關係構建測試依賴關係圖。如何使用gradle構建測試依賴關係圖

例如,假設我有以下模塊和依賴:

core <----- thing1 <----- thing1a 
    <----- thing2 

如果我運行gradle thing1:dependencies它會告訴我,thing1 dependsOn core。相反,我想知道哪些模塊依賴於thing1,因此無論何時更改thing1,我都可以運行thing1和所有相關模塊的測試。在上面的例子中,相關模塊會thing1thing1a

希望有一個簡單的方式gradle這個做到這一點(構建測試依賴圖似乎是一個很常見的事),但我一直沒能找到任何東西呢。

回答

1

使用this gist(我沒寫)爲靈感,在根build.gradle考慮這個問題:

subprojects { subproject -> 
    task dependencyReport { 
    doLast { 
     def target = subproject.name 
     println "-> ${target}" 

     rootProject.childProjects.each { item -> 
     def from = item.value 
     from.configurations 
      .compile 
      .dependencies 
      .matching { it in ProjectDependency } 
      .each { to -> 
       if (to.name == target) { 
       println "-> ${from.name}" 
       } 
      } 
     } 
    } 
    } 
} 

使用項目結構你描述一個例子來看:

$ gradle thing1:dependencyReport 
:thing1:dependencyReport 
-> thing1 
-> thing1a 
+1

謝謝爲你的答案邁克爾!當我把它放到我的項目中時,這不會立即工作,但我懷疑這是因爲我使用bndtools來管理我的類路徑,因此除了「configurations.compile.dependencies」之外,我可能需要使用其他集。我會玩弄它,看看我是否可以用這種方法得到它,並接受你的答案,如果這導致我在正確的方向 –