The Simple Java Build with Travis CI and Spring Boot

MicrosoftTeams-image (1)

In this post, I’ll show you how simple it can be to build a Java project on Travis CI whilst using Spring Boot, it’s quick, easy, and efficient. Let’s get into it.

Getting started

To get started, we will use JSP and JSTL to render a simple “Hello world” view. So, we must import these dependencies to the project. Open the pom.xml file, and add the following lines:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

Let’s create a class that declares that our application is a Spring Boot application. We’ll use the @SpringBootApplication annotation:

@SpringBootApplication
public class BootApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootApplication.class, args);
    }
}

Let’s create a configuration to resolve the view:

@Configuration
public class ViewConfig {

    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/jsp/");
        resolver.setSuffix(".jsp");

        return resolver;
    }
}

Finally let’s code out the view:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>

<%@ taglib prefix="templates" tagdir="/WEB-INF/tags/templates" %>

<templates:app>
    <jsp:body>
        hello world
    </jsp:body>
</templates:app>

The .travis.yml file

So this is what my .travis.yml file looks like:

language: java
jdk:
 - oraclejdk8
deploy:
 provider: heroku
 api-key: 
  secure: $HEROKU_API_KEY
 app: lippep-spring-boot-test

In this case I’ve deployed it to Heroku, if you’re using Heroku, set your environment variables in Travis, then run the build, and there you have it.

Conclusion

You’ve just deployed a simple app using Heroku with Travis CI. This is a very basic Spring Boot project but a great way to practice. If you have any questions please email me at montana@travis-ci.org.

Happy building.