Profile按环境打包

本文转自:https://mp.weixin.qq.com/s/dXuCWQ_I6AVpV80ho-6b5A    

在日常开发中,我们项目的开发环境和生产环境以及测试环境往往是不同的,比如:数据库的url等。在项目上生产环境时,就需要修改这些参数,给开发造成不便。为了解决该问题,Maven 2.0引入了构建配置文件的概念(build profiles)。

即按照不同的环境配置相应的文件,假如你的生产环境和开发环境所需环境配置不同,生产环境配置文件是prod.properties,开发环境配置文件是dev.properties,那么用maven profile ,你可以实现打包开发环境jar包的时候只将dev.properties打包并使用,生产环境打包同理。

在哪里声明呢?

它可以在每个项目的pom.xml文件中声明,也可以在maven的用户setting.xml下声明,也可以在maven全局环境下设置setting.xml,详情如下。

1.Per Project
Defined in the POM itself (pom.xml).

2.Per User
Defined in the Maven-settings(%USER_HOME%/.m2/settings.xml)

3.Global
Defined in the globalMaven-settings (${maven.home}/conf/settings.xml)

4.Profile descriptor
不支持3.0,详情请看:

https://cwiki.apache.org/MAVEN/maven-3x-compatibility-notes.html#Maven3.xCompatibilityNotes-profiles.xml

虽然有这么多define的方式,但是我们一般使用的是第一种defined in the pom,因为不见得每个项目的生产环境都一模一样,当然这个也是因个人情况而异。

项目结构

pom.xml

三个application.properties

打包:

可以看到只将pro/application.properties进行了编译。

本教程中的项目源码:

https://github.com/fantj/maven-tutorial

猜你喜欢

转载自blog.csdn.net/qq_34178998/article/details/85256614