本文演示使用maven
自带的profile
功能实现多环境的打包,配合jenkins
一键使用不同的配置文件发布到不同的环境中。
1. 创建SpringBoot测试项目
2. 创建多环境配置文件
创建三个环境使用不同的环境变量
即测试环境使用 application-test.properties
,
开发环境使用application-dev.properties
,
正式环境使用 application-prod.properties
。
然后原来的application.properties
内写
1
spring.profiles.active=@springboot.profileName@
springboot.profileName
也可以换成其他字符串,只需要和下面配置的`pom文件中国相同即可。
3. 配置多环境maven
pom
内配置如下,配置多个profile
,properties
中配置不同的springboot.profileName
值。
并设置dev
为默认环境。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<profiles>
<profile>
<id>dev</id>
<properties>
<springboot.profileName>dev</springboot.profileName>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<springboot.profileName>test</springboot.profileName>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<springboot.profileName>prod</springboot.profileName>
</properties>
</profile>
</profiles>
4. 配置打包资源过滤
将下面配置放入build
节点下, filtering
的作用是过滤配置文件中的文本,将 @xx@
标记的配置替换为对应的maven环境变量。不加filtering
的话application.properties
内的@springboot.profileName@
不会被替换为对应的test
,dev
,prod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<resources>
<!--先排除掉所有的application*.properties-->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
<!--再将对应配置的properties包含在内-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>application-${springboot.profileName}.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
5.尝试打包
输入下面命令,得到只包含dev
配置的jar
包,说明dev
环境是默认的环境。
1
mvn clean package
application.properties
内的@springboot.profileName@
已被替换成dev
输入下面命令,查看打包的jar包,被替换成test环境对应的配置文件。
1
mvn clean package -P=test
6. 整合jenkins
上面已经能做到不同mvn
打包命令得到含有不同环境配置的jar
包功能。只需配置下jenkins
即可。
1
2
3
4
不同环境的打包命令
mvn clean package -P=test
mvn clean package -P=prod
mvn clean package
只需在jenkins
配置的 Build -> Goals and options
里面输入对应的打包命令就能以不同的打包参数打包。
如打包test
环境命令为 (比上面的少了 mvn
)
1
clean package -P=test
7. 完整pom配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?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.2.5.RELEASE</version>
</parent>
<groupId>com.mavenpackagedemo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<properties>
<java.version>1.8</java.version>
</properties>
<profiles>
<!--配置dev为默认环境-->
<profile>
<id>dev</id>
<properties>
<springboot.profileName>dev</springboot.profileName>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<springboot.profileName>test</springboot.profileName>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<springboot.profileName>prod</springboot.profileName>
</properties>
</profile>
</profiles>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<!--先排除掉所有的application*.properties-->
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application*.properties</exclude>
</excludes>
</resource>
<!--再将对应配置的properties包含在内-->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>application-${springboot.profileName}.properties</include>
</includes>
<!--允许过滤资源-->
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>