maven pom.xml加载不同properties配置[转]
2016-10-27 09:36
399 查看
可以参考http://www.openwebx.org/docs/autoconfig.html
1.pom.xml
===========================
<!-- 不同的打包环境配置: test=开发/测试测试环境, product=生产环境; 命令行方式: mvn clean install -Dmaven.test.skip=true -Ptest 或 -Pproduct-->
<profiles>
<!-- 开发/测试环境,默认激活 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默认启用的是dev环境配置-->
</activation>
</profile>
<!-- 预发布环境 -->
<profile>
<id>preproduction</id>
<properties>
<env>preproduction</env>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<build>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/filter-${env}-env.properties</filter>
</filters>
<resources>
<resource> <!-- 配置需要被替换的资源文件路径, db.properties 应该在 src/main/resource 目录下 -->
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build>
2.src/main/filters/下不同环境的配置文件
src/main/filters/filter-preproduction-env.properties
src/main/filters/filter-production-env.properties
src/main/filters/filter-test-env.properties
======filter-test-env.properties 举例
jdbc.url=jdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=testuser
jdbc.password=123456
3.src/main/resources下要应用处理的文件
src/main/resources/conf/db.properties
======db.properties
jdbc.datasource=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.minPoolSize=10
jdbc.maxPoolSize=50
jdbc.autoCommit=false
-----------------------------------
自定义配置属性:
${my.filter.value } will result in hello if you inserted the above XML fragment in your pom.xml
再比如:在pom.xml中properties自定义如下:
Xml代码
<properties>
<springframework_version>3.1.0.RELEASE</springframework_version>
<maven.bbs.appConfig>itConfig.xml</maven.bbs.appConfig>
</properties>
在web容器加载的时候,假如spring监听的配置文件是:applicationContext.xml,那么applicationContext.xml文件内容可以这样写来引入maven的properties属性值:
<import resource="${maven.bbs.appConfig}"/>
maven编译过后,targer项目里面的applicationContext.xml里面相应的引入就会变成
<import resource="itConfig.xml"/>
1.pom.xml
===========================
<!-- 不同的打包环境配置: test=开发/测试测试环境, product=生产环境; 命令行方式: mvn clean install -Dmaven.test.skip=true -Ptest 或 -Pproduct-->
<profiles>
<!-- 开发/测试环境,默认激活 -->
<profile>
<id>test</id>
<properties>
<env>test</env>
</properties>
<activation>
<activeByDefault>true</activeByDefault><!--默认启用的是dev环境配置-->
</activation>
</profile>
<!-- 预发布环境 -->
<profile>
<id>preproduction</id>
<properties>
<env>preproduction</env>
</properties>
</profile>
<!-- 生产环境 -->
<profile>
<id>production</id>
<properties>
<env>production</env>
</properties>
</profile>
</profiles>
<build>
<filters> <!-- 指定使用的 filter -->
<filter>src/main/filters/filter-${env}-env.properties</filter>
</filters>
<resources>
<resource> <!-- 配置需要被替换的资源文件路径, db.properties 应该在 src/main/resource 目录下 -->
<directory>src/main/resources</directory>
<filtering>true</filtering> <!-- 是否使用过滤器 -->
</resource>
</resources>
</build>
2.src/main/filters/下不同环境的配置文件
src/main/filters/filter-preproduction-env.properties
src/main/filters/filter-production-env.properties
src/main/filters/filter-test-env.properties
======filter-test-env.properties 举例
jdbc.url=jdbc:mysql://192.168.120.220:3306/testdb?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
jdbc.username=testuser
jdbc.password=123456
3.src/main/resources下要应用处理的文件
src/main/resources/conf/db.properties
======db.properties
jdbc.datasource=com.mchange.v2.c3p0.ComboPooledDataSource
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=${jdbc.url}
jdbc.username=${jdbc.username}
jdbc.password=${jdbc.password}
jdbc.minPoolSize=10
jdbc.maxPoolSize=50
jdbc.autoCommit=false
-----------------------------------
自定义配置属性:
Custom properties in the POM
User defined properties in the pom.xml.<project> ... <properties> <my.filter.value>hello</my.filter.value> </properties> ... </project>
${my.filter.value } will result in hello if you inserted the above XML fragment in your pom.xml
再比如:在pom.xml中properties自定义如下:
Xml代码
<properties>
<springframework_version>3.1.0.RELEASE</springframework_version>
<maven.bbs.appConfig>itConfig.xml</maven.bbs.appConfig>
</properties>
在web容器加载的时候,假如spring监听的配置文件是:applicationContext.xml,那么applicationContext.xml文件内容可以这样写来引入maven的properties属性值:
<import resource="${maven.bbs.appConfig}"/>
maven编译过后,targer项目里面的applicationContext.xml里面相应的引入就会变成
<import resource="itConfig.xml"/>
<build>
<!-- autoconfig with antx.properties --> <filters> <filter>${filterFile}</filter> </filters> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <properties> <filterFile>antx.properties</filterFile> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles> 现在发现有两种可以实现配置 一种是使用filter 第二种是使用
autoconfig-maven-plugin 来产生autoconfig
相关文章推荐
- maven pom.xml加载不同properties配置
- maven pom.xml加载不同properties配置
- maven pom.xml加载不同properties配置
- maven pom.xml加载不同properties配置
- Maven pom.xml加载不同properties配置
- maven setting.xml 多仓库配置(插件依赖来自不同私服,须保证pom.xml中插件版本的唯一性)
- Maven 配置篇之 pom.xml
- spring.xml 配置加载jdbc.properties和ehcache.xml 的方法
- maven 配置篇 之pom.xml
- Maven系列--pom.xml 配置详解
- 大型Java项目中使用maven进行管理,pom.xml文件中build的配置
- maven 配置篇 之pom.xml
- Maven 配置篇之 pom.xml
- maven 配置篇 之pom.xml
- maven 配置篇 之pom.xml
- Maven教程初级篇02:pom.xml配置初步
- Maven系列2--pom.xml 配置详解
- maven 配置之pom.xml(2)
- [使用心得]maven2之m2eclipse使用手册之三第一个Simple MavenProject与Pom.xml配置说明
- maven 配置篇 之pom.xml(二)