Previously I made a stories about how to do testing in Java EE, and since I’m a pragmatic programmer and like to being lazy, actually you can automate your testing and build process in Java EE. Continuous Integration (CI) is really something important in your software development life cycle because it will make your life more easier, you will not needed to doing a repetitive task for testing, build and then deploy it into production or development server. Jenkins and Gitlab was very famous tools to perform CI in many company, but I and you must be not wanting to rent a server in order setup Jenkins and Gitlab for open source or experimental software, right?

There is always solutions, in this case you can use Travis CI that working pretty well with Github. I will give you a example how to perform CI, of course with Java EE. With my previous story about testing in Java EE, I will show you how to automate this build phase. Like usually the source code is available on my github account. Prerequisite using Travis of course you need to register first, don’t worry if you already have a Github account I suggest you to register using your Github account so Travis will automatically linked with your Github account and synchronize all of your Github repository. The first time you need to do after register and synchronize all of your Github repository is activate repository that you wanted to be included on Travis automation build.

activate_repo_in_travis

Now, in your project directory you need to create Travis configuration files with named .travis.yml. The configuration that will I show it’s very simple since we won’t use any database or any external configurations, but of course you can do many things with Travis configuration. This tutorial will be specific on Java EE environment so you will need to declare jdk version.

language: java

jdk:
    - oraclejdk8

With this, every time you perform git push it will also made Travis build your projects automatically. Travis will execute Maven or Gradle command depend on what build tools you use on your project, and of course it will also perform all of your testing class (You can specify some behaviour or scripts for specific command). Knowing your change will either made the build process failed or success is not enough, the benefit in doing CI was you can automatically generate report from your testing. In order to have a good quality test report from your unit and integration test you need to add a good old dependency in your pom.xml called Jacoco plugins.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>0.7.9</version>
    <executions>
        <execution>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
        </execution>
        <execution>
            <id>report</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>report</goal>
            </goals>
        </execution>
        <execution>
            <id>prepare-integration-test-agent</id>
            <goals>
               <goal>prepare-agent-integration</goal>
            </goals>
        </execution>
        <execution>
           <id>generate-integration-test-report</id>
           <goals>
               <goal>report-integration</goal>
           </goals>
       </execution>
   </executions>
</plugin>

The best things about Jacoco is integrated well with Netbeans, you will have new menu called Code Coverage when you right click your project in Netbeans. Show report menu will give you about how much percentage your testing code covering all of your business logic code.

jacoco_code_coverage

You will need one more services from Coveralls to publish and send a notifications to your team members about the code coverage reports. Like in Travis you need to register before you can use Coveralls, don’t worry you can register using your Github account same like in Travis. Then you also need to turn on your specific repository to be identified by Coveralls.

repo_coveralls

With this, you need to add another plugins for Coveralls in your pom.xml.

<plugin>
    <groupId>org.eluder.coveralls</groupId>
    <artifactId>coveralls-maven-plugin</artifactId>
    <version>4.3.0</version>
</plugin>

Also you need to modify your Travis Configuration scripts to be like this.

language: java

jdk:
    - oraclejdk8

after_success:
    - mvn jacoco:report coveralls:report

Now, every time you do git push Travis will build and perform all your testing code if it’s success it will notify Coveralls to generate the code coverage report from your testing code. Actually there is something missing from this tutorials which is how to automatically deploy your software into the server with Travis, but since I don’t have a dedicated server I can show you how to do that but I believe outside there is many tutorials that will help you and since it will be easy to do.

travis_result

coveralls_result

Continuous integration not only help you to remove all those repetitive task such as build, test and deploy your software but it’s also made more easy to collaborate between each department division. So, did you already doing continuous integration in your software development?

References: