您的位置:首页 > 其它

Maven使用profile 和 filtering实现多种环境下配置打包

2016-06-16 00:00 555 查看
项目开发过程中我们经常定义不同环境的配置文件,如开发环境、测试环境、生产环境。若我们使用一份配置文件在发布的时候进行手动更改,那将是一件非常麻烦痛苦的一件事,这是我们可以借助Maven轻松完成不同环境的打包工作。

1.写三份不同环境的配置文件如下:



2.修改pom.xml文件添加一下内容:

<!-- 全局属性配置 -->
<profiles>
<!--测试环境配置-->
<profile>
<id>test</id>
<properties>
<env>test</env>
<maven.test.skip>false</maven.test.skip>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
<!--发布环境配置-->
<profile>
<id>deploy</id>
<properties>
<env>deploy</env>
<maven.test.skip>false</maven.test.skip>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<includes>
<include>**/{env}/*</include>
<include>*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources/${env}</directory>
<targetPath>${project.build.directory}/classes</targetPath>
<filtering>true</filtering>
</resource>
</resources>
</build>


打包发布:

(1). intellij idea 方式



勾选想要打包的环境后选择package打包

(2). Eclipse方式,右键项目名,Run as ——> Run configurations ——>Maven Build ——>New

Goals: mvn clean package -Ptest

上面是打包为test环境
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: