【问题标题】:Maven Error assembling WAR: webxml attribute is required when building the SpringMVC project with pure Java Based Configuration and no xml'sMaven 组装 WAR 时出错:使用纯基于 Java 的配置构建 SpringMVC 项目且没有 xml 时需要 webxml 属性
【发布时间】:2016-01-28 04:35:54
【问题描述】:

我正在开发一个基于纯 Java 配置的 Spring MVC 项目。我在执行 Maven 全新安装时收到以下错误。

未能执行目标 org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on 项目 SpringMVC-ShoppingCart:组装 WAR 时出错:webxml 属性 是必需的(如果在更新中执行,则为预先存在的 WEB-INF/web.xml 模式)-> [帮助 1]

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

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

【问题讨论】:

    标签: java maven spring-mvc maven-assembly-plugin maven-war-plugin


    【解决方案1】:

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

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

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

    failOnMissingWebXml 的默认值已从 true 更改为 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 具有基于 Java 的配置 (MWAR-262)。但是,由于您的项目可能不会使用注释来替换此文件,在这种情况下 web.xml 实际上可能会丢失,因此在插件的 3.0.1 版本中添加了完整性检查以确保注释 @WebServlet 在WAR 项目的编译类路径 (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>
    

    【讨论】:

      【解决方案2】:

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

      例如:

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

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

      【讨论】:

      • 这项工作在 mvn ver 3.5.1 上为我工作,接受的答案没有
      • 如果您不使用 maven-war-plugin 而只是打包为战争,则此答案有效
      猜你喜欢
      • 2018-05-09
      • 2014-10-29
      • 2011-07-18
      • 1970-01-01
      • 2015-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      相关资源
      最近更新 更多