Centril
Centril

Reputation: 2648

Cache gradle dependencies, Travis CI

I'm trying to cache the dependencies for a private Travis CI repository, does Travis have some mechanism specific for gradle, or do I have to cache specific directories?

.travis.yml:

language: groovy

jdk:
  - openjdk7

env:
- TERM=dumb

before_install:
- cd application
- chmod +x gradlew

script:
- ./gradlew build

Relevant parts of last working build:

Downloading https://services.gradle.org/distributions/gradle-2.1-bin.zip

......................................................................................................................................................................................

Unzipping /home/travis/.gradle/wrapper/dists/gradle-2.1-bin/2pk0g2l49n2sbne636fhtlet6a/gradle-2.1-bin.zip to /home/travis/.gradle/wrapper/dists/gradle-2.1-bin/2pk0g2l49n2sbne636fhtlet6a

Set executable permissions for: /home/travis/.gradle/wrapper/dists/gradle-2.1-bin/2pk0g2l49n2sbne636fhtlet6a/gradle-2.1/bin/gradle

Download https://jcenter.bintray.com/com/mycila/xmltool/xmltool/3.3/xmltool-3.3.pom

...

Would adding:

cache:
  directories:
  - $HOME/.gradle

work? or perhaps:

cache:
  directories:
  - $HOME/.gradle/caches/modules-2/files-2.1

Upvotes: 18

Views: 5885

Answers (6)

user7610
user7610

Reputation: 28811

Add this to your .travis.yml:

before_cache:
  - rm -f  $HOME/.gradle/caches/modules-2/modules-2.lock
  - rm -fr $HOME/.gradle/caches/*/plugin-resolution/
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

It is documented in Travis documentation at https://docs.travis-ci.com/user/languages/java/#projects-using-gradle

Upvotes: 18

musketyr
musketyr

Reputation: 828

As of version 3.5.1 the simplest and most effective way is to just cache the caches/modules-2 and caches/wrapper directory. Caching whole caches directory adds too many files and it causes greater delay. You still need to to delete modules-2.lock file.

before_cache:
  - rm -rf $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
    - $HOME/.gradle/caches/modules-2
    - $HOME/.gradle/wrapper/

Upvotes: 2

Filipe Bezerra de Sousa
Filipe Bezerra de Sousa

Reputation: 3032

You just have to add the lines below into your .travis.yml :

before_cache:
  - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock
cache:
  directories:
    - $HOME/.gradle/caches/
    - $HOME/.gradle/wrapper/

You can obtain more information here.

Upvotes: 2

Sandro Machado
Sandro Machado

Reputation: 10205

I just added the following folders:

- $HOME/.gradle/wrapper
- $HOME/.gradle/native
- $HOME/.gradle/daemon
- $HOME/.gradle/caches/jars-1
- $HOME/.gradle/caches/2.3

Adding the .gradle/caches will create a new cache file every build. Don't forget to change 2.3 to your gradle version.

Upvotes: 3

Zimy
Zimy

Reputation: 165

Probably you should add sudo: false to your .travis.yml, because caching is not available for public repositories. It will prevent you from using sudo, setid, setgid, but it allows caching mechanism!

But I have found that caching $HOME/.gradle/caches is not a very good variant, because the file $HOME/.gradle/caches/modules-2/modules-2.lock is changed every build, so Travis would repack the cache every time, and do full upload of that cache. That is slower for me than downloading all my dependencies. So maybe it would be better specify something else than $HOME/.gradle/caches.

Upvotes: 6

Peter Niederwieser
Peter Niederwieser

Reputation: 123920

You'll have to cache at least ~/.gradle/wrapper and ~/.gradle/caches, but I'd probably start out with ~/.gradle. (If necessary, the location of the latter can be changed by setting the GRADLE_USER_HOME environment variable). When upgrading to a newer Gradle version, the cache structure may change, so it might make sense to invalidate the cache from time to time.

PS: Please don't double-post here and on the Gradle forums (either is fine).

Upvotes: 6

Related Questions