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>