部署
war包部署
修改项目pom.xml
<packaging>war</packaging>
排除springboot内嵌tomcat依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope> <!--去掉内嵌tomcat-->
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope> <!--去掉使⽤内嵌tomcat解析jsp-->
</dependency>
在插件中指定⼊⼝类
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<!--增加jvm参数-->
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
<!--指定启动⼊⼝类-->
<mainClass>com.blb.emp.EmpApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
配置启动类
继承SpringBootServletInitializer覆盖configure⽅法参数的当前启动类的class
package com.blb.emp;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
@MapperScan("com.blb.emp.mapper")
//1.继承SpringBootServletInitializer
//2.覆盖configure⽅法
public class EmpApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(EmpApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(EmpApplication.class);
}
}
我这里把项目名改成了emp,可以通过finalshell或者xshell,xftp上传到云服务器
打包测试
把war包放到tomcat的webapps
目录下即可
总结
因为这里配置的内置的tomcat其实我们在前面已经排除了所有就不起作用了
# ⼀旦使⽤war包部署注意:
- 1. application.yml 中配置port context-path 失效
- 2. 访问时使⽤打成war包的名字和外部tomcat端⼝号进⾏访问项⽬
jar包部署
默认就是jar,所有不需要改,但是如果试了war包部署的一定别忘了
<packaging>jar</packaging>
打包
启动jar包
java -jar 对应jar文件名字 nohup &
如果是thymeleaf或者前后端的springboot项目这样已经部署成功了并且是后台运行
jsp的springboot项目部署
因为springboot默认不支持jsp如果用上面直接部署会访问不到404
修改插件版本
<plugins>
<!--版本必须为1.4.2版本-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
</plugin>
</plugins>
注意:springboot :springboot部署jsp时,插件版本必须指定为1.4.2版本,并进⾏jsp
打包配置才可以,其他版本均不⽀持!!!
指定jsp打包配置
<resources>
<!-- 打包时将jsp⽂件拷⻉到META-INF⽬录下-->
<resource>
<!-- 指定resources插件处理哪个⽬录下的资源⽂件 -->
<directory>src/main/webapp</directory>
<!--指定必须要放在此⽬录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
完整版
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包时将jsp⽂件拷⻉到META-INF⽬录下-->
<resource>
<!-- 指定resources插件处理哪个⽬录下的资源⽂件 -->
<directory>src/main/webapp/WEB-INF</directory>
<!--指定必须要放在此⽬录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>