Java 什么时候使用 gradle.properties 和 settings.gradle?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45387971/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:00:19  来源:igfitidea点击:

When to use gradle.properties vs. settings.gradle?

javagradlebuildbuild-system

提问by ams

A gradle build has three files

一个 gradle 构建有三个文件

  • build.gradlethat defines the build configuration scripts
  • gradle.properties
  • settings.gradle
  • build.gradle定义构建配置脚本
  • gradle.properties
  • settings.gradle

Questions

问题

  • What are differences between settings.gradle& gradle.properties?
  • When should a settings be put in settings.gradlevs. gradle.properties?
  • settings.gradle&之间有什么区别gradle.properties
  • 什么时候应该将设置放入settings.gradlevs. gradle.properties

采纳答案by Lukas K?rfer

settings.gradle

settings.gradle

The settings.gradlefile is a Groovy script, just like the build.gradlefile. Only one settings.gradlescript will be executed in each build (in comparison to multiple build.gradlescripts in multi-project builds). The settings.gradlescript will be executed before any build.gradlescript and even before the Projectinstances are created. Therefore, it is evaluated against a Settingsobject. With this Settingsobject you can add subprojects to your build, modify the parameters from the command line (StartParameter), and access the Gradleobject to register lifecycle handlers. As a consequence, use settings.gradleif your settings are build-related and not necessarily project-related or require logic beforepossible subprojects are included.

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

gradle.properties

gradle.properties

The gradle.propertiesfile is a simple Java Propertiesfile that only gains a special role by being automatically included into the scope of the Projectobject (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.propertiesfiles to these locations:

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

  • directly in the project directory (for project-related values)
  • in the user home .gradledirectory (for user- or environment-related values)
  • 直接在项目目录中(用于与项目相关的值)
  • 在用户主.gradle目录中(用于与用户或环境相关的值)

回答by tkruse

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

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

(root)
  +- settings.gradle       
  +- build.gradle          # optional (commonly present)
  +- gradle.properties     # optional
  +-- buildSrc/            # optional
  |     +- build.gradle    
  |     +-- src/...
  +-- my-gradle-stuff/     # optional
  |     +- 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

设置.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.gradlefile 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 customized.

设置文件也是用groovy编写的,可以自定义子模块查找。

build.gradle

构建.gradle

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

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

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

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

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

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

gradle.properties

gradle.properties

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

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

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

These values can be overridden by a file USER_HOME/.gradle/gradle.properties, and overriden by gradle command line arguments. Also it is possible to set environment variables for the build in this file using systemProp.as prefix.

这些值可以由文件USER_HOME/.gradle/gradle.properties覆盖,并由 gradle 命令行参数覆盖。也可以使用systemProp.as 前缀在此文件中为构建设置环境变量。

Any property in this file can be used in any build.gradle, so some projects also put dependency version or release information in gradle.properties, but that is likely an abuse of this file.

这个文件中的任何属性都可以在任何 build.gradle 中使用,所以一些项目也在 .gradle 中放置了依赖版本或发布信息gradle.properties,但这可能是对该文件的滥用。

my-gradle-stuff/utils.gradle

我的gradle-stuff/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"

other places to put this might be src/gradleor src/build/gradle

其他放置这个的地方可能是src/gradlesrc/build/gradle

buildSrc/...

构建源/...

This folder is special, it is like a separate gradle project in itself. It is built before doing anything else, and can provide function to use in any other gradle file. Because of technical reasons, IDE support for references to this folder work much better than any other way of extracting common code from multiple build.gradlefiles to a separate location.

这个文件夹很特别,它本身就像一个单独的gradle项目。它是在做任何其他事情之前构建的,并且可以提供在任何其他 gradle 文件中使用的功能。由于技术原因,IDE 对此文件夹的引用支持比从多个build.gradle文件中提取公共代码到单独位置的任何其他方式都要好得多。

You can define complex custom build logic in java, groovy or kotlin, instead of writing and deploying a plugin. This is also useful for unit-testing your custom build code, as you can have unit tests. The source folder structure in buildSrccan be adapted like for any java/groovy/kotlin project.

您可以在 java、groovy 或 kotlin 中定义复杂的自定义构建逻辑,而不是编写和部署插件。这对于对自定义构建代码进行单元测试也很有用,因为您可以进行单元测试。中的源文件夹结构buildSrc可以像任何 java/groovy/kotlin 项目一样进行调整。