Spring boot 打包为war包报错:提示缺少web.xml
一. 异常
在开发SpringBoot多模块项目中,打包时报错了,如下:
Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project xhbase-rest:
Error assembling WAR:
webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
二. 项目目录结构如下:
其中SpringBootApplication启动类在rest模块,rest 模块依赖于 sdk 模块,sdk 模块依赖于 core 模块,core 模块依赖于common模块。构建单模块项目和多模块项目打jar包时都不会报错。
三. 原因
maven 插件 maven-war-plugin 中 Servlet 版本太低,要求必须要有web.xml文件才行。
四. 解决办法:
要么,添加插件配置项,如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
要么,修改其默认版本,如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
五. 参考
(。・v・。)Case
执行命令maven package将spring boot项目打成war包报错:
$ mvn package //... [ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project spring4-mvc-maven-ajax-example: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]Cause
spring boot项目中引用了依赖包spring-boot-starter-web。该包中引用的spring-boot-starter-tomcat里包含了tomcat嵌入式servlet容器,其不同版本实现的是不同的servlet版本规范。servlet规范与tomcat两者版本对应关系如下:
Servlet/JSP Spec
Apache Tomcat version
Actual release revision
Minimum Java Version
3.0/2.2
7.0.x
7.0.12
1.6
2.5/2.1
6.0.x
6.0.32
1.5
2.4/2.0
5.5.x
5.5.33
1.4
2.3/1.2
4.1.x (archived)
4.1.40 (archived)
1.3
2.2/1.1
3.3.x (archived)
3.3.2 (archived)
1.1
Solution
1. For servlet container < 3, make sure
WEB-INF/web.xml
file exists.Default location for web.xml is "/src/main/webapp/WEB-INF/web.xml"
2. For servlet container >=3, and NO
web.xml
web application, declares the followingmaven-war-plugin
plugin, and set thefailOnMissingWebXml
option to false.pom.xml
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>