根据网上的介绍可以使用maven 的profile功能实现不同环境properties文件的配置,
这里我使用filters方式,具体就是:
在application-test.properties文件中添加
- #这是test环境配置
- jdbc_url=jdbc:mysql://192.168.1.180:3306/abcdev
- jdbc_user=test_user
- jdbc_password=test_password
在application.properties中添加
username=${db_username}
password=${db_password}
然后在pom文件中配置
<profiles>
<profile>
<id>test</id>
<properties>
<env>sit</env>
</properties>
</profile>
</profiles>
<build>
<filters>
<filter>src/main/resources/application/application-${env}.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<excludes>
<exclude>application/**</exclude>
<exclude>config/**</exclude>
</excludes>
<filtering>true</filtering>
</resource>
</resources>
</build>
最后执行 mvn clean package -P test,就可以修改application.properties中的属性,实现多环境打包。
但是在使用时application.properties中的属性值死活改不了,然后我新建了一个maven项目在新环境中可以执行成功,真实奇了怪了。我就开始比较两个pom文件的差别,发现老项目使用了spring boot,在pom文件中配置了parent标签
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/>
</parent>
而点开parent的pom文件可以看到已经配置了resource标签
感觉这里可能有冲突,于是使用决定去掉parent标签,改用
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.5.10.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
再次执行mvn clean package -P test,属性设置成功。
看来parent项目中的resource配置会影响到子项目resource的配置,多加注意。