我得到以下错误:
装配WAR: webxml属性是必需的(如果以更新模式执行,则需要预先存在的WEB-INF/web.xml)
我已经得到web.xml在正确的地方,这是projectname\src\main\webapp\WEB-INF\web.xml
web.xml
projectname\src\main\webapp\WEB-INF\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
src/main/webapp
我的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的新文件夹
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同步。以下是我的解决方法:
发生此错误是因为您告诉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
当Maven构建项目时:
Maven's Standard Directory Layout
Sun Microsystems Directory Structure Standard
你可以合并很多东西,包括maven插件,更改/重新配置项目根目录,等等,但更好更容易的是遵循默认的约定而不是配置,根据它,(现在是你问题的答案)有一个简单的步骤可以让你的项目工作:只要把你的web.xml放在src\main\webapp\WEB-INF\下,并尝试用mvn package构建项目。
src\main\webapp\WEB-INF\
mvn package
所有其他关于的答案可能都是过时的,因为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。
mvn install
我在尝试将Spring Boot项目打包为WAR文件并在独立的Tomcat实例中部署该WAR文件时遇到了这条消息。
<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>
当然,也改变了包装
关于包装,有两件事需要注意。
war
jar