Travis CI Tutorial: Java Projects
Travis CI Tutorial: Java Projects
Check out how the Travis CI stack can be applied to your Java projects. The effects are far-reaching, ranging from better build speed to testing.
Join the DZone community and get the full member experience.Join For Free
as a consultant, i’ve done a lot of java projects in different “enterprise” environments. in general, the continuous integration stack — when there is one — is comprised of:
- github enterprise or atlassian stash for source version control.
- jenkins as the ci server, sometimes but rarely atlassian bamboo.
- maven for the build tool.
- jacoco for code coverage.
- artifactory as the artifacts repository — once i had nexus.
recently, i started to develop kaadin , a kotlin- dsl to design vaadin applications. i naturally hosted it on github, and wanted the same features as for my real-life projects above. this post describes how to achieve all the desired features using a whole new stack that might not be familiar to enterprise java developers.
github was a perfect match. then i went on to search for a jenkins cloud provider to run my builds… to no avail. this wasn’t such a surprise, as i already searched for that last year for a course on continuous integration without any success. i definitely could have installed my own instance on any iaas platform, but it always pays to be idiomatic. there are plenty of java projects hosted on github. they mostly use travis ci , so i went down this road.
travis ci registration
registering is as easy as signing in using github credentials, accepting all conditions, and setting the project(s) that need to be built.
the project configuration is read from a
.travis.yml file at its root. historically, the platform was aimed at ruby, but nowadays different kind of projects in different languages can be built. the most important configuration part is to define the language. since mine is a java project, the second most important is which jdk to use:
language: java jdk: oraclejdk8
from that point on, every push on the github repository will trigger the build - including branches.
if a pom exists at the root of the project, it’s automatically detected and maven (or the maven wrapper) will be used as the build tool in that case. it’s a good idea to use the wrapper for it sets the maven version. barring that, there’s no hint of the version used.
as written above, travis ci was originally designed for ruby projects. in ruby, dependencies are installed system-wide, not per project as for maven. hence, the lifecycle is composed of:
- an install dependencies phase, translated by default into
./mvnw install -dskiptests=true -dmaven.javadoc.skip=true -b -v
- a build phase, explicitly defined by each project
this two-phase mapping is not relevant for maven projects as maven uses a lazy approach: if dependencies are not in the local repository, they will be downloaded when required. for a more idiomatic management, the first phase can be bypassed, while the second one just needs to be set:
install: true script: ./mvnw clean install
improve build speed
in order to speed up future builds, it’s a good idea to keep the maven local repository between different runs, as it would be the case on jenkins or a local machine. the following configuration achieves just that:
cache: directories: - $home/.m2
tests and failing the build
as for standard maven builds, phases are run sequentially, so calling
install will first use
compile and then
test . a single failing test, and the build will fail. a report is sent by email when it happens.
most open source projects display their build status badge on their homepage to build trust with their users. this badge is provided by travis ci, it just needs to be hot-linked, as seen below:
travis ci doesn’t use jacoco reports generated during the maven build. one has to use another tool. there are several available: i chose https://codecov.io/ , for no other reason than because mockito also uses it. the drill is the same, register using github, accept conditions and it’s a go.
codecov happily uses jacoco coverage reports, but chooses to display only lines coverage - the less meaningful metrics imho. yet, it’s widespread enough, so let’s display it to users via a nice badge that can be hot-linked:
the build configuration file needs to be updated:
script: - ./mvnw clean install - bash <(curl -s https://codecov.io/bash)
on every build, travis ci calls the online codecov shell script, which will somehow update the code coverage value based on the jacoco report generated during the previous build command.
deployment to bintray
companies generally deploy built artifacts on an internal repository. open source projects should be deployed on public repositories for users to download - this means bintray and jcenter. fortunately, travis ci provides a lot of different remotes to deploy to, including bintray.
usually, only a dedicated branch should deploy to the remote e.g.
release . that parameter is available in the configuration file:
deploy: - on: branch: release provider: bintray skip_cleanup: true file: target/bin/bintray.json user: nfrankel key: $bintray_api_key
note the above
$bintray_api_key variable. travis ci offers environment variables to allow for some flexibility. each of them can be defined as not to be displayed in the logs. beware that in that case, it’s treated as a secret and it cannot be displayed again in the user interface.
for bintray, it means getting hold of the api key on bintray, creating a variable with a relevant name and setting its value. of course, other variables can be created, as many as required.
most of the deployment configuration is delegated to a dedicated bintray-specific json file. among other information, it contains both the
version values from the maven pom. maven filtering is configured to automatically get the content from the pom and source it to the configuration file to use: notice the path reference is under the generated
open source development on github requires a different approach than for in-house enterprise development. in particular, the standard build pipeline is based on a completely different stack. using this stack is quite time-consuming because of different defaults and all the documentation that needs to be read, but it’s far from impossible.
Published at DZone with permission of Nicolas Fränkel, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.