Search code examples
gradlegradlew

Gradle Wrapper without the jar


I am designing an enterprise wide build system, over a thousand projects in about 21 different languages so far. We are using gradle for this and I have a problem. We are migrating to a gitlab server and we just finished spending weeks building out systems and hooks that explicitly prevent people from committing jar files an other libraries to the source repo. (big long story why, you don't really care anyways).

Now the problem

How do we use the gradle wrapper without a jar file in the gradle/wrapper folder? I tried deleting the folder, exactly as the git hooks will do and the gradlew gives us a class not found error, as I would expect.

Is it possible to use the wrapper without the jar or would the users have to regen the wrapper when they clone the project? Can the gradlew file be setup to automatically regen if the gradle/wrapper folder or the jar is missing?


Solution

  • So basically, when you setup wrapper via

    gradle wrapper
    

    you put:

    • scripts
    • properties
    • gradle wrapper jar file

    into current project directory. The gradle-wrapper.jar comes from your local Gradle distribution. You can check this in the source code

    URL jarFileSource = Wrapper.class.getResource("/gradle-wrapper.jar");
    if (jarFileSource == null) {
        throw new GradleException("Cannot locate wrapper JAR resource.");
    }
    GFileUtils.copyURLToFile(jarFileSource, jarFileDestination);
    

    When you run gradle wrapper you invoke the main class from that jar in order to download and run Gradle. So technically, you could modify the SH and BASH scripts to download the wrapper file from GitHub repository prior Java execution, obviously only if the file isn't present. I actually did it and it worked, however I can't be sure how error prone it may be. I don't know if GitHub won't change URL for instance in the future. I just added the following snippet somewhere in gradlew.sh before the CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar line.

    if [ ! -f "gradle/wrapper/gradle-wrapper.jar" ]
    then
         curl https://raw.githubusercontent.com/gradle/gradle/master/gradle/wrapper/gradle-wrapper.jar -o $APP_HOME/gradle/wrapper/gradle-wrapper.jar
    fi
    

    Anyway, the wrapper file has 56K and it was designed to be as small as possible, to allow people to put it into Git repositories. That being said, I believe you shouldn't be worried about it and just add this particular JAR file to whitelist to your hooks and allow it in the repository.