Spring boot打war包

前言

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

1. 准备pom

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

<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与扫描路径

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

         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包

    pom加入maven插件    

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

执行如下任意一条

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方式也可以开启,但是相对比较复杂一点。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值