webxml属性是必需的。Maven错误

我得到以下错误:

装配WAR: webxml属性是必需的(如果以更新模式执行,则需要预先存在的WEB-INF/web.xml)

我已经得到web.xml在正确的地方,这是projectname\src\main\webapp\WEB-INF\web.xml

是什么导致了这种情况?

349806 次浏览
如果你能提供你的maven-war-plugin的代码片段,这将是很有帮助的。 看起来web.xml在正确的位置,仍然可以尝试显式给出位置

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>src\main\webapp\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

这对我来说也是完美的。

<project>


.....


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>
</plugins>
</build>
</project>

看起来web.xml在正确的位置,但即使如此,这个错误通常是由目录结构与Maven期望看到的不匹配引起的。例如,如果您从一个试图用Maven构建的Eclipse web应用程序开始。

如果这是问题,一个快速修复是创建一个
src/main/java和a
src/main/webapp目录(和其他目录,如果你需要它们),只移动你的文件 这里是maven目录布局的概述: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html < / p >

我的webXml标签的值需要看起来像这样才能工作:

<webXml>${project.basedir}\src\main\webapp\WEB-INF\web.xml</webXml>

确保在Project文件夹中正确放置了pom.xml。而不是在目标文件夹或其他任何地方。

看起来pom.xml没有相对对齐。

Mvn-war-plugin 2.3修复了这个问题:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
</plugin>
...

我也遇到过同样的问题,我是这样解决的:

然后在src/main/webbapp下创建一个名为WEB-INF的新文件夹

右键单击你的生成部署描述符存根

这将生成你的web.xml

我希望这有助于解决你的问题:D

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

这个解决方案适合我(我以前使用的是2.2)。此外,我使用基于Java的Servlet 3.0配置,不需要有web.xml文件。

如果你改变默认的项目路径,你必须指定web.xml文件的位置,例如:

        <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.5</version>
<configuration>
<webXml>src\main\web\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

如果您正在从基于xml的配置迁移到基于java的配置,并且您已经通过实现WebApplicationInitializer消除了对web.xml的需求,那么只需删除对web.xml文件的要求。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
...
</configuration>

这对我也有用。

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webXml>WebContent\WEB-INF\web.xml</webXml>
</configuration>
</plugin>

根据文档,它说:如果web.xml文件丢失,是否构建失败。如果你想在没有web.xml文件的情况下构建WAR,则设置为false。如果您正在构建一个没有web.xml文件的覆盖,这可能是有用的。 缺省值:true。 用户属性为:failOnMissingWebXml.

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

希望它能让你更清楚

你的文件夹结构改变了,所以文件不再在/ src / main / webapp / web - inf / web . xml ?

为了解决这个问题,我给我的web文件夹命名为webapp,并把它放在src/main中。Maven似乎默认在/ src / main / webapp / web - inf / web . xml中查找web.xml。如果这样做,则不需要显式地告诉maven web.xml的位置。如果你改变了你的文件夹结构,你的构建最近停止工作,这可能是原因。

注意:这是一个旧帖子,但发布的解决方案并没有指出为什么一个正在工作的构建会突然停止工作。

我在测试服务器上有同样的错误,但不是在本地。几分钟后,我发现IDE没有与pom.xml同步。以下是我的解决方法:

使用Eclipse重新生成部署描述符

  1. 右键单击项目文件夹
  2. 在上下文菜单中选择“Java EE工具”,然后选择“生成部署描述符存根” 李Generate web.xml < / >
  3. 将创建web.xml文件。 李web.xml in project structure < / >

使用IntelliJ重新生成部署描述符

  1. 右键单击项目文件夹
  2. 在上下文菜单中,选择“打开模块设置”,然后单击+添加web部署描述符。 李生成部署描述符 < / >
  3. 然后你可以改变描述符的路径或__abc1和右边的。
  4. 然后你会得到类似项目结构的东西

发生此错误是因为您告诉Maven将文件打包到战争

<packaging>war</packaging>

你真的需要战争吗?如果没有,把罐子放在那里。 以下是完整代码:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>
<version>1.0-SNAPSHOT</version>


<groupId>com.your.groupid</groupId>
<artifactId>artifactid</artifactId>
<packaging>jar</packaging>

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

例如:

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

更多细节请参见博客:https://ankurjain26.blogspot.in/2017/05/error-assembling-war-webxml-attribute.html

这是一个老问题,有很多答案,其中大多数或多或少都会有帮助;然而,有一个非常重要的和仍然相关的点,没有一个答案触及(相反,提供了不同的技巧,使构建成为可能),而且,我认为,无论如何,它的重要性都不低。恰恰相反。 < br > < br > 根据您的日志信息,您正在使用Maven,这是一个项目管理工具,严格遵循约定,过度配置原则

当Maven构建项目时:

  1. 它期望您的项目具有特定的目录结构,以便它知道在哪里期望什么。这被称为Maven's Standard Directory Layout;
  2. 在构建过程中,它还创建了适当的目录结构,并将文件放置到相应的位置/目录中,这与Java EE [web]应用程序的Sun Microsystems Directory Structure Standard一致。

你可以合并很多东西,包括maven插件,更改/重新配置项目根目录,等等,但更好更容易的是遵循默认的约定而不是配置,根据它,(现在是你问题的答案)有一个简单的步骤可以让你的项目工作:只要把你的web.xml放在src\main\webapp\WEB-INF\下,并尝试用mvn package构建项目。

🔴failOnMissingWebXml自2020年

所有其他关于的答案可能都是过时的,因为maven-war-plugin使用的默认值发生变化:

从3.0.1开始,如果项目依赖于Servlet 3.0 API或更新版本,此属性默认为false。

所以你必须做的只有的事情是添加

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>

例子:

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">


<modelVersion>4.0.0</modelVersion>
<groupId>foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.3.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

确保在编译war之前运行mvn install

我在尝试将Spring Boot项目打包为WAR文件并在独立的Tomcat实例中部署该WAR文件时遇到了这条消息。

我使用Spring initializr来生成初始的pom.xml和相关工件。 问题是在那个pom.xml (Spring版本2.6.+)中,这个依赖是存在的
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>

我不得不把它换成这个:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

并且还添加了范围为"提供"

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

当然,也改变了包装

<packaging>war</packaging>

关于包装,有两件事需要注意。

  1. 打包到war文件需要在相应的文件夹中定义web.xml文件,如Catalin Ciolocoiu所述。
  2. 但是将包类型更改为jar将不需要该结构,并且应该立即工作。