1. 程式人生 > >Spring Boot將專案打包成war包

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:[埠號]/[打包專案名]/