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.
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.
Creating a Java Project
Because we want to create a Java project, we have to apply the java plgin. 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:
|
1
2
3
4
|
apply plugin: 'java'sourceCompatibility = 1.8targetCompatibility = 1.8 |
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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.8targetCompatibility = 1.8testSets { integrationTest { dirName = 'integration-test' }}project.integrationTest { outputs.upToDateWhen { false }}check.dependsOn integrationTestintegrationTest.mustRunAfter testtasks.withType(Test) { reports.html.destination = file("{reporting.baseDir}/{name}")} |
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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.8targetCompatibility = 1.8testSets { integrationTest { dirName = 'integration-test' }}project.integrationTest { outputs.upToDateWhen { false }}check.dependsOn integrationTestintegrationTest.mustRunAfter testtasks.withType(Test) { reports.html.destination = file("{reporting.baseDir}/{name}")} |
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.
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:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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.8targetCompatibility = 1.8repositories { 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 integrationTestintegrationTest.mustRunAfter testtasks.withType(Test) { reports.html.destination = file("{reporting.baseDir}/{name}")} |
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:
|
1
|
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:
|
1
|
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: