我试图了解如何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?


当前回答

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 to build.gradle file and run this task to get the wrapper structure. 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 } and run: gradle wrapper 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. With Gradle 2.4 (or higher) you can set up a wrapper without adding a dedicated task: gradle wrapper --gradle-version 2.3 or gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip All the details can be found here

在Gradle 3.1中,也可以使用distribution-type选项。选项包括binary、all和bin。所有额外包含源代码和文档。当使用IDE时,all也更好,因此编辑器工作得更好。缺点是构建可能会持续更长的时间(需要下载更多的数据,在CI服务器上毫无意义),它将占用更多的空间。

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 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内置任务在4.8中已弃用,请尝试以下内容

wrapper {
   gradleVersion = '2.0' //version required
}

和运行

gradle wrapper

如果你想下载带有源代码和文档的gradle,在gradle-wrapper中配置的默认分发url。财产不能满足你的需要。它是https://services.gradle.org/distributions/gradle-2.10-bin.zip,而不是https://services.gradle.org/distributions/gradle-2.10-all.zip.This完整的url是IDE建议的,如Android Studio。如果你想下载完整的gradle,你可以这样配置包装任务:

task wrapper(type: Wrapper) {
    gradleVersion = '2.13'
    distributionUrl = distributionUrl.replace("bin", "all")
}

从Gradle 2.4开始,你可以使用Gradle包装器——Gradle version X.X来配置一个特定版本的Gradle包装器,而不需要向你的构建中添加任何任务。gradle文件。下次使用包装器时,它将下载适当的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.

这是用来告诉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不要在构建服务器上下载源代码,请评论或提供其他答案。