何时使用gradle.properties与settings.gradle?

时间:2022-09-16 11:13:55

A gradle build has three files

gradle构建有三个文件

  • build.gradle that defines the build configuration scripts
  • build.gradle,用于定义构建配置脚本

  • gradle.properties
  • settings.gradle

Questions

  • What are differences between settings.gradle & gradle.properties?
  • settings.gradle和gradle.properties之间有什么区别?

  • When should a settings be put in settings.gradle vs. gradle.properties?
  • 应该何时将设置放入settings.gradle与gradle.properties中?

2 个解决方案

#1


37  

The settings.gradle file is, just like the build.gradle file, a Groovy script. Only one settings.gradle script will be executed in each build (in comparison to the build.gradle script in multi-project builds). It will be executed before any build.gradle and even before the Project instances are created. Therefore, it is evaluated against a Settings object. With this Settings object, you can add subprojects to your build, modify the parameters from the command line (StartParameter) and access the Gradle object to register lifecycle handlers. Use the file, if your settings are build-related and not necessarily project-related or require logic before possible subprojects are included.

settings.gradle文件与build.gradle文件一样,是一个Groovy脚本。每个构建中只会执行一个settings.gradle脚本(与多项目构建中的build.gradle脚本相比)。它将在任何build.gradle之前执行,甚至在创建Project实例之前执行。因此,它将根据Settings对象进行评估。使用此Settings对象,您可以将子项目添加到构建中,从命令行(StartParameter)修改参数并访问Gradle对象以注册生命周期处理程序。如果您的设置与构建相关且不一定与项目相关或在包含可能的子项目之前需要逻辑,请使用该文件。

The gradle.properties file is a simple Java Properties file, that only gains it special role by being automatically included into the scope of the Project object (as so-called project properties). It's a simple key-value store, that only allows string values (so you need to split lists or arrays by yourself). You can put gradle.properties files to these locations:

gradle.properties文件是一个简单的Java Properties文件,它只通过自动包含在Project对象的范围内(作为所谓的项目属性)来获得特殊的角色。它是一个简单的键值存储,只允许字符串值(因此您需要自己拆分列表或数组)。您可以将gradle.properties文件放到这些位置:

  • directly in the project directory (for project-related values)
  • 直接在项目目录中(与项目相关的值)

  • in the user home .gradle directory (for user- or environment-related values)
  • 在用户主目录.gradle目录中(对于用户或环境相关的值)

#2


19  

A multi-module project has one main module and many submodules. It has this layout:

多模块项目有一个主模块和许多子模块。它有这样的布局:

(root)
  +- gradle.properties     # optional
  +- settings.gradle       
  +- build.gradle
  +- buildSrc/             # optional
  +-- ...
  +- gradle/
  |    +- utils.gradle     # optional
  +-- sub-a/
  |     +- build.gradle
  |     +- src/
  +-- sub-b/
        +- build.gradle
        +- src/

submodules can also be located deeper in subfolders, but without modifying code in settings.gradle, their name will include the name of such folders.

子模块也可以位于子文件夹的更深处,但是如果不修改settings.gradle中的代码,它们的名称将包含此类文件夹的名称。

settings.gradle

The main role of settings.gradle is to define all included submodules and to mark the directory root of a tree of modules, so you can only have one settings.gradle file in a multi-module project.

settings.gradle的主要作用是定义所有包含的子模块并标记模块树的目录根目录,因此在多模块项目中只能有一个settings.gradle文件。

rootProject.name = 'project-x'

include 'sub-a', 'sub-b'

The settings file is also written in groovy, and submodule lookup can be adapted alot.

设置文件也是用groovy编写的,子模块查找可以很多调整。

gradle.properties

This is optional, it's main purpose is to provide startup options to use for running gradle itself, e.g.

这是可选的,它的主要目的是提供用于运行gradle本身的启动选项,例如,

org.gradle.jvmargs=-Dfile.encoding=UTF-8 ...
org.gradle.configureondemand=true

build.gradle

There is one such file per module, it contains the build logic for this module.

每个模块有一个这样的文件,它包含该模块的构建逻辑。

In the build.gradle file of the main module, you can use allprojects {} or subprojects {} to define settings for all other modules.

在主模块的build.gradle文件中,您可以使用allprojects {}或子项目{}来定义所有其他模块的设置。

In the build.gradle file of the submodules, you can use compile project(':sub-a') to make one submodule depend on the other.

在子模块的build.gradle文件中,您可以使用编译项目(':sub-a')使一个子模块依赖于另一个子模块。

gradle/utils.gradle

(Any name of folder or file is possible.) You can define additional custom gradle files to reuse definitions, and include them in other gradle files via

(可以使用文件夹或文件的任何名称。)您可以定义其他自定义gradle文件以重用定义,并将它们包含在其他gradle文件中

apply from: "$rootDir/gradle/utils.gradle"

buildSrc/...

This folder is special, it is like a separate gradle project in itself. It is build before doing anything else, and can provide function to use in any other gradle file. You can define complex custom build logic in java or groovy, instead of writing and deploying a plugin. This is useful for unit-testing your custom build code.

这个文件夹很特别,就像一个单独的gradle项目本身。它是在做任何其他事情之前构建的,并且可以提供在任何其他gradle文件中使用的函数。您可以在java或groovy中定义复杂的自定义构建逻辑,而不是编写和部署插件。这对于自定义构建代码的单元测试非常有用。

#1


37  

The settings.gradle file is, just like the build.gradle file, a Groovy script. Only one settings.gradle script will be executed in each build (in comparison to the build.gradle script in multi-project builds). It will be executed before any build.gradle and even before the Project instances are created. Therefore, it is evaluated against a Settings object. With this Settings object, you can add subprojects to your build, modify the parameters from the command line (StartParameter) and access the Gradle object to register lifecycle handlers. Use the file, if your settings are build-related and not necessarily project-related or require logic before possible subprojects are included.

settings.gradle文件与build.gradle文件一样,是一个Groovy脚本。每个构建中只会执行一个settings.gradle脚本(与多项目构建中的build.gradle脚本相比)。它将在任何build.gradle之前执行,甚至在创建Project实例之前执行。因此,它将根据Settings对象进行评估。使用此Settings对象,您可以将子项目添加到构建中,从命令行(StartParameter)修改参数并访问Gradle对象以注册生命周期处理程序。如果您的设置与构建相关且不一定与项目相关或在包含可能的子项目之前需要逻辑,请使用该文件。

The gradle.properties file is a simple Java Properties file, that only gains it special role by being automatically included into the scope of the Project object (as so-called project properties). It's a simple key-value store, that only allows string values (so you need to split lists or arrays by yourself). You can put gradle.properties files to these locations:

gradle.properties文件是一个简单的Java Properties文件,它只通过自动包含在Project对象的范围内(作为所谓的项目属性)来获得特殊的角色。它是一个简单的键值存储,只允许字符串值(因此您需要自己拆分列表或数组)。您可以将gradle.properties文件放到这些位置:

  • directly in the project directory (for project-related values)
  • 直接在项目目录中(与项目相关的值)

  • in the user home .gradle directory (for user- or environment-related values)
  • 在用户主目录.gradle目录中(对于用户或环境相关的值)

#2


19  

A multi-module project has one main module and many submodules. It has this layout:

多模块项目有一个主模块和许多子模块。它有这样的布局:

(root)
  +- gradle.properties     # optional
  +- settings.gradle       
  +- build.gradle
  +- buildSrc/             # optional
  +-- ...
  +- gradle/
  |    +- utils.gradle     # optional
  +-- sub-a/
  |     +- build.gradle
  |     +- src/
  +-- sub-b/
        +- build.gradle
        +- src/

submodules can also be located deeper in subfolders, but without modifying code in settings.gradle, their name will include the name of such folders.

子模块也可以位于子文件夹的更深处,但是如果不修改settings.gradle中的代码,它们的名称将包含此类文件夹的名称。

settings.gradle

The main role of settings.gradle is to define all included submodules and to mark the directory root of a tree of modules, so you can only have one settings.gradle file in a multi-module project.

settings.gradle的主要作用是定义所有包含的子模块并标记模块树的目录根目录,因此在多模块项目中只能有一个settings.gradle文件。

rootProject.name = 'project-x'

include 'sub-a', 'sub-b'

The settings file is also written in groovy, and submodule lookup can be adapted alot.

设置文件也是用groovy编写的,子模块查找可以很多调整。

gradle.properties

This is optional, it's main purpose is to provide startup options to use for running gradle itself, e.g.

这是可选的,它的主要目的是提供用于运行gradle本身的启动选项,例如,

org.gradle.jvmargs=-Dfile.encoding=UTF-8 ...
org.gradle.configureondemand=true

build.gradle

There is one such file per module, it contains the build logic for this module.

每个模块有一个这样的文件,它包含该模块的构建逻辑。

In the build.gradle file of the main module, you can use allprojects {} or subprojects {} to define settings for all other modules.

在主模块的build.gradle文件中,您可以使用allprojects {}或子项目{}来定义所有其他模块的设置。

In the build.gradle file of the submodules, you can use compile project(':sub-a') to make one submodule depend on the other.

在子模块的build.gradle文件中,您可以使用编译项目(':sub-a')使一个子模块依赖于另一个子模块。

gradle/utils.gradle

(Any name of folder or file is possible.) You can define additional custom gradle files to reuse definitions, and include them in other gradle files via

(可以使用文件夹或文件的任何名称。)您可以定义其他自定义gradle文件以重用定义,并将它们包含在其他gradle文件中

apply from: "$rootDir/gradle/utils.gradle"

buildSrc/...

This folder is special, it is like a separate gradle project in itself. It is build before doing anything else, and can provide function to use in any other gradle file. You can define complex custom build logic in java or groovy, instead of writing and deploying a plugin. This is useful for unit-testing your custom build code.

这个文件夹很特别,就像一个单独的gradle项目本身。它是在做任何其他事情之前构建的,并且可以提供在任何其他gradle文件中使用的函数。您可以在java或groovy中定义复杂的自定义构建逻辑,而不是编写和部署插件。这对于自定义构建代码的单元测试非常有用。