我得到以下错误:

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

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

是什么导致了这种情况?


当前回答

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

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

其他回答

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

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

<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>

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

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