How To Build A Java Project Using Travis CI On Windows Platform?

Reading Time: 3 minutes

What Is Travis CI?

Travis CI is an open-source CI service used to build and test projects hosted at your favorite SCM. You can easily configure Travis CI by adding a file named .travis.yml to the root directory of the GitHub repository.

Travis CI was the first CI as a Service tool!

You can easily integrate Travis with the common cloud repositories like GitHub, Bitbucket, etc. It removes the need for a dedicated server as the Travis CI server is hosted in the cloud and therefore it saves a lot of cost.

Travis CI is free for open source projects. For commercial projects, you need to purchase an enterprise plan. You can find out more info about Travis from our other articles by clicking here.

Running Java Project On Travis CI

Running a Java project using Travis CI on a Linux platform is pretty starightforward. All you have to do is set language as java like this:

language: java

If Travis detects a pom/gradle file in the project directory, it will automatically install the required dependencies for us. By default, it uses Maven 3.

Switching between JDK versions in the same pipeline can also be achieved very easily using jdk_switcher :

script:
  - jdk_switcher use openjdk8
  - # do stuff with open Java 8

The best part is, jdk_switcher also automatically updates the $JAVA_HOME appropriately.

However, when it comes to Windows platform, the support isn’t that great as of now as Windows platform is still in experimental stages. But, do not worry as this blog is exactly for that.

Windows Platform Support For Travis CI

The official Travis docs states the following:

Take note that our Windows environment is in early stages and a minimal subset of what’s available on Linux or macOS is currently supported.

To build on Windows simply do:

os: windows

Do note that only Windows 1809 version is supported and all builds run inside a Gitbash instance. You also have Chocolatey package manager which is quite useful and also the standard powershell module.

The default packages which come installed in Chocolatey are as follows:

  • 7zip.install v19.0
  • chocolatey v0.10.15
  • chocolatey-core.extension v1.3.5.1
  • chocolatey-dotnetfx.extension v1.0.1
  • chocolatey-fastanswers.extension v0.0.2
  • chocolatey-visualstudio.extension v1.8.1
  • chocolatey-windowsupdate.extension v1.0.4
  • cmake.install v3.16.2
  • curl v7.68.0
  • DotNet4.5.2 v4.5.2.20140902
  • DotNet4.6 v4.6.00081.20150925
  • DotNet4.6-TargetPack v4.6.00081.20150925
  • DotNet4.6.1 v4.6.01055.20170308
  • dotnetfx v4.8.0.20190930
  • git.install v2.25.01
  • hashdeep v4.4
  • jq v1.6
  • KB2919355 v1.0.20160915
  • KB2919442 v1.0.20160915
  • KB2999226 v1.0.20181019
  • KB3033929 v1.0.5
  • KB3035131 v1.0.3
  • llvm v9.0.0
  • microsoft-build-tools v15.0.26320.2
  • mingw v8.1.0
  • netfx-4.5.1-devpack v4.5.50932
  • netfx-4.5.2-devpack v4.5.5165101.20180721
  • netfx-4.6.1-devpack v4.6.01055.00
  • rsync v5.5.0.20190204
  • ruby v2.7.0.1
  • vcredist140 v14.24.28127.4
  • vcredist2017 v14.16.27033
  • visualstudio-installer v2.0.1
  • visualstudio2017-workload-netcorebuildtools v1.1.2
  • visualstudio2017-workload-vctools v1.3.2
  • visualstudio2017-workload-webbuildtools v1.3.2
  • visualstudio2017buildtools v15.9.18.0
  • Wget v1.20.3.20190531
  • windows-sdk-10.1 v10.1.18362.1
  • winscp v5.15.9
  • winscp.install v5.15.9
  • wsl v1.0.1

And, if you need anything apart from the list above then you can install it manually. However, the way Travis stores the Windows path is a bit different than what we are used to seeing. For example:

A basic Python 2.7.9 interpreter is also included: /C/ProgramData/chocolatey/bin/python.exe

Travis provides some basic language support for Windows platform such as:

  • Bash
  • C
  • C++
  • Go
  • Julia
  • Node.js
  • Rust

And as you can tell, Java isn’t one of them! Now, let us see how we can achive that.

How To Build A Java Project Using Travis CI On Windows Platform?

Firstly, we will use shell as an alternative to Java here. You can use any language listed above as long as you follow the template I am providing below. I will be using Maven as an example. Also, this is a multi-os template which I created and you may modify and use it.

branches:
  only:
  - master

os:
  - windows

language: shell

jdk:
  - openjdk11

before_install:
  - |
    if [ "$TRAVIS_OS_NAME" = "windows" ]; then 
      choco install maven;
      refreshenv;
    fi;

install:
  - |
    if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
      #Whatever you want to do
    fi;

script:
  - export JAVA_HOME="${JAVA_HOME:-/c/jdk}"
  - |
    if [[ "$TRAVIS_OS_NAME" == "windows" ]]; then
        maven="$(find 'C:\ProgramData\chocolatey\lib\maven' -name mvn | head -n1)";
    else
        maven="mvn";
    fi;
  - $maven <your_goal_here>
  

First, we have our standard branch filter. Then, we are setting OS to Windows and language as Shell. For JDK, we are using openjdk11. This is the standard setup which you must be already aware of. You can modify it as per your needs.

The real magic starts beyond that! On our before_install we are installing Maven using Chocolately package manager and also refreshing the environment. However, refreshing is totally optional. Next, you can install whatever you want.

Finally, we have the script block where we are setting path to our Java and also creating an alias for Maven. This step is important as Travis will complain about not finding mvn if you skip this!

After that you can run any Maven command using the alias we set above! Enjoy! I hop you liked the article and if you did make sure to check our other useful articles and leave a like and comment!

knoldus-advt-sticker

Written by 

Mohit Saxena is a Software Consultant having experience of more than 2 years. He is always up for new challenges and loves to aggressively move towards completing the software requirements. On a personal front, he loves to climb mountains and is a big foodie.

Leave a Reply