The traditional way to run a Spring web application on a remote server is to package it into a war file and deploy that file into a servlet container.

Although this method has served us well in the past, managing multiple servlet containers has always been a bit cumbersome.

Spring Boot provides one solution to this problem. It allows us to package our web application into an executable jar file that uses an embedded servlet container.

This blog post describes how we can create a Spring Boot web application project that fulfils the following requirements:

Our Spring Boot application must use Thymeleaf as a templating engine.

Our Spring Boot application must provide us a way to monitor it.

Our Gradle project must have separate source and resource directories for unit and integration tests.

Let’s get started.

Creating a Java Project

Because we want to create a Java project, we have to apply the Java plugin. We can do this by following these steps:

Apply the Gradle Java plugin. Set the version of our Java source to 1.8. Configure Gradle to generate classes for Java 1.8.

Our build.gradle file looks as follows:

apply plugin: 'java' sourceCompatibility = 1.8 targetCompatibility = 1.8

Getting Started with Gradle: Our First Java Project The Java plugin adds new conventions (e.g. the default directory layout), tasks, and properties into our build. If you want to know more about this, you should read the following blog post:

Let’s move on and add integration tests into our Gradle build.

Adding Integration Tests Into Our Gradle Build

We can add integration tests into our Gradle build by using the Gradle TestSets plugin. Because I have already written a blog post that describes how we can use this plugin, I won’t describe the configuration of this plugin in this blog post.

After we have fulfilled the requirements specified in this blog post, our build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath( 'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2' ) } } apply plugin: 'java' apply plugin: 'org.unbroken-dome.test-sets' sourceCompatibility = 1.8 targetCompatibility = 1.8 testSets { integrationTest { dirName = 'integration-test' } } project.integrationTest { outputs.upToDateWhen { false } } check.dependsOn integrationTest integrationTest.mustRunAfter test tasks.withType(Test) { reports.html.destination = file("${reporting.baseDir}/${name}") }

Let’s move on and add Spring Boot support into our Gradle project.

Adding Spring Boot Support Into Our Gradle Project

We can add Spring Boot support into our Gradle project by using the Spring Boot Gradle plugin. We can use this plugin by following these steps:

Add the Spring Boot Gradle plugin (version 1.2.5.RELEASE) to the classpath of the build script. Apply the Spring Boot Gradle plugin.

The source code of our build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath( 'org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE', 'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2' ) } } apply plugin: 'java' apply plugin: 'org.unbroken-dome.test-sets' apply plugin: 'spring-boot' sourceCompatibility = 1.8 targetCompatibility = 1.8 testSets { integrationTest { dirName = 'integration-test' } } project.integrationTest { outputs.upToDateWhen { false } } check.dependsOn integrationTest integrationTest.mustRunAfter test tasks.withType(Test) { reports.html.destination = file("${reporting.baseDir}/${name}") }

We don’t have to use the Bintray’s JCenter Maven repository, but because the Gradle TestSets plugin requires it, the example application of this blog post uses it as well. Additional Reading: Spring Boot Reference Guide: 59.1 Including the plugin

After we have applied the Spring Boot Gradle plugin, we can

Package our application into an executable jar file.

Run our application by using the bootRun task.

Omit the version information information of Spring Boot dependencies.

Package our application into a war file.

Naturally we can also configure the Spring Boot Gradle plugin and customize the tasks that are used to run and package our application.

Let’s move on and get the required dependencies with Gradle.

Getting the Required Dependencies

We can get the dependencies of our Spring Boot application by using so called starter POMs. The Spring Boot Reference Guide describes the starter POMs as follows:

Starter POMs are a set of convenient dependency descriptors that you can include in your application. You get a one-stop-shop for all the Spring and related technology that you need, without having to hunt through sample code and copy paste loads of dependency descriptors.

In other words, we have to select the correct starter POMs and add the starter POM dependencies into our Gradle build.

We can get the required dependencies by following these steps:

Ensure that the dependencies are fetched from the central Maven2 repository. Add the spring-boot-starter-actuator dependency into the compile configuration. We need this dependency because it provides us a way to monitor our application when it is running. Add the spring-boot-starter-thymeleaf dependency into the compile configuration. We need this dependency because we want to create a web application that uses Thymeleaf as a templating engine. Add the spring-boot-starter-test dependency into the testCompile configuration. We need this dependency because we want to write both unit and integration tests for our web application.

The source code of our build.gradle file looks as follows:

buildscript { repositories { jcenter() } dependencies { classpath( 'org.springframework.boot:spring-boot-gradle-plugin:1.2.5.RELEASE', 'org.unbroken-dome.gradle-plugins:gradle-testsets-plugin:1.0.2' ) } } apply plugin: 'java' apply plugin: 'org.unbroken-dome.test-sets' apply plugin: 'spring-boot' sourceCompatibility = 1.8 targetCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile( 'org.springframework.boot:spring-boot-starter-actuator', 'org.springframework.boot:spring-boot-starter-thymeleaf' ) testCompile('org.springframework.boot:spring-boot-starter-test') } testSets { integrationTest { dirName = 'integration-test' } } project.integrationTest { outputs.upToDateWhen { false } } check.dependsOn integrationTest integrationTest.mustRunAfter test tasks.withType(Test) { reports.html.destination = file("${reporting.baseDir}/${name}") }

Let’s move on and find out how we can run our Spring Boot application.

Running Our Spring Boot Application

We can run our Spring Boot application by using one of the following methods:

First, we can run our application without creating a jar file by using the bootRun task of the Spring Boot Gradle plugin. We should use this method during the development phase because it makes our static classpath resources (i.e. files found from the src/main/resources directory) reloadable.

In other words, if we use this method, we can make changes to these files when our Spring Boot application is running, and we can see these changes without restarting our application.

We can use this method by running the following command at the command prompt:

gradle clean bootRun

Second, we can package our application into an executable jar file and run the created jar file. We should use this method when we want to run our Spring Boot application on a remote server.

We can create an executable jar file by running the following command at the command prompt:

gradle clean build

This command creates the spring-boot-web-application.jar file to the build/libs directory. After we have copied this jar file to the remote server, we can start our application by running the following command at the command prompt:

java -jar spring-boot-web-application.jar

Let’s move on and summarize what we learned from this blog post.

Summary

This blog post has taught us four things:

We can add Spring Boot support into our Gradle project by applying the Spring Boot Gradle plugin.

We can select the preferred Spring Boot version by setting the version of the Spring Boot Gradle plugin. This means that we don’t have to set the dependency versions of the Spring Boot dependencies.

If we want to run our Spring Boot application in a development environment, we should use the bootRun task of the Spring Boot Gradle plugin.

If we want to run our Spring Boot application on a remote server, we should package it into an executable jar file, copy that jar file to the remote server, and run it.

P.S. You can get the example application of this blog post from Github.