maven打包springboot成war包
报错一:
Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project Ocr: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)
解决:新建图中目录webapp/WEB-INFO/web/xml,web.xml不需要额外写内容
报错二:
程序包javax.servlet不存在
解决:添加依赖
<!-- 解决编译时,报程序包javax.servlet不存在的错误 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<!-- 只在编译和测试的时候用 -->
<scope>provided</scope>
</dependency>
重新打包即可
clean->package
如何打包成war
1.修改pom打包方式,为war
2.修改启动类
@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
// 用于构建war文件并进行部署
@Override
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
3.移除springboot内嵌的tomcat
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- 移除嵌入式tomcat插件 -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
【勤则百弊皆除】