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 following maven-war-plugin plugin, and set the failOnMissingWebXml 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>
本文介绍了解决SpringBoot项目使用Maven打包成WAR文件时遇到的问题,即因缺少web.xml导致的错误。文章提供了两种解决方案:一是确保对于Servlet容器版本小于3的情况存在web.xml文件;二是对于Servlet容器版本大于等于3且不需要web.xml的应用,可通过配置maven-war-plugin插件来避免错误。

4223

被折叠的 条评论
为什么被折叠?



