[英]Can a Gradle plugin modify the list of subprojects in a multi-module project?
I've hacked together combination of build.gradle
and settings.gradle
below for creating an ad-hoc multi-module project out of several single-module projects (eg, an application and all of its dependencies, or a shared library and everything that uses that library). 我将以下的
build.gradle
和settings.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:
: 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()
}
}
This works, but it's hard to share -- if somebody else wants to do something similar they have to copy my *.gradle
files or cut and paste. 这行得通,但很难共享-如果其他人想要做类似的事情,他们必须复制我的
*.gradle
文件或剪切并粘贴。
What I'd like to do is take this functionality and encapsulate it in a plugin. 我想做的就是利用此功能并将其封装在插件中。 The
build.gradle
part looks easy enough to do in the plugin apply()
method, but it seems like the list of subprojects is already set in stone before the plugin gets a chance at it. build.gradle
部分看起来很容易在插件的apply()
方法中完成,但是子项目列表似乎已经在插件获得成功之前就已经设定好了。 Is there any way to get in earlier in the build process, eg by applying to something other than Project
? 有什么方法可以使您更早地进入构建过程,例如通过应用到
Project
其他东西? Or should I resign myself to giving my plugin a task for overwriting settings.gradle
? 还是我应该辞职给我的插件一个覆盖
settings.gradle
的任务?
Solution: Per Peter Niederweiser's answer , I moved the code above into two plugins, one to be called from settings.gradle
and the other to be called from build.gradle
. 解决方案:根据Peter Niederweiser的回答 ,我将上面的代码移动到了两个插件中,一个从
settings.gradle
调用,另一个从build.gradle
。 In settings.gradle:
在
settings.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
apply plugin: 'find-subprojects'
And in build.gradle:
在
build.gradle:
buildscript {
repositories { /* etc... */ }
dependencies { classpath 'my-group:my-plugin-project:1.0-SNAPSHOT' }
}
evaluationDependsOnChildren()
apply plugin: 'local-dependencies'
Note that calling the plugin from settings.gradle
doesn't work in Gradle 1.11 or 1.12 but does work in Gradle 2.0. 请注意,从
settings.gradle
调用插件在Gradle 1.11或1.12中不起作用,但在Gradle 2.0中起作用。
您需要在settings.gradle
应用一个插件,我相信最新版本支持该插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.