Deploying and Running a SpringBoot Project in Tomcat
A record of the process for packaging a SpringBoot project and deploying it to run in a Tomcat container (you can never guess what other bizarre requirements a customer might have).
Contents
Package Spring Boot as a WAR
Package the Spring Boot application as a WAR and run it in a Tomcat container
1. Modify the packaging type in the Maven file
<packaging>war</packaging>
2. Remove Spring Boot’s Embedded Tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Review the lifecycle of Maven dependencies
compile: Compile dependency scope, the default dependency scope. The dependency is effective in all threeclasspath: compile, test, and runtime.test: Test dependency scope, effective only in the testclasspath.provided: Provided dependency scope, effective for compile and testclasspath, but ineffective for runtimeclasspath. For example, servlet-api is needed as a dependency during compilation and testing, but when running the project, the container already provides it, so there is no need toMaveninclude it again.runtime: Runtime dependency scope. Effective for test and runtimeclasspath, but ineffective at compile time. For example, ajdbcdriver package: compiling the project's main code only requires theJDBCinterface provided byJDK; the concreteJDBCdriver that implements the interface is needed only when running tests or the project.system: System dependency scope, consistent with theprovideddependency scope. However, when using asystemdependency, the path to the dependency file must be explicitly specified through thesystemPathelement. Since this type of dependency is not resolved through a Maven repository and is often tied to the local system, it may make the build non-portable and should therefore be used with caution. ThesystemPathelement can reference environment variables.import: Used for onedependencyManagementto inherit from anotherdependencyManagement.
3. Modify the startup class
@SpringBootApplication
// 继承 SpringBootServletInitializer 类
public class MapApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MapApplication.class, args);
}
//重写configure方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(MapApplication.class);
}
}
4. Repackaging
mvn clean
mvn package
5. Deploy the WAR package
Move the WAR file from the target directory into Tomcat’s webapps folder.
Of course, you can also use Baota—or whatever you prefer.
6. Start Tomcat
Check the Tomcat logs to confirm it started successfully.

7. Celebrate with a Coke?
[dog]