使用maven profile实现多环境可移植构建 http://blog.csdn.net/mhmyqn/article/details/24501281
一、配置profile
首先是profile配置,在pom.xml中添加如下profile的配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<profiles> <!-- 配置多个profile --> <!-- mvn clean package -Pprod --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <profiles.active>dev</profiles.active> <ibatis.log>TRACE</ibatis.log> </properties> </profile> <profile> <id>prod</id> <properties> <profiles.active>prod</profiles.active> <ibatis.log>ERROR</ibatis.log> </properties> </profile> </profiles> |
定义两个环境
二、配置文件 针对不同的环境,我们定义不同的配置文件,而这些配置文件都做为资源文件放到maven工程的resources目录下,即src/main/resources目录下,存放的是公共配置文件,不同环境使用不同文件夹包含,比如dev和prod文件夹
三、maven资源插件配置 在pom中的build节点下,配置资源文件的位置,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<resources> <resource> <directory>src/main/resources</directory> <excludes> <exclude>**/*.bak</exclude> <exclude>dev/*</exclude> <exclude>prod/*</exclude> </excludes> <filtering>true</filtering> </resource> <resource> <directory>src/main/resources/${profiles.active}</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> <include>**/*.properties</include> </includes> <filtering>true</filtering> </resource> </resources> |
注意:要在不同配置里面定义profiles.active变量,赋给这个profile的id,处理资源文件的时候,过滤掉dev和prod文件夹,然后在复制相应的profile配置文件,要使用<filtering>true</filtering>,才能覆盖公共文件。
四、构建或发布
打包正式环境
1 |
mvn clean package -Pprod |
或者
打包开发环境
1 |
mvn clean package -Pdev |
遇到的坑
- 要使用<filtering>true</filtering>,才能覆盖公共文件
- 在application.xml文件中不能出现@关键字,就算你注释了也不行。当出现@了,之后的所有环境变量将不会被注入
如: