出处gradle doc 5.4.1
If you have a multi-project build, you probably want to apply plugins to some or all of the subprojects in your build, but not to the root
or master
project. The default behavior of the plugins {}
block is to immediately resolve
and apply
the plugins. But, you can use the apply false
syntax to tell Gradle not to apply the plugin to the current project and then use apply plugin: «plugin id»
in the subprojects
block or use the plugins {}
block in sub projects build scripts
如里你有一个多模块项目build, 你可能需要将应用插件到一些或所有build中的子项目, 而不是 root 或 master 项目. 使用plugins{}模块会默认立刻resolve以及apply这些插件。但是,你可以使用 apply false 语法来阻止插件在当前模块里运行,然后运用 apply plugin: 《plugin id》在subproject 块里或直接在子模块里使用plugins{}块
示例:
settings.gradle
include 'helloA'
include 'helloB'
include 'goodbyeC'
build.gradle
plugins {
id 'org.gradle.sample.hello' version '1.0.0' apply false
id 'org.gradle.sample.goodbye' version '1.0.0' apply false
}
subprojects {
if (name.startsWith('hello')) {
apply plugin: 'org.gradle.sample.hello'
}
}
也可以 goodbyeC/build.gradle
plugins {
id 'org.gradle.sample.goodbye'
}