我得到以下错误:

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

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

是什么导致了这种情况?


当前回答

我在尝试将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>

并且还添加了范围为"provided"的依赖项

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

当然,也改变了包装

<packaging>war</packaging>

其他回答

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

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

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

右键单击您的项目-> Java EE工具->生成部署描述符存根

这将生成web.xml

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

如果你改变默认的项目路径,你必须指定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>

我在尝试将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>

并且还添加了范围为"provided"的依赖项

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

当然,也改变了包装

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

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