Spring Boot将项目打包成war包

1 修改项目打包类型

在pom.xml里,项目打包类型将jar设置成war:

<packaging>war</packaging>

2 移除内置tomcat容器

在pom.xml里设置:

<dependencies>
        <!--web启动器依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--移除默认启动容器-->
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>

3 添加servlet-api依赖

若项目的某些工具类会用到该依赖,如果缺失,会报错:

/tool/WebUtil.java:[6,26] 程序包javax.servlet.http不存在

需要在pom.xml里添加如下依赖:

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

或者下面依赖(任选其一):

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-servlet-api</artifactId>
    <version>8.0.36</version>
    <scope>provided</scope>
</dependency>

4 修改项目启动类

Spring Boot入口类必须实现SpringBootServletInitializer接口的configure方法才能让外部容器运行Spring Boot项目。

原入口类MainApplication.java内容如下:

package com.maxbill;

import com.maxbill.core.desktop.DesktopApp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;


@SpringBootApplication
@MapperScan("com.maxbill.base.dao")
public class MainApplication extends DesktopApp {

    public static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        //启动后台服务
        context = SpringApplication.run(MainApplication.class, args);
        //启动桌面服务
        launch(args);
    }

}

修改后内容如下:

package com.maxbill;


import com.maxbill.core.desktop.DesktopApp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;       
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
@MapperScan("com.maxbill.base.dao")
public class MainApplication extends SpringBootServletInitializer{
    @Override
    //修改启动类,继承 SpringBootServletInitializer并重写 configure方法
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder){
        //注意这里要指向原先用main方法执行的Application启动类
        return builder.sources(MainApplication.class);
        
    }
    
    public static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        //启动后台服务
        context = SpringApplication.run(MainApplication.class, args);
        //启动桌面服务
        launch(args);
    }

}

5 打包部署项目

maven执行命令跳过测试打包:

mvn clean package -DskipTests

build信息如下:

[INFO] Building war: D:\Workspace\MaxBill-RedisPlus-master\RedisPlus\target\RedisPlus-0.0.1-SNAPSHOT.war
[INFO]
[INFO] --- spring-boot-maven-plugin:2.0.4.RELEASE:repackage (default) @ RedisPlus ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

build成功后,在项目target目录下把war包部署到tomcat的webapps目录下,例如:

D:\Program Files\Apache Software Foundation\Tomcat 8.0\webapps

启动tomcat服务,在浏览器访问:

http://localhost:[端口号]/[打包项目名]/

 

posted @ 2018-09-27 15:15  ycyzharry  阅读(1356)  评论(0编辑  收藏  举报