项目开发经常碰到配置文件中测试的jdbc.url和线上的是不一样的。为此每次打包都要修改配置文件,很烦,而且也容易出错。
最近才发现,原来用maven的profile可以实现多种环境的配置。
1、在src/main/resources/filters,建两个文件:test.properties和prod.properties。
分别放置测试环境和生产环境的配置。(假设两个文件中都设置了jdbc.url属性)
2、新建src/main/resources/conf.properties文件。里面设置
3、配置pom.xml。配置如下
最近才发现,原来用maven的profile可以实现多种环境的配置。
1、在src/main/resources/filters,建两个文件:test.properties和prod.properties。
分别放置测试环境和生产环境的配置。(假设两个文件中都设置了jdbc.url属性)
2、新建src/main/resources/conf.properties文件。里面设置
- jdbc.url=${jdbc.url}
3、配置pom.xml。配置如下
- <profiles>
- <profile>
- <id>test</id>
- <properties>
- <env>test</env>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- <profile>
- <id>prod</id>
- <properties>
- <env>prod</env>
- </properties>
- </profile>
- </profiles>
- <build>
- <filters>
- <filter>src/main/resources/filters/${env}.properties</filter>
- </filters>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- <includes>
<include>**/*</include>
</includes> - </resource>
- </resources>
- <plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin> - </plugins>
- </build>
4、打包。使用mvn packge -Ptest 即可实现测试环境打包。如果要prod环境配置,只需-Pprod即可。当然pom配置中默认不传-P参数的话是使用test环境配置。