问题背景
在maven里,项目clean完做install时发生报错如下
Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project…“webxml attribute is required” error in Maven…
pom.xml文件如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 指定端口 -->
<port>81</port>
<!-- 请求路径 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
问题原因:
在maven-war-plugin的2.6以下的版本中,把
src/main/webapp/WEB-INF/web.xml
作为寻找web.xml文件的默认路径,由于在创建模块时候使用使用了add Framework Support 自动创建web框架,导致web.xml的目录结构是moduleName1/web/WEB-INF/web.xml
因此由于找不到web.xml文件,所以install报错
解决方法:
- 更改web包的名字为webapp
- 把webapp包移动到src/main目录下
- 重新install
运行成功:
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 19.283 s
[INFO] Finished at: 2022-12-01T16:26:32+08:00
[INFO] ------------------------------------------------------------------------
Process finished with exit code 0
其他解决方法:
使用注释并升级到3.0.0或更新版本
从插件的3.0.0版本开始,默认情况下web.xml的存在不再是强制的
failOnMissingWebXml 的默认值已从true更改为false。
因此升级插件到3.0.0版本以上即可。
可以在POM中添加以下配置:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
重新加载,install成功!