Gradle on Travis CI
This page is a reference for integrating Gradle with Travis CI.
Default Gradle behavior
When Travis CI detects a build.gradle or build.gradle.kts file in the repository root, it automatically applies Gradle-specific defaults for the build lifecycle:
| Phase | Without Wrapper | With Wrapper (gradlew present) |
|---|---|---|
|
|
|
|
|
|
Most projects override one or both phases in .travis.yml to run custom tasks.
Minimal configuration
A .travis.yml that builds a Gradle project with the Wrapper:
language: java
dist: focal
jdk: openjdk17
install: skip
script:
- ./gradlew build
-
language: javaactivates the Java environment and JDK selection. -
dist: focalselects Ubuntu 20.04 (the current default). Other options includejammy(22.04) andnoble(24.04). -
jdkselects the JDK. Common values:openjdk17,openjdk21. IBM Semeru builds are available assemeru11,semeru17, etc. -
install: skipskips the defaultgradle assemblephase — thebuildtask already includes assembly. -
The Gradle Wrapper is detected automatically when
gradlewexists in the repository root.
Testing against multiple JDKs
Use a jdk matrix to test across several Java versions in parallel:
language: java
dist: focal
jdk:
- openjdk17
- openjdk21
install: skip
script:
- ./gradlew build
Travis CI creates one job per JDK entry. Combine with os to add cross-platform coverage:
os:
- linux
- osx
jdk:
- openjdk17
- openjdk21
Caching
Travis CI can persist directories between builds. For Gradle projects, cache the Wrapper distribution and the dependency cache:
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/
The before_cache step removes lock files that should not be persisted.
|
If you run Gradle with |
|
For large dependency trees, the cache archive upload/download time on shared infrastructure may offset the savings. In that case, consider a Gradle Remote Build Cache or a repository manager to proxy external dependencies. |
Publishing Build Scans
Pass --scan in the script phase to publish a Build Scan® for every build:
script:
- ./gradlew build --scan
To auto-accept the terms of use without the --scan flag, configure the Develocity plugin in settings.gradle[.kts].
Travis CI sets CI=true automatically, which can be used to gate publication:
plugins {
id("com.gradle.develocity") version "3.18.1"
}
develocity {
buildScan {
if (System.getenv("CI") != null) {
termsOfUseUrl = "https://gradle.com/help/legal-terms-of-use"
termsOfUseAgree = "yes"
publishing.onlyIf { true }
}
}
}
Useful environment variables
| Variable | Purpose |
|---|---|
|
Always |
|
Branch name for push builds; base branch for pull-request builds. |
|
Pull-request number, or |
|
Trigger type: |
|
Git tag name if the build was triggered by a tag push; empty otherwise. |
|
Absolute path to the checked-out repository on the worker. |
|
The SHA of the commit being built. |
See Default environment variables for the full list.
Build stages and conditional jobs
Travis CI build stages run groups of jobs sequentially, while jobs within a stage run in parallel. Use stages to model pipelines — for example, build first, then deploy only on tagged commits:
jobs:
include:
- stage: build
script: ./gradlew build
- stage: publish
if: tag IS present
script: ./gradlew publish
Managing secrets
Store credentials (publishing tokens, signing keys) as encrypted environment variables in the repository settings or in .travis.yml via the Travis CLI:
travis encrypt PUBLISH_TOKEN=secret --add env.global
Access them in build.gradle[.kts] with System.getenv("PUBLISH_TOKEN") or providers.environmentVariable("PUBLISH_TOKEN").