Maven 错误组装 WAR:使用基于纯 Java 的配置且没有 xml 的配置构建 SpringMVC 项目时需要 webxml 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33390460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Maven Error assembling WAR: webxml attribute is required when building the SpringMVC project with pure Java Based Configuration and no xml's
提问by Omkar
I am developing a Spring MVC project with pure Java based configuration. I am getting the error below when I do a Maven clean install.
我正在开发一个带有纯 Java 配置的 Spring MVC 项目。当我执行 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]
无法在项目 SpringMVC-ShoppingCart 上执行目标 org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war): Error assemble WAR: webxml attribute is required (or pre-existing WEB-INF/web .xml 如果在更新模式下执行)-> [帮助 1]
The error says that web.xml
is missing, but I did not have one since I used pure Java based configuration.
错误说web.xml
缺少,但我没有,因为我使用了基于纯 Java 的配置。
How to make sure that the project builds and creates war file without web.xml
?
如何确保项目在没有web.xml
.
采纳答案by Tunaki
This error happens because the maven-war-plugin
, in version 2.6 or lower, expects by default a src/main/webapp/web.xml
file to be present in your WAR project, and it didn't find it.
发生此错误的原因是maven-war-plugin
,在 2.6 或更低版本中,默认情况下期望src/main/webapp/web.xml
WAR 项目中存在一个文件,但它没有找到它。
Use annotations and upgrade to 3.0.0 or newer
使用注解并升级到 3.0.0 或更新版本
As of version 3.0.0 of the plugin, the presence of a web.xml
is not mandatory by default anymore:
从插件的 3.0.0 版本开始,web.xml
默认情况下 a 的存在不再是强制性的:
The default value for
failOnMissingWebXml
has been changed fromtrue
tofalse
.
的默认值
failOnMissingWebXml
已从 更改true
为false
。
This means that upgrading the plugin directly solves the issue. You can add the following configuration in your POM:
这意味着升级插件可以直接解决问题。您可以在 POM 中添加以下配置:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.0.0</version>
</plugin>
The reason is that since Servlet 3.0, the web.xml
file is no longer needed in a web application, and can be replaced with annotationsto 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). If it isn't, and there is no web.xml
file in your project, the plugin will still fail by default.
原因是从 Servlet 3.0 开始,web.xml
Web 应用程序中不再需要该文件,并且可以用注释替换以具有基于 Java 的配置 ( MWAR-262)。但是,由于您的项目可能不会使用注释来替换此文件,在这种情况下web.xml
实际上可能会丢失,因此在插件的 3.0.1 版中添加了健全性检查以确保注释@WebServlet
在您的 WAR 的编译类路径中项目(MWAR-396)。如果不是,并且web.xml
您的项目中没有文件,默认情况下插件仍然会失败。
Ignore the missing web.xml
忽略缺失 web.xml
If you just want the plugin to explicitly ignore a missing web.xml
file, regardless of the usage of annotations, you can set the failOnMissingWebXml
parameter to false
. A sample configuration would be:
如果您只想让插件显式忽略丢失的web.xml
文件,而不管注释的使用,您可以将failOnMissingWebXml
参数设置为false
. 示例配置是:
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
回答by Ankur jain
This is because you have not included web.xml in your web project and trying to build war using maven. To resolve this error, you need to set the failOnMissingWebXml to false in pom.xml file.
这是因为您没有在 web 项目中包含 web.xml 并尝试使用 maven 构建 war。要解决此错误,您需要在 pom.xml 文件中将 failOnMissingWebXml 设置为 false。
For example:
例如:
<properties>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
Please see the blog for more details: https://ankurjain26.blogspot.in/2017/05/error-assembling-war-webxml-attribute.html
更多详情请看博客:https: //ankurjain26.blogspot.in/2017/05/error-assemble-war-webxml-attribute.html