Build a Java project
For Language versions and other build-environment specific information visit our reference pages:
The rest of this guide covers configuring Java projects in Travis CI. If you’re new to Travis CI, please read our Onboarding and General Build configuration guides first.
Overview #
The Travis CI environment contains various versions of OpenJDK, Gradle, Maven, and Ant.
To use the Java environment, add the following to your .travis.yml
:
language: java
Maven Projects #
Maven Dependency Management #
Before running the build, Travis CI installs dependencies:
mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
or if your project uses the mvnw
wrapper script:
./mvnw install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
Note that the Travis CI build lifecycle and the Maven build lifecycle use similar terminology for different build phases. For example,
install
in a Travis CI build comes much earlier thaninstall
in the Maven build lifecycle. More details can be found about the Travis Build Lifecycle and the Maven Build Lifecycle.
Maven Default Script Command #
If your project has pom.xml
file in the repository root but no build.gradle
,
Travis CI builds your project with Maven 3:
mvn test -B
If your project also includes the mvnw
wrapper script in the repository root,
Travis CI uses that instead:
./mvnw test -B
The default command does not generate JavaDoc (
-Dmaven.javadoc.skip=true
).
To use a different script
command, customize the build step.
Gradle Projects #
Gradle Dependency Management #
Before running the build, Travis CI installs dependencies:
gradle assemble
or
./gradlew assemble
To use a different install
command, customize the installation step.
Gradle Default Script Command #
If your project contains a build.gradle
file in the repository root, Travis CI
builds your project with Gradle:
gradle check
If your project also includes the gradlew
wrapper script in the repository
root, Travis CI uses that wrapper instead:
./gradlew check
To use a different script
command, customize the build step.
Caching #
A peculiarity of dependency caching in Gradle means that to avoid uploading the
cache after every build you need to add the following lines 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/
Note that if you use Gradle with
sudo
(i.e.sudo ./gradlew assemble
), the caching configuration above will have no effect, since the depencencies will be in/root/.gradle
which thetravis
user account does not have write access to.
Ant Projects #
Ant Dependency Management #
Because there is no single standard way of installing project dependencies with
Ant, you need to specify the exact command to run using install:
key in your
.travis.yml
, for example:
language: java
install: ant deps
Ant Default Script Command #
If Travis CI does not detect Maven or Gradle files it runs Ant:
ant test
To use a different script
command, customize the build step.
Use Ant on Ubuntu Xenial (16.04) #
Unfortunately, ant
currently doesn’t come pre-installed on our Xenial image. You’ll need to install it manually by adding the following recipe to your .travis.yml file:
dist: xenial
language: java
addons:
apt:
packages:
- ant
Note that testing against multiple Java versions is not supported on macOS. See the macOS Build Environment for more details.
The list of available JVMs for different dists are at
- JDKs installed for Focal
- JDKs installed for Bionic
- JDKs installed for Xenial
- JDKs installed for Trusty
- JDKs installed for Precise
Switch JDKs (Java 8 and lower) within one Job #
If your build needs to switch JDKs (Java 8 and below) during a job, you can do so with
jdk_switcher
.
script:
- jdk_switcher use openjdk8
- # do stuff with open Java 8
Use of jdk_switcher
also updates $JAVA_HOME
appropriately.
Use Java 10 and higher #
Take note that
oraclejdk10
is EOL since October 2018 and as such it’s not supported anymore on Travis CI. See https://www.oracle.com/technetwork/java/javase/eol-135779.html.
openjdk
is now the default jdk available on our VMs asinstall-jdk
no longer installsoraclejdk
. Please see this Github Issue for context.
Switch JDKs (to Java 10 and higher) within one Job #
If your build needs to switch JDKs (Java 10 and up) during a job, you can do so with
install-jdk.sh
.
jdk: openjdk10
script:
- jdk_switcher use openjdk10
- # do stuff with OpenJDK 10
- wget https://github.com/sormuras/bach/raw/master/install-jdk.sh
- chmod +x $TRAVIS_BUILD_DIR/install-jdk.sh
- export JAVA_HOME=$HOME/openjdk11
- $TRAVIS_BUILD_DIR/install-jdk.sh -F 11 --target $JAVA_HOME
- # do stuff with open OpenJDK 11
Current JDK providers #
Currently our builds are using Bellsoft and Adoptium JDK providers - they are switched based on the distribution and architecture you are using in your build. Additionally, we’ve added IBM’s Semeru JDK - to use it, instead of using jdk: jdkX or jdk: openjdkX syntax, simply use jdk: semeruX like here:
language: java
jdk: semeru11
Available semeru JDKs for AMD, S390X, PPC64LE and ARM architectures: 8, 11, 16, 17, 18, 19, 20, 21, 22
Examples #
- JRuby
- Riak Java client
- Cucumber JVM
- Symfony 2 Eclipse plugin
- RESThub
- Joni, JRuby’s regular expression implementation
Build Config Reference #
You can find more information on the build config format for Java in our Travis CI Build Config Reference.