1. 程式人生 > >SpringBoot專案部署到外部Tomcat中的方法

SpringBoot專案部署到外部Tomcat中的方法

SpringBoot專案部署到外部Tomcat中的方法

1. 特別說明

  • 由於SpringBoot預設訪問無需專案名,所以打包成war的SpringBoot專案也需要部署成不需要輸入專案名的方式,否則資源無法訪問,後面會給部署示例

pom.xml調整

1.1 打包方式修改

<packaging>jar</packaging>

變更為

<packaging>war</packaging>

1.2. 新增依賴

重點:scope是provided

<dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>

1.3. 排除嵌入式tomcat

不排除也沒影響,多個jar而已

<dependency>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency>

1.4. 註釋spring-boot-maven-plugin,使用maven-war-plugin

<!-- 
<plugin>
    <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>bing.AuthorityApplication</mainClass> <layout>ZIP</layout> </configuration> <executions> <execution> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin>

2. 啟動類修改

繼承SpringBootServletInitializer覆蓋configure方法,示例:

public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(Application.class); } public static void main(String[] args) { new SpringApplicationBuilder(Application.class).web(true).run(args); } }

3. 省略專案名部署方法

修改TOMCAT的server.xml,demo是專案資料夾

<Host appBase="webapps" autoDeploy="true" name="localhost" unpackWARs="true"> ... <Context path="" docBase="D:/opt/demo" debug="0" reloadable="true"/> ... </Host>