如何/何时生成 Gradle 包装器文件? [英] How/when to generate Gradle wrapper files?
问题描述
我正在尝试了解 Gradle Wrapper 的工作原理.在许多源代码库中,我看到以下结构:
I am trying to understand how the Gradle Wrapper works. In many source repos, I see the following structure:
projectRoot/
src/
build.gradle
gradle.properties
settings.gradle
gradlew
gradlew.bat
gradle/
wrapper/
gradle-wrapper.jar
gradle-wrapper.properties
我的问题:
- 如何/何时生成
gradlew
/gradlew.bat
?您是否应该在第一次创建项目时只生成一次,每次提交/推送更改时都会生成它们吗?它们是如何生成的? - 同样的问题,但对于
gradle/wrapper/*
文件(gradle-wrapper.jar
和gradle-wrapper.properties
)? - 有时我会在项目的
gradle
目录中看到其他*.gradle
文件.这些额外的 Gradle 文件是什么?它们代表/做什么?自定义插件? settings.gradle
中的属性与gradle.properties
中应该定义的属性有何不同?
- How/when does one generate
gradlew
/gradlew.bat
? Are you supposed to generate them only one time when the project is first created, do you generate them every time you commit/push changes? And how are they generated? - Same question above, but for the
gradle/wrapper/*
files (gradle-wrapper.jar
andgradle-wrapper.properties
)? - Some times I see other
*.gradle
files inside the project'sgradle
directory. What are these additional Gradle files and what do they represent/do? Custom plugins? - What is the difference in properties that go into
settings.gradle
vs what should be defined insidegradle.properties
?
推荐答案
您生成一次,当您想更改项目中使用的 Gradle 版本时再生成一次.没有必要如此频繁地生成.这里是文档.只需将
wrapper
任务添加到build.gradle
文件并运行此任务即可获取包装器结构.
You generate it once, and again when you'd like to change the version of Gradle you use in the project. There's no need to generate is so often. Here are the docs. Just add
wrapper
task tobuild.gradle
file and run this task to get the wrapper structure.
请注意,您需要安装Gradle 才能生成包装器.管理 g-ecosystem 工件的好工具是 SDKMAN!.要生成 gradle 包装器,请将以下代码添加到 build.gradle
文件中:
Mind that you need to have Gradle installed to generate a wrapper. Great tool for managing g-ecosystem artifacts is SDKMAN!. To generate a gradle wrapper, add the following piece of code to build.gradle
file:
task wrapper(type: Wrapper) {
gradleVersion = '2.0' //version required
}
并运行:
gradle wrapper
任务.将生成的文件添加到 SCM(例如 git),从现在开始所有开发人员在使用 Gradle Wrapper 时都将拥有相同版本的 Gradle.
task. Add the resulting files to SCM (e.g. git) and from now all developers will have the same version of Gradle when using Gradle Wrapper.
使用 Gradle 2.4(或更高版本),您无需添加专用任务即可设置包装器:
With Gradle 2.4 (or higher) you can set up a wrapper without adding a dedicated task:
gradle wrapper --gradle-version 2.3
或
gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip
所有细节都可以找到这里
从 Gradle 3.1
也可以使用 --distribution-type
选项.选项是二进制和all 和bin.all 还包含源代码和文档.all 在使用 IDE 时也更好,因此编辑器效果更好.缺点是构建可能会持续更长时间(需要下载更多数据,在 CI 服务器上毫无意义)并且会占用更多空间.
From Gradle 3.1
--distribution-type
option can be also used. The options are binary and all and bin. all additionally contains source code and documentation. all is also better when IDE is used, so the editor works better. Drawback is the build may last longer (need to download more data, pointless on CI server) and it will take more space.
这些是 Gradle Wrapper 文件.您需要生成一次(针对特定版本)并添加到版本控制中.如果您需要更改 Gradle Wrapper 的版本,请在
build.gradle
中更改版本,请参阅 (1.) 并重新生成文件.
These are Gradle Wrapper files. You need to generate them once (for a particular version) and add to version control. If you need to change the version of Gradle Wrapper, change the version in
build.gradle
see (1.) and regenerate the files.
举个详细的例子.此类文件可能有多种用途:多模块项目、职责分离、稍微修改脚本等.
Give a detailed example. Such file may have multiple purposes: multi-module project, responsibility separation, slightly modified script, etc.
settings.gradle
负责项目的结构(模块、名称等),而 gradle.properties
用于项目和 Gradle 的外部细节(版本、命令行参数-XX
、属性等)
settings.gradle
is responsible rather for structure of the project (modules, names, etc), while, gradle.properties
is used for project's and Gradle's external details (version, command line arguments -XX
, properties etc.)
这篇关于如何/何时生成 Gradle 包装器文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!