Maven 错误组装 WAR:使用纯 Java 基于配置构建 SpringMVC 项目时需要 webxml 属性并且没有 xml's

Maven Error assembling WAR: webxml attribute is required when building the SpringMVC project with pure Java Based Configuration and no xml's

我正在开发一个 Spring MVC 项目,其配置完全基于 Java。当我执行 Maven 全新安装时出现以下错误。

Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project SpringMVC-ShoppingCart: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

错误提示缺少 web.xml,但我没有,因为我使用的是纯 Java 的配置。

如何确保项目在没有 web.xml 的情况下构建和创建 war 文件?

发生此错误是因为 maven-war-plugin,在版本 2.6 或更低版本中,默认情况下期望 src/main/webapp/web.xml 文件出现在您的 WAR 项目中,但它没有找到它。

使用注释并升级到 3.0.0 或更新版本

从插件的 3.0.0 版开始,存在 web.xml is not mandatory by default anymore:

The default value for failOnMissingWebXml has been changed from true to false.

这意味着直接升级插件即可解决问题。您可以在 POM 中添加以下配置:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>3.0.0</version>
</plugin>

原因是从 Servlet 3.0 开始,Web 应用程序不再需要 web.xml 文件,并且 can be replaced with annotations to have a Java-based configuration (MWAR-262). However, since your project might not use annotations to replace this file, in which case the web.xml could actually be missing, a sanity check was added in version 3.0.1 of the plugin to make sure that the annotation @WebServlet is in the compile classpath of your WAR project (MWAR-396)。如果不是,并且您的项目中没有 web.xml 文件,插件仍然会默认失败。

忽略缺失的web.xml

如果您只想让插件明确忽略丢失的web.xml文件,而不考虑注释的使用,您可以将failOnMissingWebXml参数设置为false。示例配置为:

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.6</version>
  <configuration>
    <failOnMissingWebXml>false</failOnMissingWebXml>
  </configuration>
</plugin>

这是因为您没有将 web.xml 包含在您的 Web 项目中并尝试使用 Maven 构建 war。要解决此错误,您需要在 pom.xml 文件中将 failOnMissingWebXml 设置为 false。

例如:

<properties>
    <failOnMissingWebXml>false</failOnMissingWebXml>   
</properties>

详情请看博客:https://ankurjain26.blogspot.in/2017/05/error-assembling-war-webxml-attribute.html