Build Java Maven github project on travis-ci
I used to use Cloudbees’ buildhive for continuous builds of my Java/Maven based github projects. But buildhive currently does not offer JDK 8. So far that hasn’t been a problem, but I recently started using lambdas and default methods in interfaces and other Java 8 goodness. And now buildhive does not work for me anymore.
So I looked for alternatives and tried travis-ci.org. It was easy enough to set up a free account: You just authorize their service through your github login. Then all your projects will be listed on travis and you just click a switch to enable a build.
.travis.yml with FTP upload
To actually activate a build, you have to add a .travis.yml file at the root of your project.
The builds then happen automatically whenever you commit changes to github. This is the build list for one of my projects.
My build produces a distributable zip file, using Maven assembly plugin, that contains all the jars and start scripts of my application. I want to make the latest stable version of that zip file available for public download. With buildhive I used the permanent URL of the build artifact within the workspace of the last stable Jenkins build. But travis does not store anything after the build.
So to build my Java Maven project with JDK 8 and do the FTP upload of the zip artifact, I ended up with these lines:
language: java jdk: oraclejdk8 env: global: - secure: L2lr/F0gIvyVUl0nJ7w9saGV7wZkL6nO61IxilDY/76iTlnhrFXn5Q8vATGbiRYdDW/tG1kyDUbKaWSkYrpV2Agm4wV/KmMg2CWRiIcQPPqwSEENx/1UZ/dBnCQGcRkkYApu5ayjGnX3Srg3ty1zvdud/O8tiKtWkkBDipJSpfY= - secure: OekVM5ZyLGHpqurOUWJcq0kKBA78WKZdXaA9aylwrjjQFeVoZxyxeZTYbhLajN4Ggg4Th58QwjUHpwcgZlnsxx4heDo1wyHxXojJd0H1LWKXJwet82IXaFJbl+Yz/htr7uWSFTUF6Szx70cpMxlGe3qsIFlgViEo9UGhHHdrjdY= after_success: ./.travis/artifact-upload.sh
The env – global – secure entries are the encrypted username and password for my FTP server. Details about the encryption steps are at the end of this blog post.
Artifact upload script
The .travis/artifact-upload.sh script performs the actual upload. The .travis directory is in the root of my github project. The script looks like this:
#! /bin/bash local_file="$(ls $TRAVIS_BUILD_DIR/typepad-dist/target/*.zip | head -n 1)" target_url='ftp://doepner.net/~/public_html/dev/dist/ci-builds/typepad.zip' echo "Uploading $local_file to $target_url" curl -u $FTP_USER:$FTP_PASSWORD -T "$local_file" "$target_url"
I am only interested in the latest zip and I want the URL to be permanent, that’s why the filename is hardcoded as typepad.zip.
Build status and download links
Similar to buildhive, travis-ci provides nice build status icons that automatically show the current status of your build.
== Build status image:https://travis-ci.org/odoepner/typepad.svg?branch=master[ link="https://travis-ci.org/odoepner/typepad"] http://dev.doepner.net/dist/ci-builds/typepad.zip[Download latest build]
If you are not used to this syntax: It is AsciiDoc, not the default Markdown format of github READMEs.
Encryption of FTP credentials
Travis supports encryption of environment variables. This makes sense, because you probably don’t want to expose your FTP username/password to the world.
To perform the encryption, a local travis command-line installation is required. On Debian it can be setup like this:
1) Install JRuby (but not Rails)
2) gem install travis
3) cd to local working copy of your project
4) travis encrypt FTP_USER=yourusername --add
5) travis encrypt FTP_PASSWORD=yourpassword --add
--add tells the travis command to add the resulting config directly to the .travis.yml file in your project directory. That’s why you first need cd to the base dir of your project.