在开发的过程当中,常常须要面对不一样的运行环境(开发环境、测试环境、准发布环境、生产环境等等),在不一样的环境中,相关的配置通常是不同的,好比数据源配置、用户名密码配置、以及一些软件运行过程当中的基本配置。html
使用Maven来进行构建能够达到不一样环境构建不一样的部署包。在maven中实现多环境的构建可移植性须要使用profile,经过不一样的环境激活不一样的profile来达到构建的可移植性。apache
/** 这里定义了三个环境,local(本地环境)、dev(开发环境)、pro(生产环境), 其中开发环境是默认激活的(activeByDefault为true),这样若是在不指定profile时默认是开发环境 */ <profiles> <profile> <id>local</id> <properties> //这里的env只是一个变量而已,名字能够由你任意来定,这个变量在后面有用到 <env>local</env> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>dev</id> <properties> <env>dev</env> </properties> </profile> <profile> <id>pro</id> <properties> <env>pro</env> </properties> </profile> </profiles> <build> <finalName>mqConsumer</finalName> <filters> //${env}这个变量就是在前面定义过的 <filter>src/main/resources/filters/${env}.properties</filter> </filters> <resources> <resource> //对resources资源文件下的包含${}占位符的变量进行替换 <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
local.propertiesmaven
#activeMQ配置 brokerURL=tcp://192.168.1.55:61616
dev.propertiestcp
#activeMQ配置 brokerURL=tcp://115.29.173.120:61616
pro.propertieside
#activeMQ配置 brokerURL=failover://(tcp://115.29.173.121:61616,tcp://115.29.173.121:61617, tcp://115.29.189.46:61616,tcp://115.29.189.46:61617)``` #### 三、待替换的配置文件(通常位于资源路径下) config.propeties
#activeMQ brokerURL配置 brokerURL=${brokerURL}测试
#### 四、执行maven命令进行打包
//本地环境默认为激活模式,因此可不加-P参数 mvn clean package //dev环境 mvn clean package -Pdev //pro环境 mvn clean package -Pproui
//执行命令后,能够看到最后的部署包里面的配置文件配置就是对应环境须要的code
ref: [http://maven.apache.org/guides/introduction/introduction-to-profiles.html](http://maven.apache.org/guides/introduction/introduction-to-profiles.html)