Why should the Gradle Wrapper be committed to VCS?

后端 未结 6 1823
甜味超标
甜味超标 2021-01-29 23:49

From Gradle\'s documentation: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.wrapper.Wrapper.html

The scripts generated by this

相关标签:
6条回答
  • 2021-01-30 00:12

    I would like to recommend a simple approach.

    In your project's README, document that an installation step is required, namely:

    gradle wrapper --gradle-version 3.3
    

    This works with Gradle 2.4 or higher. This creates a wrapper without requiring a dedicated task to be added to "build.gradle".

    With this option, ignore (do not check in) these files/folders for version control:

    • ./gradle
    • gradlew
    • gradlew.bat

    The key benefit is that you don't have to check-in a downloaded file to source control. It costs one extra step on installation. I think it is worth it.

    0 讨论(0)
  • 2021-01-30 00:12

    What is the "project"?

    Maybe there is a technical definition of this idiom that excludes build scripts. But if we accept this definition, then we must say your "project" is not all the things that you need to versioned!

    But if we say "your project" is everything you have done. Then we can say you must include it and only it into VCS.

    This is very theoretical and maybe not practical in case of our development works. So we change it to "your project is every file (or folder) you need to editing them directly".

    "directly" means "not indirectly" and "indirectly" means by editing another file and then an effect will be reflected into this file.

    So we reach the same that OP said (and is said here):

    I think Generated files should not be in the VCS.

    Yes. Because you haven't created them. So they are not part of "your project" according to the second definition.


    What is the result about these files:

    • build.gradle: Yes. We need to edit it. Our works should be versioned.

      Note: There is no difference where you edit it. Whether in your text editor environment or in Project Structure GUI environment. Anyway you doing it directly!

    • gradle-wrapper.properties: Yes. We need to at least determine Gradle version in this file.

    • gradle-wrapper.jar and gradlew[.bat]: I haven't created or edited them in any of my development works, till this moment! So the answer is "No". If you have done so, the answer is "Yes" about you at that work (and about the same file you edited).


    The important note about the latest case is the user who clones your repo, needs to execute this command on repo's <root-directory> to auto-generate wrapper files:

    > gradle wrapper --gradle-version=$v --distribution-type=$distType
    

    $v and $distType are determined from gradle-wrapper.properties:

    distributionUrl=https\://services.gradle.org/distributions/gradle-{$v}-{$distType}.zip
    

    See https://gradle.org/install/ for more information.

    gradle executable is bin/gradle[.bat] in local distribution. It's not required that local distribution be same as that determined in the repo. After wrapper files created then gradlew[.bat] can download determined Gradle distribution automatically (if not exists locally). Then he/she probably must regenerate wrapper files using new gradle executable (in downloaded distribution) using above instructions.


    Note: In the above instructions, supposed the user has at least one Gradle distribution locally (e.g. ~/.gradle/wrapper/dists/gradle-4.10-bin/bg6py687nqv2mbe6e1hdtk57h/gradle-4.10). It covers almost all real cases. But what happens if the user hasn't any distribution already?

    He/She can download it manually using the URL in .properties file. But if he/she doesn't locate it in the path that the wrapper expected, the wrapper will download it again! The expected path is completely predictable but is out of the subject (see here for the most complex part).

    There are also some easier (but dirty) ways. For example, he/she can copy wrapper files (except .properties file) from any other local/remote repository to his/her repository and then run gradlew on his/her repository. It will automatically download the suitable distribution.

    0 讨论(0)
  • 2021-01-30 00:22

    According to Gradle docs, adding gradle-wrapper.jar to VCS is expected as making Gradle Wrapper available to developers is part of the Gradle approach:

    To make the Wrapper files available to other developers and execution environments you’ll need to check them into version control. All Wrapper files including the JAR file are very small in size. Adding the JAR file to version control is expected. Some organizations do not allow projects to submit binary files to version control. At the moment there are no alternative options to the approach.

    0 讨论(0)
  • 2021-01-30 00:26

    Because the whole point of the Gradle wrapper is to be able, without having ever installed Gradle

    Same argument goes for the JDK, do you want to commit that also? Do you also commit all your dependency libraries?

    The dependencies should be upgraded continuously as new versions are released to get security and other bug fixes, and because if you get to far behind it can be a very time consuming task to get up to date again.

    If the Gradle wrapper is incremented for every new release, and it is committed, the repo will grow very large. The problem is obvious when working with distributed VCS where a clone will download all versions of everything.

    and without even knowing how it works

    Create a build script that downloads the wrapper and uses it to build. Everyone does not need to know how the script works, they need to agree that the project is build by executing it.

    where to download it from, which version

    task wrapper(type: Wrapper) {
      gradleVersion = 'X.X' 
    }
    

    and then

    gradle wrapper
    

    to download the correct version.

    to clone the project from the VCS, to execute the gradlew script it contains, and to build the project without any additional step.

    Solved by the steps above. Downloading the Gradle wrapper is not different from downloading any other dependency. The script could be smart enough to check for any current Gradle wrapper and only download it if there is a new version.

    If the developer has never used Gradle before and maybe doesn't know the project is built with Gradle, then it is more obvious to run a build.sh compared to running gradlew build.

    If all you had was a gradle version number in a build.gradle file, you would need a README explaining everyone that gradle version X must be downloaded from URL Y an installed,

    No, you would not need a README. You could have one, but we are developers and we should automate as much as possible. Creating a script is better.

    and you would have to do it every time the version is incremented.

    If the developers agree that the correct process is to:

    1. Clone repo
    2. Run build script

    Then there upgrading to latest Gradle wrapper is no problem. If the version is incremented since last run, the script could download the new version.

    0 讨论(0)
  • 2021-01-30 00:27

    Old question, fresh answer. If you don't upgrade gradle often (most of us don't), it's better to commit it to VCS. And the main reason for me is to increase the build speed on the CI server. Nowadays, most of the projects are getting built and installed by CI servers, different server instance every time.

    If you don't commit it, CI server will download a jar for every build and it significantly increases a build time. There are other ways to handle this problem, but I find this one the easiest to maintain.

    0 讨论(0)
  • 2021-01-30 00:35

    Because the whole point of the gradle wrapper is to be able, without having ever installed gradle, and without even knowing how it works, where to download it from, which version, to clone the project from the VCS, to execute the gradlew script it contains, and to build the project without any additional step.

    If all you had was a gradle version number in a build.gradle file, you would need a README explaining everyone that gradle version X must be downloaded from URL Y and installed, and you would have to do it every time the version is incremented.

    0 讨论(0)
提交回复
热议问题