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

 

搞定!

 

================================華麗的分割線====================================

但是,強烈不建議使用SpringBoot發布war包,原因有三:

1、默認的SpringBoot支持靜態資源以jar包的方式發布部署

2、前后端分離,后端使用SpringBoot開發,前段就無所謂了,完全可以不依賴SpringBoot

3、在服務端加入Swagger插件,直接通過接口做測試,無需web界面

 

 

參考:

 spring boot 用war包部署到tomcat下詳細教程


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM