Deploying Spring Boot App
A WAR-Packaged Spring Boot App¶
Application Entry Point¶
When we deploy to the existing Tomcat web container, Tomcat won't call our Boot application's main method.
- Instead, it needs another way of configuring and launching our application.
If we chose War Packaging when we created our app, our dependencies will be set up for building a WAR file with our build tool (Maven or Gradle).
- The Spring Boot starter will create a
ServletInitializerclass that allows Tomcat to launch our application.
JMX Identifier¶
When multiple Spring Boot applications are deployed to a Tomcat server, each needs a unique internal identifier for Tomcat's management functions.
In application.properties, add:
spring.jmx.default-domain=EventTracker
(You can choose any unique value for this property.)
- Open
application.propertiesand add:# You can substitute your application name for "EventTracker" spring.jmx.default-domain=EventTracker
WAR File Creation¶
We can customize how Gradle builds the WAR file.
-
Open your
build.gradlefile. -
Add the following war task to specify where the war file should be created and what it should be called.
war { // Gradle will replace the variable archivesBaseName // with our app name, as defined in settings.gradle archiveFileName = "${archivesBaseName}.war" destinationDirectory = file("../") // Project workspace directory } - Gradle | Refresh Gradle project
Deploying¶
Application Database¶
Your application's database must be deployed and accessible on the server before you deploy your application WAR file.
-
Use MySQL Workbench Forward Engineer to dump your schema to a
.sqlfile to your project's workspace directory. -
Secure copy your dump file to your AWS instance (in your local terminal):
- NOTE: 123.45.6.78 is a placeholder for your IP address
scp -i ~/.ssh/aws.pem ~/SD/Java/YOURWORKSPACENAME/dump.sql ec2-user@12.34.56.78:~ -
Secure Shell into your server and import your dump.sql into your sql server:
-
Connect to your AWS server from your terminal:
ssh -i ~/.ssh/aws.pem ec2-user@12.34.56.78- On your remote server, load your dump.sql into MySQL:
mysql -u root -p < ~/dump.sql
Application Deployment¶
-
In the gradle tasks area select the project you are trying to deploy and run the
build/wartask.
-
Secure copy your
.warfile to your AWS instance (in your local terminal). Use your IP address, not the one in the example. Navigate into your projects directory in terminal and then execute:scp -i ~/.ssh/aws.pem YOURPROJECTNAME.war ec2-user@12.34.56.78:/var/lib/tomcat/webapps/