[英]In a multi-module project can Gradle build a plugin as one module and then use that plugin in the same build?
[英]Can a Gradle plugin modify the list of subprojects in a multi-module project?
我將以下的build.gradle
和settings.gradle
組合在一起,以從幾個單模塊項目(例如,一個應用程序及其所有依賴項,或一個共享庫以及所有使用該庫)。
settings.gradle
:
// find all subprojects and include them
rootDir.eachFileRecurse {
if (it.name == "build.gradle") {
def projDir = it.parentFile
if (projDir != rootDir) {
include projDir.name
project(":${projDir.name}").projectDir = projDir
}
}
}
build.gradle:
::
// Make sure we've parsed subproject dependencies
evaluationDependsOnChildren()
// Map of all projects by artifact group and name
def declarationToProject = subprojects.collectEntries { p -> [toDeclaration(p), p] }
// Replace artifact dependencies with subproject dependencies, if possible
subprojects.each { p ->
def changes = [] // defer so we don't get ConcurrentModificationExceptions
p.configurations.each { c ->
c.dependencies.each { d ->
def sub = declarationToProject[[group:d.group, name:d.name]]
if (sub != null) {
changes.add({
c.dependencies.remove(d)
p.dependencies.add(c.name, sub)
})
}
}
}
for (change in changes) {
change()
}
}
這行得通,但很難共享-如果其他人想要做類似的事情,他們必須復制我的*.gradle
文件或剪切並粘貼。
我想做的就是利用此功能並將其封裝在插件中。 build.gradle
部分看起來很容易在插件的apply()
方法中完成,但是子項目列表似乎已經在插件獲得成功之前就已經設定好了。 有什么方法可以使您更早地進入構建過程,例如通過應用到Project
其他東西? 還是我應該辭職給我的插件一個覆蓋settings.gradle
的任務?
解決方案:根據Peter Niederweiser的回答 ,我將上面的代碼移動到了兩個插件中,一個從settings.gradle
調用,另一個從build.gradle
。 在settings.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
apply plugin: 'find-subprojects'
在build.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
evaluationDependsOnChildren()
apply plugin: 'local-dependencies'
請注意,從settings.gradle
調用插件在Gradle 1.11或1.12中不起作用,但在Gradle 2.0中起作用。
您需要在settings.gradle
應用一個插件,我相信最新版本支持該插件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.