Building a Java project

What This Guide Covers

Language versions and other build-environment specific information are in 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 Getting Started and build configuration guides first.

Overview

The Travis CI environment contains various versions of OpenJDK, Oracle JDK, Gradle, Maven and Ant.

To use the Java environment, add the following to your .travis.yml:

language: java

Projects Using Maven

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 than install 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.

Projects Using Gradle

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/

Gradle daemon is disabled by default

As recommended by the Gradle team, the Gradle daemon is disabled by default. If you would like to run gradle with daemon, add --daemon to the invocation.

Projects Using Ant

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.

Testing Against Multiple JDKs

To test against multiple JDKs, use the jdk: key in .travis.yml. For example, to test against Oracle JDKs 8 and 9, as well as OpenJDK 7:

jdk:
  - oraclejdk8
  - oraclejdk9
  - openjdk7

Note that testing against multiple Java versions is not supported on OS X. See the OS X Build Environment for more details.

Switching JDKs Within One Job

If your build needs to switch JDKs during a job, you can do so with jdk_switcher use ….

script:
  - jdk_switcher use oraclejdk8
  - # do stuff with Java 8
  - jdk_switcher use openjdk7
  - # do stuff with Java 7

Use of jdk_switcher also updates $JAVA_HOME appropriately.

Updating Oracle JDK to a recent release

Your repository may require a newer release of Oracle JDK than the pre-installed version. (You can consult the list of published Oracle JDK packages.)

The following example will use the latest Oracle JDK 8:

sudo: false
addons:
  apt:
    packages:
      - oracle-java8-installer

Build Matrix

For Java projects, env and jdk can be given as arrays to construct a build matrix.

Examples