Travis CI Java Tutorial

This is a short tutorial on how to get started with a Java project on GitHub, and enabling continuous integration using Travis CI.

There is a boilerplate repository on GitHub that is ready-to-use by doing a simple clone. This was the main motivation to do this writeup, as it has become one of the most popular references to learn about this.

Why use Travis CI?

Travis CI (or any other CI, for that matter) is a service that will run your test suite every time you push a new commit to GitHub, or whenever you receive a pull request.

There are many advantages[1] of using these kind of services, such as ensuring builds are reproducible on a fresh OS install, to have repeatable testing processes and to automate publishing and deployment of your releases.

An Example Java Project

We will build a simple calculator library in Java, and we will use Maven for building the project.

Create the project model and the source file:

/pom.xml
/src/main/java/io/github/joaomlneto/travis_ci_tutorial_java/SimpleCalculator.java

TODO: compile and show it’s running!

We now have a working calculator! Let’s add some unit tests:

/src/test/java/io/github/joaomlneto/travis_ci_tutorial_java/SimpleCalculatorTest.java

TODO: run tests manually

Enabling Travis CI

Before we proceed, you will also need to authorize your GitHub account and GitHub repository to use Travis CI, which is described in the Travis CI Tutorial, To get started with Travis CI.

We can now add Travis CI to our project by creating the /.travis.yml:

1
language: java

This minimalistic configuration will try to autodetect your repository configuration and use default settings. There are many options that you can explore, such as selecting the operating system, installing required OS packages, setting up docker containers, etc.

In our case, Travis CI will automatically detect we are using Maven through the existence of the pom.xml file and will execute the tests as per the Maven Lifecycle, executing the following commands:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ mvn install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
$ mvn test -B
...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running io.github.joaomlneto.travis_ci_tutorial_java.SimpleCalculatorTest
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.204 sec
Results :
Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.7.201606060606:report (report) @ travis-ci-tutorial-java ---
[INFO] Loading execution data file /home/travis/build/joaomlneto/travis-ci-tutorial-java/target/jacoco.exec
[INFO] Analyzed bundle 'travis-ci-tutorial-java' with 1 classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.358 s
[INFO] Finished at: 2019-03-28T12:26:18Z
[INFO] Final Memory: 22M/255M
[INFO] ------------------------------------------------------------------------
The command "mvn test -B" exited with 0.

We can trigger Travis to run a build either manually on the website or simply by pushing to GitHub.

Sources

[1] DevOps Zone article: 9 Benefits of Continuous Integration