build.gradle中的buildscript和allprojects有什么区别?

时间:2021-08-24 13:31:54

On a multi-project gradle build, can someone tell me what exactly is the difference between the "allprojects" section and the "buildscript" one? Both have a repositories and dependencies task. Is allprojects for my project? What about buildscript?

在多项目gradle构建中,有人可以告诉我“allprojects”部分和“buildscript”部分之间究竟有什么区别?两者都有一个存储库和依赖项任务。是我项目的所有项目吗?那个buildcript怎么样?

buildscript {  
     repositories {
         ...
     }
     dependencies {
         ...
     }
}

and

allprojects(subprojects) { 
     repositories {
         ...
     }
     dependencies {
         ...
     }
}

2 个解决方案

#1


The "buildscript" configuration section is for gradle itself (i.e. changes to how gradle is able to perform the build). So this section will usually include the Android Gradle plugin.

“buildscript”配置部分用于gradle本身(即更改gradle如何执行构建)。所以本节通常会包含Android Gradle插件。

The "allprojects" section is for the modules being built by Gradle.

“allprojects”部分适用于Gradle构建的模块。

Oftentimes the repository section is the same for both, since both will get their dependencies from jcenter usually (or maybe maven central). But the "dependencies" section will be different.

通常情况下,存储库部分对于两者都是相同的,因为两者通常都会从jcenter(或者可能是maven central)获得它们的依赖关系。但“依赖”部分将有所不同。

Usually the "dependencies" section for "allprojects" is empty since the dependencies for each module are unique and will be in the "build.gradle" file within each of the modules. However, if all of the modules shared the same dependencies then they could be listed here.

通常,“allprojects”的“dependencies”部分是空的,因为每个模块的依赖关系是唯一的,并且将位于每个模块中的“build.gradle”文件中。但是,如果所有模块共享相同的依赖项,则可以在此处列出它们。

#2


TL;DR: buildscript helps find plugins, allprojects applies to all projects

TL; DR:buildscript帮助查找插件,所有项目都适用于所有项目


https://docs.gradle.org/current/userguide/userguide_single.html#applyPluginBuildscript says

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.

通过将插件添加到构建脚本类路径然后应用插件,可以将已发布为外部jar文件的二进制插件添加到项目中。

So you need buildscript for gradle to find the plugins, as

因此,您需要使用buildscript for gradle来查找插件

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.

Gradle的核心故意为现实世界的自动化提供很少的东西。所有有用的功能,如编译Java代码的能力,都是由插件添加的。插件添加新任务(例如JavaCompile),域对象(例如SourceSet),约定(例如Java源位于src / main / java)以及从其他插件扩展核心对象和对象。

Concerning allprojects:

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.

Project API提供了一个属性allprojects,它返回一个列表,其中包含当前项目及其下的所有子项目。如果使用闭包调用allprojects,则闭包的语句将委托给与allprojects关联的项目。

#1


The "buildscript" configuration section is for gradle itself (i.e. changes to how gradle is able to perform the build). So this section will usually include the Android Gradle plugin.

“buildscript”配置部分用于gradle本身(即更改gradle如何执行构建)。所以本节通常会包含Android Gradle插件。

The "allprojects" section is for the modules being built by Gradle.

“allprojects”部分适用于Gradle构建的模块。

Oftentimes the repository section is the same for both, since both will get their dependencies from jcenter usually (or maybe maven central). But the "dependencies" section will be different.

通常情况下,存储库部分对于两者都是相同的,因为两者通常都会从jcenter(或者可能是maven central)获得它们的依赖关系。但“依赖”部分将有所不同。

Usually the "dependencies" section for "allprojects" is empty since the dependencies for each module are unique and will be in the "build.gradle" file within each of the modules. However, if all of the modules shared the same dependencies then they could be listed here.

通常,“allprojects”的“dependencies”部分是空的,因为每个模块的依赖关系是唯一的,并且将位于每个模块中的“build.gradle”文件中。但是,如果所有模块共享相同的依赖项,则可以在此处列出它们。

#2


TL;DR: buildscript helps find plugins, allprojects applies to all projects

TL; DR:buildscript帮助查找插件,所有项目都适用于所有项目


https://docs.gradle.org/current/userguide/userguide_single.html#applyPluginBuildscript says

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.

通过将插件添加到构建脚本类路径然后应用插件,可以将已发布为外部jar文件的二进制插件添加到项目中。

So you need buildscript for gradle to find the plugins, as

因此,您需要使用buildscript for gradle来查找插件

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.

Gradle的核心故意为现实世界的自动化提供很少的东西。所有有用的功能,如编译Java代码的能力,都是由插件添加的。插件添加新任务(例如JavaCompile),域对象(例如SourceSet),约定(例如Java源位于src / main / java)以及从其他插件扩展核心对象和对象。

Concerning allprojects:

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.

Project API提供了一个属性allprojects,它返回一个列表,其中包含当前项目及其下的所有子项目。如果使用闭包调用allprojects,则闭包的语句将委托给与allprojects关联的项目。