6

I'm trying to setup CI for a project that I'm working on, and I'm wondering if we really need to commit the gradlew and/or the gradle.bat files to make it work.

Is there a workaround for this, or committing those files is the only way?

albodelu
  • 7,931
  • 7
  • 41
  • 84
Mauker
  • 11,237
  • 7
  • 58
  • 76
  • it is advised, https://docs.gradle.org/current/userguide/gradle_wrapper.html#sec:wrapper_generation – petey Mar 15 '17 at 18:52
  • Don't know if it is the only way, but it is the way my own repo is set up. – Code-Apprentice Mar 17 '17 at 17:23
  • A friend told me that it might be possible to run it if you have gradle installed on your system and a system variable. But I don't think it'll work for projects with different gradle versions... – Mauker Mar 17 '17 at 17:30
  • @Mauker It is possible and yes, you need to have gradle installed (any version that can create the wrapper). Travic CI fulfills this requirement. – MartinTeeVarga Apr 19 '17 at 12:18

3 Answers3

6

Committing the gradlew script is not mandatory to build a Gradle project in Travis CI.

Probably the best alternative is to use the pre-installed Gradle to install a Gradle wrapper. This is how a simple build.gradle looks like:

apply plugin: 'java'

check.doFirst {
    println "Running gradle v${project.gradle.gradleVersion}"
}

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

If you use this build file with the standard Travis descriptor, it won't work. It will simply use gradle to run your build. However if you add the wrapper task to the descriptor:

language: java
jdk:
  - oraclejdk8
before_install:
  gradle wrapper

Travis will run the wrapper task first and then correctly detect that gradlew is present and use it to run your build.

However this approach has a disadvantage that can break your build. If you use some features in Gradle that were implemented after the pre-installed Gradle version, the gradle wrapper step will fail. Example of such feature is the S3 maven repository, that was introduced in v2.4 I believe.

To prevent this, you can move the wrapper task to a separate build file, let's say wrapper.gradle:

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

And change the .travis.yml file to:

language: java
jdk:
  - oraclejdk8
before_install:
  gradle -b wrapper.gradle wrapper

This should do it. This setup uses the pre-installed Gradle to install the wrapper without changing your main build script.

You can see an example build here and this is the whole GitHub repository.


Note: There is another way. You could use the before_install step to install required Gradle version from a downloadable distribution or maybe using the Debian package system. However that would need sudo privileges. Such virtual machines take long time to start (around 30s?).

Another thing, I mentioned it in a comment on another answer, if you commit the gradlew script, you also need the wrapper jar and properties. The jar is a binary file and putting it in version control is sometimes seen as controversial. So if you use the solution described above, you can also omit committing the gradle folder.

MartinTeeVarga
  • 10,478
  • 12
  • 61
  • 98
2

It's not mandatory because it's already pre-installed for Java and Android projects.

However, it's the recommended way to go because the installed version depended on the date when the virtual machine was created, probably outdated.

You can try it and check the version with gradle --version command.


Update #1

I demonstrated that my answer is correct forking your MaterialSearchView:

Gradle version 2.2.1 is already pre-installed, so Gradle wrapper is not mandatory, but it's the recommend way to go because your project requires version 2.14.1.

script:
  - gradle clean build

I also demonstrate that the selected answer as correct is based on a wrong assumption. Try to use an unnecessary file and break a build doesn't make this file mandatory, only don't use it and remove chmod line.

enter image description here

You can check here required Gradle version for each Gradle plugin version, in your case plugin version 2.2.3 needs Gradle 2.14.1+

Community
  • 1
  • 1
albodelu
  • 7,931
  • 7
  • 41
  • 84
  • 2
    It's important to add that the recommendation is not just about the scripts. The `jar` and `properties` must be committed too. Many people don't like to commit binary files to version control (quite understandably). – MartinTeeVarga Apr 19 '17 at 12:27
-1

Yes, these files are required.

I determined this with a little trial and error. I deleted them from a branch on one of my personal projects and pushed them up to GitHub, and the travis build failed.

If you are curious what the log looks like, check this gist.

As you can see near the bottom, this is the failing error:

chmod: cannot access `gradlew': No such file or directory
The command "chmod +x gradlew" failed and exited with 1 during .

Minor edit: I tried using it with the gradlew file but not the gradlew.bat file. This worked for a simple clean/build, but when Travis tried to start an emulator, it hung for over 10 minutes.

TL;DR use both files.

AdamMc331
  • 16,492
  • 10
  • 71
  • 133
  • As it seems, it might not be mandatory if you are in full control of your CI environment, but as for Travis CI, it seems to be mandatory. Thanks for the test! – Mauker Mar 27 '17 at 22:23
  • No problem. I am testing right now with the gradlew file but without gradlew.bat and will edit my answer shortly with the results. – AdamMc331 Mar 27 '17 at 22:24
  • @Mauker please read my updated answer and fix this for future readers. – albodelu Mar 31 '17 at 21:48
  • @Mauker You are in control of Travis, if you use the `sudo: required`. This will start a whole VM for you (as opposed to a simple container). Then you can do whatever the Debian environment allows you to do. – MartinTeeVarga Apr 19 '17 at 12:23
  • Isn't that quite a security problem? Or will travis really create a sandbox VM just to run my code? – Mauker Apr 19 '17 at 17:08
  • @Mauker yes, with sudo, it's a new VM every time. – MartinTeeVarga Apr 19 '17 at 21:32