springboot的打包方式有很多种。有打成war的,有打成jar的,也有直接提交到github,通过jekins进行打包部署的。
这里主要介绍如何打成jar进行部署。不推荐用war,因为springboot适合前后端分离,打成jar进行部署更合适。
JAR
1、application.properties当中配置端口
server.port=8088
server.tomcat.uri-encoding=UTF-8
#指定context path 2.0前写法
# server.context-path=/gift
#指定context path 更新到2.0后写法
server.servlet.context-path=/
2、POM.xml配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xxxx</groupId>
<artifactId>dbcenter</artifactId>
<version>0.0.1</version>
<name>dbcenter</name>
<description>dbcenter</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.xxxx.dbcenter.DbcenterApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
注意build部分,否则打包成JAR会经常说找不到主类
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.xxxx.dbcenter.DbcenterApplication</mainClass>
</configuration>
</plugin>
3、启动类配置DbcenterApplication.java
启动类当中加上extends SpringBootServletInitializer并重写configure方法,这是为了打包springboot项目用的
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class DbcenterApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(DbcenterApplication.class, args);
}
@Override//为了打包springboot项目
protected SpringApplicationBuilder configure(
SpringApplicationBuilder builder) {
return builder.sources(this.getClass());
}
}
然后按照顺序运行mvn clean再mvn install
然后到这个jar的根目录下执行java -jar dbcenter-0.0.1
(执行方式windows和linux上都一样)
注意,需要在服务器上将对应的端口开放
WAR
用maven创建的springboot项目,默认是jar包,springboot还有自己带的tomcat,因此要调整成WAR需要做以下调整
1、POM.xml
- 将jar修改为war
<packaging>war</packaging>
- 将
build
节点中的finalName
修改为server.context-path中的路径。就是项目名称<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> <finalName>xxxx</finalName> </build>
-
在
spring-boot-starter-web
依赖中移除tomcat模块
<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>
-
添加servlet依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
2、启动类(需要和之前的启动类在同一级目录)
public class SpringBootStartApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 注意这里要指向原先用main方法执行的Application启动类
return builder.sources(DemoApplication.class);
}
}
3、打包
mvn clean package -Dmaven.test.skip=true
部署到tomcat的webapps目录下面