目的:通常情况下,开发环境、测试环境、产品环境都是不一样的,为了方便部署时不用修改配置文件直接部署,这里提供两种策略。
你需要有一定的maven和Spring Boot的基础。
- 基于使用maven实现
- 创建maven项目
- 添加Spring Boot依赖,pom.xml
<properties>
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
- 配置开发、测试、产品环境
profiles>
<profile>
<!-- 这个ID很有用 -->
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<environment>pro</environment>
</properties>
</profile>
</profiles>
- maven相关插件以配置文件路径等
<build>
<finalName>demo1</finalName>
<filters>
<!-- src/main/filters这个目录需要新建 -->
<filter>src/main/filters/config-${environment}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- spring boot 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
<!-- 这里是启动类 -->
<mainClass>com.demo.Application</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
- 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.demo</groupId>
<artifactId>demo1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<spring-boot.version>2.1.0.RELEASE</spring-boot.version>
</properties>
<profiles>
<profile>
<id>dev</id>
<properties>
<environment>dev</environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<environment>test</environment>
</properties>
</profile>
<profile>
<id>pro</id>
<properties>
<environment>pro</environment>
</properties>
</profile>
</profiles>
<build>
<finalName>demo1</finalName>
<filters>
<filter>src/main/filters/config-${environment}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 打包时跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
<!-- spring boot 打包插件 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
<mainClass>com.platform.PlatfomApplication</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
</project>
- 新建目录src/main/filters,并在该目录下新建文件
config-dev.properties
_server.port = 9999
config-test.properties
_server.port = 8888
config-pro.properties
_server.port = 80
- 添加启动类,com.demo.Application
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@SpringBootApplication
@Controller
public class Application {
@ResponseBody
@RequestMapping("/test")
public String test() {
return "hello world";
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- src/main/resources下新建application.yml
server:
port: ${_server.port} # ${},这里面的内容是config-xxx.properties里面的key
- 右键 >> debug,浏览器访问http://127.0.0.0:9999/test,返回“hello world”表示成功。
- mvn打包,打包完成后在target目录下会存在一个可执行jar
# 开发环境包,默认
mvn clean package -Pdev
# 测试包
mvn clean package -Ptest
# 产品包
mvn clean package -Ppro
- Java运行,不一样的命令打出来的使用的是不用的配置文件。
java -jar demo.jar
- 基于Spring Boot实现,其实也属于maven项目
- 新建maven项目
- 引入Spring Boot依赖
- maven插件,这里只需要引入插件就行,就是里面的内容
- 在src/main/resources下新建文件
application.yml
server:
port: 9999
application-dev.yml
server:
port: 9999
application-test.yml
server:
port: 8080
application-pro.yml
server:
port: 80
- maven打包
mvn clean package
- 执行
java -jar xxx.jar --spring.profiles.active=dev
java -jar xxx.jar --spring.profiles.active=test
java -jar xxx.jar --spring.profiles.active=pro