Maven profile介绍

本文介绍Maven中的Profile概念,用于实现项目的环境配置管理。通过多种方式激活Profile,如命令行、settings文件、系统属性等,并解释如何判断哪些Profile被激活。

同一个项目,在不同的环境中部署可能需要不同的配置信息,典型的比如数据库的配置,在开发环境需要连接本地的数据库,测试环境需要连接测试环境的数据库,线上环境需要连接线上的数据库。要使一个项目不经过任何修改就在不同的环境写运行是基本不可能的。为了能让一个项目在不同的环境下方便地移植,Maven引入了profile的概念,profile能够在构建项目的时候修改pom文件的一个子集,或者添加一些额外的配置元素。用户可以使用多种方式激活profile,以实现在不同环境下的移植。

profile长啥样

下面的代码引入了一个针对开发环境和测试环境的profile

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://localhost:3306/test</db.url>
            <db.username>dev</db.username>
            <db.password>dev-pwd</db.password>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <db.driver>com.mysql.jdbc.Driver</db.driver>
            <db.url>jdbc:mysql://192.168.1.100:3306/test</db.url>
            <db.username>test</db.username>
            <db.password>test-pwd</db.password>
        </properties>
    </profile>
</profiles>

其中的dev和test表示的是profile的id。

激活profile的方式

命令行激活

以上一节的示例代码为例,我们在使用maven编译项目的时候可以使用-P参数后面跟上profile的id来选择激活某一个profile,比如如下的编译命令将会激活测试环境的profile

mvn package install -Ptest

需要说明的是,可以同时激活多个profile,多个id之间使用逗号分隔,比如:

mvn package install -Pp1,p2

settings文件显式激活

如果用户希望某个profile默认一直处于激活状态,就可以配置settings.xml文件的activePro-files元素,表示其配置的profile对于所有项目都处于激活状态,例如:

<settings>
    ...
    <activeProfiles>
        <activeProfile>dev-x</activeProfile>
    </activeProfiles>
    ...
</settings>

上面的代码让dev-x这个profile一直处于激活状态。

系统属性激活

用户可以配置当某系统属性存在的时候,自动激活profile,例如:

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name>
            </property>
        </activation>
        ...
    </profile>
</profiles>

可以进一步配置当某系统属性test存在,且值等于x的时候激活profile,例如:

<profiles>
    <profile>
        <activation>
            <property>
                <name>test</name>
                <value>x</value>
            </property>
        </activation>
        ...
    </profile>
</profiles>

不要忘了,用户可以在命令行声明系统属性。例如:

mvn clean install -Dtest=x

因此,这其实也是一种从命令行激活profile的方法,而且多个profile完全可以使用同一个系统属性来激活。

操作系统环境激活

Profile还可以自动根据操作系统环境激活,如果构建在不同的操作系统有差异,用户完全可以将这些差异写进profile,然后配置它们自动基于操作系统环境激活,例如:

<profiles>
    <profile>
        <activation>
            <os>
                <name>Windows XP</name>
                <family>Windows</family>
                <arch>x86</arch>
                <version>5.1.2600</version>
            </os>
        </activation>
        ...
    </profile>
</profiles>

这里family的值包括Windows、UNIX和Mac等,而其他几项name、arch、version,用户可以通过查看环境中的系统属性os.name、os.arch、os.version获得。

文件存在与否激活

Maven能够根据项目中某个文件存在与否来决定是否激活profile,例如:

<profiles>
    <profile>
        <activation>
            <file>
                <missing>x.properties</missing>
                <exists>y.properties</exists>
            </file>
        </activation>
        ...
    </profile>
</profiles>

默认激活

用户可以在定义profile的时候指定其默认激活,例如:

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        ...
    </profile>
</profiles>

使用activeByDefault元素用户可以指定pro-file自动激活。不过需要注意的是,如果POM中有任何一个profile通过以上其他任意一种方式被激活了,所有的默认激活配置都会失效。

哪些profile被激活

如果项目中有很多的profile,它们的激活方式各异,用户怎么知道哪些profile被激活了呢?maven-help-plugin提供了一个目标帮助用户了解当前激活的profile:

mvn help:active-profiles

maven-help-plugin还有另外一个目标用来列出当前所有的profile:

mvn help:all-profiles

profile的种类

根据具体的需要,可以在以下位置声明profile:

  • pom.xml:很显然,pom.xml中声明的profile只对当前项目有效。
  • 用户settings.xml:用户目录下.m2/settings.xml中的profile对本机上该用户所有的Maven项目有效。
  • 全局settings.xml:Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。
  • profiles.xml(Maven 2):还可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,不过该特性已经在Maven 3中被移除。建议用户将这类profile移到settings.xml中。

为了不影响其他用户且方便升级Maven,用户应该选择配置用户范围的settings.xml,避免修改全局范围的settings.xml文件。也正是因为这个原因,一般不会在全局的settings.xml文件中添加profile。

参考资料

  • Maven实战,徐晓斌,2014
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值