spring_boot 釋出成war包 ,部署到外部的tomcat

Muroidea發表於2016-06-30

由jar變成war

<packaging>jar</packaging>

如果是上面的打包方式,啟動方式則為

 mvn package
 java -jar target/mymodule-0.0.1-SNAPSHOT.jar

改變成war

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- ... -->
    </dependencies>
</project>

只需要把tomcat的範圍改成provided

如果要釋出到外部的tomcat同時需要改變啟動方式

新增ServletInitializer類

import org.springframework.boot.builder.SpringApplicationBuilder;  
import org.springframework.boot.context.web.SpringBootServletInitializer;  

public class ServletInitializer extends SpringBootServletInitializer {  

    @Override  
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {  
        return application.sources(Application.class);  
    }  

}  

Application.class 為標註有@SpringBootApplication的主啟動類

注意的問題

此時打成的包的名稱應該和application.properties的
server.context-path=/spring-boot
保持一致

<build>
    <finalName>spring-boot</finalName>
</build>

如果不一樣釋出到tomcat的webapps下上下文會變化

相關文章