build.gradle 中的 buildscript 和 allprojects 有什么区别?
What's the difference between buildscript and allprojects in build.gradle?
在多项目 gradle 构建中,有人能告诉我 "allprojects" 部分和 "buildscript" 部分之间到底有什么区别吗?两者都有 repositories
和 dependencies
任务。 allprojects
适合我的项目吗? buildscript
呢?
buildscript {
repositories {
...
}
dependencies {
...
}
}
和
allprojects(subprojects) {
repositories {
...
}
dependencies {
...
}
}
“buildscript
”配置部分是针对 gradle 本身的(即更改 gradle 能够执行构建的方式)。所以这部分通常会包含 Android Gradle 插件。
“allprojects
”部分用于 Gradle 构建的模块。
通常两者的存储库部分是相同的,因为两者通常都会从 jcenter(或者可能是 maven central)获取它们的依赖项。但是 "dependencies" 部分会有所不同。
通常 "allprojects" 的 "dependencies" 部分是空的,因为每个模块的依赖项都是唯一的,并且将在每个模块的 "build.gradle" 文件中。但是,如果所有模块共享相同的依赖项,那么它们可以列在此处。
TL;DR: buildscript
帮助查找插件,allprojects
适用于所有项目
https://docs.gradle.org/current/userguide/userguide_single.html#applyPluginBuildscript 说
Binary plugins that have been published as external jar files can be added to a project by adding the plugin to the build script classpath and then applying the plugin.
因此 gradle 需要 buildscript
才能找到插件,as
Gradle at its core intentionally provides very little for real world automation. All of the useful features, like the ability to compile Java code, are added by plugins. Plugins add new tasks (e.g. JavaCompile), domain objects (e.g. SourceSet), conventions (e.g. Java source is located at src/main/java) as well as extending core objects and objects from other plugins.
The Project API provides a property allprojects
which returns a list with the current project and all its subprojects underneath it. If you call allprojects
with a closure, the statements of the closure are delegated to the projects associated with allprojects
.
在多项目 gradle 构建中,有人能告诉我 "allprojects" 部分和 "buildscript" 部分之间到底有什么区别吗?两者都有 repositories
和 dependencies
任务。 allprojects
适合我的项目吗? buildscript
呢?
buildscript {
repositories {
...
}
dependencies {
...
}
}
和
allprojects(subprojects) {
repositories {
...
}
dependencies {
...
}
}
“buildscript
”配置部分是针对 gradle 本身的(即更改 gradle 能够执行构建的方式)。所以这部分通常会包含 Android Gradle 插件。
“allprojects
”部分用于 Gradle 构建的模块。
通常两者的存储库部分是相同的,因为两者通常都会从 jcenter(或者可能是 maven central)获取它们的依赖项。但是 "dependencies" 部分会有所不同。
通常 "allprojects" 的 "dependencies" 部分是空的,因为每个模块的依赖项都是唯一的,并且将在每个模块的 "build.gradle" 文件中。但是,如果所有模块共享相同的依赖项,那么它们可以列在此处。
TL;DR: buildscript
帮助查找插件,allprojects
适用于所有项目
https://docs.gradle.org/current/userguide/userguide_single.html#applyPluginBuildscript 说
Binary plugins that have been published as external jar files can be added to a project by adding the plugin to the build script classpath and then applying the plugin.
因此 gradle 需要 buildscript
才能找到插件,as
Gradle at its core intentionally provides very little for real world automation. All of the useful features, like the ability to compile Java code, are added by plugins. Plugins add new tasks (e.g. JavaCompile), domain objects (e.g. SourceSet), conventions (e.g. Java source is located at src/main/java) as well as extending core objects and objects from other plugins.
The Project API provides a property
allprojects
which returns a list with the current project and all its subprojects underneath it. If you callallprojects
with a closure, the statements of the closure are delegated to the projects associated withallprojects
.