我试图了解如何Gradle包装工作。在许多源代码回购中,我看到了以下结构:
projectRoot/
src/
build.gradle
gradle.properties
settings.gradle
gradlew
gradlew.bat
gradle/
wrapper/
gradle-wrapper.jar
gradle-wrapper.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 and gradle-wrapper.properties)?
Some times I see other *.gradle files inside the project's gradle 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 inside gradle.properties?
这是用来告诉Gradle升级包装器的命令,这样它就会抓取包含源代码的Gradle jar(只是Gradle,不是库)的分发版本:
./gradlew wrapper --gradle-version <version> --distribution-type all
这现在可以使用Gradle构建本身来完成,改变你的根构建。Gradle包含:
tasks.named('wrapper') {
distributionType = Wrapper.DistributionType.ALL
gradleVersion = '7.5'
}
现在升级gradle包装器:只需将代码更改为新版本,并在没有任何参数的情况下运行包装器任务。
用“all”指定分发类型将确保Gradle为自己下载可以被您的开发环境使用的源文件。
优点:
ide可以立即访问Gradle的源代码。例如,Intellij IDEA不会提示您更新构建脚本以包括源发行版(因为这个命令已经做到了这一点)
缺点:
更长的/更大的构建过程,因为它下载Gradle源代码。这是在构建或CI服务器上浪费时间/空间,因为源代码是不必要的(110MB vs 150MB对于Gradle 7.4)。
如果你知道任何命令行选项告诉Gradle不要在构建服务器上下载源代码,请评论或提供其他答案。
You will generate them once, but update them if you need a new feature or something from a plugin which in turn needs a newer gradle version.
Easiest way to update:
as of Gradle 2.2 you can just download and extract the complete or binary Gradle distribution, and run:
$ <pathToExpandedZip>/bin/gradle wrapper
No need to define a task, though you probably need some kind of build.gradle file.
This will update or create the gradlew and gradlew.bat wrapper as well as gradle/wrapper/gradle-wrapper.properties and the gradle-wrapper.jar to provide the current version of gradle, wrapped.
Those are all part of the wrapper.
Some build.gradle files reference other files or files in subdirectories which are sub projects or modules. It gets a bit complicated, but if you have one project you basically need the one file.
settings.gradle handles project, module and other kinds of names and settings, gradle.properties configures resusable variables for your gradle files if you like and you feel they would be clearer that way.