maven 利用 profile 进行多环境配置
我们在进行项目的多环境配置时,有很多种方式供我们选择,比如 SpringBoot 自带的 application-dev.yml、maven 的 profile 等。这里介绍的就是如何利用 profile 进行多环境配置。
首先,在 pom.xml 中添加你需要的 profile 配置:
dev
dev
publish
publish
local
local
true
profiles 里面配置了多个 profile 文件,即 dev、publish、local 环境,
接下来,我们要将
Whether resources are filtered to replace tokens with parameterised values or not.
The values are taken from the properties element and from the properties in the files listed
in the filters element.
src/main/resources/profiles/${env}/env.properties
src/main/resources
**/**
profiles/**
true
前面说到
server:
port: 8080
tomcat:
max-threads: 800
uri-encoding: UTF-8
spring:
redis:
host: ${spring.redis.host}
timeout: ${spring.redis.timeout}
pool:
max-idle: ${spring.redis.pool.max-idle}
max-active: ${spring.redis.pool.max-active}
password: ${spring.redis.password}
database: ${spring.redis.database}
那么问题来了,maven 怎么认识到 ${*} 这个符号是标记呢?这个标记符号是在
org.apache.maven.plugins
maven-resources-plugin
UTF-8
${*}
@
false
xlsx
store
因此,整个流程应该是这样进行的:执行 maven compile 命令,
最后,只剩下一个问题了,怎么切换环境呢?如果你开发的工具是 IDEA,直接在旁边窗口切换即可:
如果使用命令行编译,加上 -P 选择 profile 即可,如下:
clean -U package -P dev -DskipTests=true -f pom.xml