008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required

时间:2019-09-09
本文章向大家介绍008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required,主要包括008-SpringBoot发布WAR启动报错:Error assembling WAR: webxml attribute is required使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、Spring Boot发布war包流程:

1、修改web model的pom.xml

<packaging>war</packaging>

SpringBoot默认发布的都是jar,因此要修改默认的打包方式jar为war

2、修改web model的依赖(dependency)

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!-- 移除嵌入式tomcat插件,或者scope = provided
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
            -->
        </dependency>

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

        <!--<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <scope>provided</scope>
        </dependency>-->

注意:

这里添加了起步依赖spring-boot-starter-web,因此,建议把起步依赖Spring-boot-starter-tomcat的scope设置为provided,原因很简单:我们的项目中可能会使用Filter等Servlet的api;

因此,不建议spring-boot-starter-web中移除嵌入式Spring-boot-starter-tomcat的起步依赖,因为这样就必须再单独添加servet-api的依赖

3、maven编译

启动时报错:Error assembling WAR: webxml attribute is required

很明显:webapp/WEB-INF下找不到web.xml

使用Spring开发,默认把所有的静态资源+界面view都放在resources下了,如下图:

  

  因此,webapp都不复存在了,更何况/WEB-INF和/WEB-INF/web.xml

  解决方案:

            <!-- 没有web.xml文件的情况下构建WAR
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
            -->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
                <configuration>
                    <!--如果想在没有web.xml文件的情况下构建WAR,请设置为false。-->
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

1、使用maven-war-plugin3.0,解决了web.xml不存在无法构建war的问题

2、继续使用maven-war-plugin2.6,添加设置failOnMissingWebXml=false

原文地址:https://www.cnblogs.com/bjlhx/p/11491297.html