Spring boot打war包

前言html

        最近在处理旧业务须要war部署,并准备升级框架spring boot,就须要Spring boot打一个war,发布到tomcat。java

1. 准备pomweb

依赖spring boot,去除spring boot starter web内置的嵌入式tomcat,但程序依赖JavaEE的jar能够使用provided依赖。        spring

<packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.3.RELEASE</version>
            <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>
            <version>2.1.3.RELEASE</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

2. 配置war的加载main与扫描路径apache

        写一个main方法,spring boot标准模式tomcat

         war运行须要指定servlet初始化的资源路径,继承SpringBootServletInitializer,覆写configure方法。框架

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class WarMain extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(WarMain.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(this.getClass());
    }
}

3. 打war包maven

    pom加入maven插件    ide

<build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

执行以下任意一条spring-boot

mvn clean package -Dmaven.test.skip=true     #跳过测试执行,但test代码会编译
mvn clean package -DskipTests                #跳过测试执行,跳过测试代码的编译

报错了

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project tomcat-remote: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode)

意思是缺web.xml,或者预先准备web.xml,可是spring boot自己已经不须要咱们写web.xml。

可经过2种方式解决

3.1 升级maven插件版本

默认使用2.2版本,升级3.0.0便可

<build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.0.0</version>
            </plugin>
        </plugins>
    </build>

3.2 配置规避

 <failOnMissingWebXml>false</failOnMissingWebXml>

<build>
        <finalName>ROOT</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>

按如上打成war,扔到tomcat下,便可运行spring boot项目 

总结

       Spring Boot通常使用jar方式部署,仅在比较特殊的时候使用war,使用war部署JVM的参数须要在tomcat下修改

       tomcat部署对于使用apr方式的IO很是方便,IO效率高,Spring boot方式也能够开启,可是相对比较复杂一点。