Maven打包时自动选择不同的配置(利用profile和filter插件)
来源:互联网 发布:淘宝头层牛皮是真的吗 编辑:程序博客网 时间:2024/06/05 08:17
项目开发时,一般都会部署到两套以上不同的环境中(比如开发环境和生产环境)。打包的时候需要修改配置文件中的很多信息、或替换不同的配置文件,很容易出错而且不方便。下面举例介绍如何利用Maven的profile和filter插件来解决这个问题——为不同环境打包前自动修改配置文件中的内容:
假设有两个环境:
- 开发环境
- 生产环境
第一步:为两个不同的环境建立两个配置文件,作为其他配置文件实际内容的来源
- development.properties,内容为:
db.url=development-url
- production.properties,内容为:
db.url=production-url
第二步:在其他公用配置文件(.properties, .xml, etc.)中,使用${}来引用上面定义的值,如:
db.url=${db.url}<bean id="testService" class="com.xjj.service.impl.TestServiceImpl"> <property name="url" value="${db.url}"></property></bean>
第三步:在POM文件中定义两个profile
<project>... <!-- 选择不同的属性为resource中的变量赋值。development:开发环境,production:生产环境 --> <profiles> <profile> <id>development</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <filters> <filter>src/main/resources/profile/development.properties</filter> </filters> </build> </profile> <profile> <id>production</id> <build> <filters> <filter>src/main/resources/profile/production.properties</filter> </filters> </build> </profile> </profiles>...</project>
第四步:在POM文件中定义需要被替换的文件,并使用filter
<project>... <build> ... <!-- 启用filtering功能为resources中的变量赋值 --> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build></project>
第五步:在MyEclipse中添加两个Maven Build快捷方式
Run -> Run Configurations... -> Maven Build
Base directory: Browse Workspace...选择当前项目
Goals中填入:
- 开发环境:package -Pdevelopment
- 生产环境:package -Pproduction
如果要先clean并且跳过测试
- 开发环境:clean package -Pdevelopment -Dmaven.test.skip=ture
- 生产环境:clean package -Pproduction -Dmaven.test.skip=ture
第六步:打包
分别运行这两个Maven Build,查看生成的jar或war包,就可以看到第二步配置文件中的变量已经被替换成development.properties或production.properties中的内容了。
注1:如果其他目录中有需要打包的xml文件(比如MyBatis的mapper.xml文件),需要另外指定,不然Maven默认不会打包:
<resources>
…… <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource></resources>
注2:如果resources文件夹中有二进制文件,需要排除在filter之外(先exclude,后面再单独在一个resource中声明),不然可能会搞坏它:
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.p12</exclude> </excludes> </resource> <resource> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>**/*.p12</include> </includes> </resource></resources>
具体的全部代码请参考:https://github.com/xujijun/MyJavaStudio
(原创文章,转载请注明转自Clement-Xu的博客:http://blog.csdn.net/clementad/article/details/43603441)
0 0
- Maven打包时自动选择不同的配置(利用profile和filter插件)
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件(POM中)
- 使用maven的profile和filter插件管理配置项
- 使用maven的profile和filter插件管理配置项
- 使用maven的profile和filter插件管理配置项
- 使用maven的profile和filter插件管理配置项
- 使用maven的profile和filter插件管理配置项
- 一点一点学maven(13)--利用maven的filter和profile实现不同环境使用不同的配制
- 使用maven的profile和filter插件管理配置项--不同环境(dev,beta,production)轻松切换
- 使用maven的profile和filter插件管理配置项--不同环境(dev,beta,production)轻松切换
- 使用maven的profile和filter插件管理配置项--不同环境(dev,beta,production)轻松切换
- 利用maven的filter和profile实现不同环境使用不同的配制
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- Maven中实现多环境的配置profile和filter
- Maven 利用Profile属性配置,生成不同环境(生产、测试)下的发布包
- 在iOS7中修改状态栏字体的颜色
- (二十六)静态单元格(Cell)
- 把英语口头禅变作自己的口头禅
- android Handler的使用
- Android 解决某些机型CheckBox自定义按钮图片时发生的错位问题
- Maven打包时自动选择不同的配置(利用profile和filter插件)
- 算法训练 操作格子
- POJ 1056 IMMEDIATE DECODABILITY
- 为js对象创建命名空间
- JS常用正则表达式
- 好的文本编辑器
- 今年暑假不AC
- 介绍ucenter整合思路以及承接iwebshop二次开发
- 侧滑效果,fragment嵌套时遇到错误:The specified child alred has a parent...