一、为什么需要构建配置文件
在开发过程中,我们经常会根据不同的环境配置不同的参数,如数据源的ip,username,password、url、秘钥等都会不同,传统方式是在一个配置文件中通过修改properties文件中的参数值或者通过注释解注释来达到目的,这样不仅容易出错,还浪费不必要的时间,更重要的是把代码发布到测试环境或者生产环境还容易忘记改。为解决这种问题,maven提供了一种解决方案,就是profile。
下图为传统方式,需要来回的注释和解注释
二、什么是构建配置文件?
构建配置文件是一组配置的集合,用来设置或者覆盖 Maven 构建的默认配置。使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Development 环境。
Profile 在 pom.xml 中使用 activeProfiles / profiles 元素指定,并且可以用很多方式触发。Profile 在构建时修改 POM,并且为变量设置不同的目标环境(例如,在开发、测试和产品环境中的数据库服务器路径)。
三、Profile 类型
Profile 主要有三种类型。
类型 | 在哪定义 |
---|---|
项目级(Per Project) | 定义在项目的POM文件pom.xml中 |
用户级 (Per User) | 定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml) |
全局(Global) | 定义在 Maven 全局的设置 xml 文件中 (%M2_HOME%/conf/settings.xml) |
四、配置文件激活
Maven的构建配置文件可以通过多种方式激活。
- 使用命令控制台输入显式激活。
- 通过 maven 设置。
- 基于环境变量(用户或者系统变量)。
- 操作系统设置(比如说,Windows系列)。
- 文件的存在或者缺失。
五、实现方式
1、filter方式实现
第一步:分别定义application-dev.properties、application-test.properties、application-pro.properties三个文件
application-dev.properties
env.jdbc.username=dev
env.jdbc.password=123456
application-test.properties
env.jdbc.username=test
env.jdbc.password=888888
application-pro.properties
env.jdbc.username=root
env.jdbc.password=666666
第二步:定义总的属性文件application.properties,该文件中的值去引用application-<env>.properties中的key
// 引用application-<env>中的key
jdbc.username=${env.jdbc.username}
jdbc.password=${env.jdbc.password}
# 公共配置
salt=123456789
第三步:配置profile
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 设置默认激活这个配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!-- 发布环境 -->
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
第四步:配置filter和resource
${env}就是在mvn package -P <env>
的名字,这样就告诉application.properties中应用的key是那个属性文件的key了
<build>
<finalName>profile-app</finalName>
<!-- 定义了变量配置文件的地址 -->
<filters>
<filter>src/main/resources/config/application/application-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
打包运行
// 如果不指定环境,默认是activeByDefault=true的环境,当前是指开发环境
mvn package
// 打包指定的环境通过-P 参数,注意p是大写的
mvn package -P <env>
从mvn packege -P test运行的结果中可以看到生成的target目录下classes/application.perperties中的jdbc.username和jdbc.password 就是application-test.properties中配置的env.jdbc.username和env.jdbc.password的值。
在spring中如果要使用属性配置文件,直接引入这个总的配置文件即可,其他的环境配置文件的使命已经结束了。 <context:property-placeholder location="classpath:application.properties"/>
实现原理:
在pom.xml中为每个不同的环境定义不同的profile,每个profile都有一个环境名称,然后为不同环境定义不同的配置文件(如application-<env>.properties
), 再定义一个总的属性文件(如application.properties), 然后让application.properties的value去引用application-<env>.properties
中对应的key,在打包时指定要打包的环境的名称即可,这样application.properties中的key的值就是相对应环境application-<env>.properties对应的值了。
2.多resource实现方式
步骤
第一步:在src/main/resource创建一个env目录,再创建各个环境的子目录,再再各个环境子目录下创建名为config.properties的文件,每个键相同,值不同。
env/dev/config.properties
jdbc.username=dev
jdbc.password=123456
env/test/config.properties
jdbc.username=test
jdbc.password=888888
env/pro/config.properties
jdbc.username=root
jdbc.password=666666
第二步:创建一个与环境无关的application.properties
# 公共配置
salt=123456789
第三步:配置profiles
<profiles>
<profile>
<!-- 开发环境 -->
<id>dev</id>
<properties>
<env>dev</env>
</properties>
<activation>
<!-- 设置默认激活这个配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 测试环境 -->
<id>test</id>
<properties>
<env>test</env>
</properties>
</profile>
<profile>
<!-- 发布环境 -->
<id>pro</id>
<properties>
<env>pro</env>
</properties>
</profile>
</profiles>
第四步:配置resource
<build>
<finalName>profile-app</finalName>
<!-- 定义了变量配置文件的地址 -->
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>env/dev/*</exclude>
<exclude>env/test/*</exclude>
<exclude>env/pro/*</exclude>
</excludes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/env/${env}</directory>
<includes>
<include>*.*</include>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
第五步:运行 mvn package -P test
3.两种方式比较
filter方式会把所有的application-dev.properties、application-test.properties、application-pro.properties文件都会打包进去,而且此种方式只能针对属性文件,如果有其他文件(如.xml)也根据不同的环境有不同的配置,这种方式是不好处理。
多resource方式在打包时只打包指定环境的配置文件,可以将各种文件放到各自的环境文件夹中,在打包的时候会将整个文件夹都打包进去。推荐此种方式