Springboot项目打包war配置详解 1. 排除内置tomcat依赖 2. 添加servlet依赖 3. 修改打包方式 4. 修改主启动类 5. 完整pom.xml 6. 效果图
1. 排除内置tomcat依赖
< dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> < exclusions> < exclusion> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-tomcat</ artifactId> </ exclusion> </ exclusions> </ dependency>
2. 添加servlet依赖
< dependency> < groupId> jakarta.servlet</ groupId> < artifactId> jakarta.servlet-api</ artifactId>
</ dependency>
3. 修改打包方式
< packaging> war</ packaging>
4. 修改主启动类
@SpringBootApplication
public class Springboot3Application extends SpringBootServletInitializer {public static void main(String[] args) {SpringApplication.run(Springboot3Application.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(Springboot3Application.class);}
}
5. 完整pom.xml
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0" xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" > < modelVersion> 4.0.0</ modelVersion> < parent> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-parent</ artifactId> < version> 3.1.2</ version> < relativePath/> </ parent> < groupId> com.zlm</ groupId> < artifactId> springboot3</ artifactId> < version> 0.0.1-SNAPSHOT</ version> < name> springboot3</ name> < description> springboot3</ description> < packaging> war</ packaging> < properties> < java.version> 17</ java.version> </ properties> < dependencies> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-web</ artifactId> < exclusions> < exclusion> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-tomcat</ artifactId> </ exclusion> </ exclusions> </ dependency> < dependency> < groupId> jakarta.servlet</ groupId> < artifactId> jakarta.servlet-api</ artifactId> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-devtools</ artifactId> < scope> runtime</ scope> < optional> true</ optional> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-configuration-processor</ artifactId> < optional> true</ optional> </ dependency> < dependency> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> < optional> true</ optional> </ dependency> < dependency> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-starter-test</ artifactId> < scope> test</ scope> </ dependency> </ dependencies> < build> < plugins> < plugin> < groupId> org.springframework.boot</ groupId> < artifactId> spring-boot-maven-plugin</ artifactId> < configuration> < skip> true</ skip> < excludes> < exclude> < groupId> org.projectlombok</ groupId> < artifactId> lombok</ artifactId> </ exclude> </ excludes> </ configuration> </ plugin> </ plugins> </ build> </ project>
6. 效果图