0%

Gradle中配置SpringBoot环境配置参数

目标

gradle中配置Springboot环境参数,使之能根据参数打包目标环境。

方案

maven对应配置参考

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
<profiles>
<profile>
<id>dev</id>
<properties>
<profileActive>dev</profileActive>
<package.environment>dev</package.environment>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profileActive>prod</profileActive>
<package.environment>prod</package.environment>
</properties>
</profile>
</profiles>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application-${package.environment}.properties</include>
<include>**/application.properties</include>
</includes>
</resource>
</resources>
...
</build>

这里有2个目标:

  1. springboot能获取目前activeProfile
  2. 根据目标环境变量package.environment过滤目标配置资源文件

首先,我们需要用到gradleapply指令来应用一个外部kts配置文件(类似springimport)。

1
2
val buildProfile: String? by project
apply(from = "profile-${buildProfile ?: "test"}.gradle.kts")

这里定义在build.gradle.kts中的buildProfile就是需要我们定义的环境配置变量。我们用一个简单任务来测试一下。

build.gradle.kts中添加测试任务

1
2
3
4
5
6
tasks.register("testProfile") {
val message: String by project.extra
doLast {
println(message)
}
}

添加profile-test.gradle.ktsprofile-prod.gradle.kts

1
val message by extra("Hello, test!")
1
val message by extra("Hello, prod!")

分别执行结果如下:

1
2
3
4
5
6
7
8
9
$./gradlew testProfile
> Task :testProfile
Hello, test!
$./gradlew testProfile -PbuildProfile=prod
> Task :testProfile
Hello, prod!
$./gradlew testProfile -PbuildProfile=test
> Task :testProfile
Hello, test!

这里test就是默认配置,对比于maven中的activeByDefault

接下来我们可以在profile-*.gradle.kts中进行对应环境的特殊化配置。

我们先来完成第一个目标,配置spring.profiles.active。这里以test为例。

build.gradle.kts

1
2
3
4
5
6
7
tasks.withType<Copy> {
from("src/main/resources")
.include("**/application.properties")
.filter { line: String ->
if (line.contains("spring.profiles.active")) "spring.profiles.active=$buildProfile" else line
}
}

application.properties

1
spring.profiles.active=

application.properties中的这一行,我们利用gradle脚本中的Copy任务,将其替换。

1
2
3
$ unzip build/libs/test-0.0.1-SNAPSHOT.jar -d build/libs
$ cat build/libs/BOOT-INF/classes/application.properties
spring.profiles.active=test

这样,我们就能直接run起来而不需要考虑在运行时还手动指定profile了。

第二个目标的实现方式也是类似,借助于Copy任务

profile-test.gradle.kts

1
2
3
4
5
6
tasks {
tasks.withType<Copy> {
from("src/main/resources")
.include("**/application-test.properties")
}
}
1
2
3
4
$ unzip build/libs/test-0.0.1-SNAPSHOT.jar -d build/libs
$ ls build/libs/BOOT-INF/classes/*.properties
application-test.properties
application.properties

运行效果

1
2
$ java -jar build/libs/test-0.0.1-SNAPSHOT.jar
2021-03-25 11:15:41.502 INFO 72542 --- [ main] com.demo.test.TestApplicationKt : The following profiles are active: test

更新

升级到gradle 7.0.2之后会出现一个报错

1
is a duplicate but no duplicate handling strategy has been set

需要在build.gradle.ktsCopy任务中添加如下配置

1
duplicatesStrategy=DuplicatesStrategy.EXCLUDE

参考

how-to-set-system-properties-on-run-in-kotlin-dsl