[Java] Deploy WEB application by Spring Boot to Tomcat server as WAR

1 minute read

Premise

  • AmazonLinux2(EC2)
  • JDK 1.8.0
  • MySQL 8.0
  • Gradle 6.0 for configuration management
  • Here has already built an application server environment with EC2 and RDS. Initially, the EC2 instance was set to micro, but the build failed due to lack of memory, so I chose medium.
  • Here with Tomcat and JDK installed. This time I don’t use Apache, but for now I’ll just use Tomcat.
  • Project is cloned from GitHub.

Add description for war deployment to the class that executes #Spring

  • I think that a class that automatically executes Spring is created when the project is created (class with @SpringBootApplication annotation).
  • Describe here to make it deployable as a war. Referenced Spring formula.
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}

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

}

Gradle configuration and build

Gradle settings

  • Set war creation. archiveName is the name of war.
  • Set SpringBoot embedded Tomcat so that it does not conflict with the one on EC2
  • Besides, since I also included the screen test code by Selenied in my project, I wrote a setting to exclude it in test.
plugins {
id'org.springframework.boot' version '2.2.4.RELEASE'
id'io.spring.dependency-management' version '1.0.9.RELEASE'
id'java'
id'war'
}

// ~ omitted ~

war {
    enabled = true
    archiveName ='sample.war'
}

dependencies {
// ~ omitted ~
providedRuntime'org.springframework.boot:spring-boot-starter-tomcat'
}

test {
// ~ omitted ~
exclude'com/example/demo/ViewTest.class'
}

Build on EC2

  • Git clone the WEB application project in your home directory.
  • Reflect RDS settings in application.properties in the project.

In the directory where the application.properties of the project is


$ sudo vim application.properties

application.properties


spring.datasource.url=jdbc:mysql://RDS endpoint:3306/database name?serverTimezone=JST
spring.datasource.username=******
spring.datasource.password=***********
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.open-in-view=true
#~omission~
  • Execute gradlew build command. If build succeeds, sample.war is generated in project name/build/libs.
  • There is a place I don’t know yet, but a war called project name-0.0.1-SNAPSHOT.war was also generated (the contents are the same as sample.war?).

in the project directory


$ ./gradlew build

Deploy the generated war to Tomcat

  • Copy sample.war under tomcat/webapps. * Deploy to /opt/tomcat-9/webapps for yourself.
$ cp sample.war /opt/tomcat-9/webapps/
  • Restart Tomcat
$ sudo systemctl restart tomcat

If you access it like ↓, the application will move. http://EC2 endpoint: 8080/sample/ Request destination according to created application