Maven的profile使用详解
来源:互联网 发布:手机注册淘宝账号申请 编辑:程序博客网 时间:2024/06/03 14:41
背景
一个项目常常有很多发布环境,比如说测试环境,预发布环境,正式环境等等。而不同的发布环境里面需要配置的信息是有区别的,比如说测试环境需要连接测试DB的url和用户名密码,正式环境就需要连接正式DB的url和用户名密码。如果每次部署项目都需要改pom配置文件的话,不但麻烦而且有可能会改错和漏改。
这时候就需要用到pom的profile标签了。
profile标签的工作流程
1,一个环境配置一个profile标签,在里面配置环境信息
下面例子有本地开发环境,测试环境和正式环境共三个环境的配置信息,不同的配置里面有不同的redis服务器连接配置。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><!--<dependencies>等等配置,省掉 --><profiles> <!-- 本地开发环境(Development) --> <profile> <id>Development</id> <properties> <!-- redis --> <redis.host>192.168.14.73</redis.host> <redis.port>6379</redis.port> <redis.auth></redis.auth> <redis.select></redis.select> </properties> <!-- 打包时不指定profile id,默认激活本环境 --> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <!-- 测试环境(TEST) 提供给测试团队的--> <profile> <id>Test</id> <properties> <!-- redis --> <redis.host>192.168.14.200</redis.host> <redis.port>6379</redis.port> <redis.auth></redis.auth> <redis.select></redis.select> </properties> </profile> <!-- 正式生产环境(Production) --> <profile> <id>Production</id> <properties> <!-- redis --> <redis.host>192.168.14.206</redis.host> <redis.port>6379</redis.port> <redis.auth>password</redis.auth> <redis.select></redis.select> </properties> </profile></profiles>
2,java代码中想要取得profile中的配置的话,要通过.properties文件来读取
例redis.properties
#redis服务器redis.host=${redis.host}redis.port=${redis.port}redis.auth=${redis.auth}redis.select=${redis.select}
3,.properties文件怎么才能取到pom文件中profile定义的值呢,需要在pom文件中增加filtering标签,加在build标签内
<build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/test/java</testSourceDirectory> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <excludes> <exclude>**/*.java</exclude> </excludes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <testResources> <testResource> <directory>src/test/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <excludes> <exclude>**/*.java</exclude> <exclude>**/*.class</exclude> </excludes> <filtering>true</filtering> </testResource> <testResource> <directory>src/test/resources</directory> <filtering>true</filtering> </testResource> </testResources> <!--<plugins>省掉 --></build>
4,java读取.properties文件,使用ResourceBundle就可以了
ResourceBundle resourceBundle = ResourceBundle.getBundle("redis");//redis配置信息String host = resourceBundle.getString("redis.host");//redis服务器hostString port = resourceBundle.getString("redis.port");//redis服务器端口String auth = resourceBundle.getString("redis.auth");//redis服务器认证信息String select = resourceBundle.getString("redis.select");//redis数据库
5,打包的时候通过-P指定用哪一个profile的信息来编译打包
`mvn package -PprofileId `
0 0
- Maven的profile使用详解
- Maven Profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- maven profile的使用
- Maven之Profile详解
- Maven与Spring profile的结合使用
- Maven的filtering和profile标签使用
- Maven使用之profile
- maven profile 使用
- maven的profile设置
- maven的profile
- maven的profile介绍
- Odoo ORM API(五)- Fields
- 微信双11活动预热草稿
- 第七周项目4-队列数组
- mapreduce的reduce输出文件进行压缩
- SQL那些事儿(六)--数据库三大范式
- Maven的profile使用详解
- 全局搜索的两种方式
- [李景山php]每天TP5-20161211|App.php-1
- android studio快捷键
- Harris角点检测器
- css span原来是行内元素
- atoi 函数
- .Net调用Java的WebService之亲身体验 (转自梦幻Dot Net)
- 位运算的威力