Maven工具学习使用(九)——Maven Profile

使用profile实现在不同环境下不同方式的构建。

profile激活方式:

命令行激活

使用mvn命令行参数-P加上profile的id来激活profile,多个id之间以逗号分割。例如 -Ptest 激活id为test 的profile

settings文件显示激活

settings中激活的profile对所有项目都处于激活状态。settings.xml文件配置如下:

<settings>
...
     <activeProfiles>
        <activeProfile>profileid<activeProfile>
     </activeProfiles>
...
</settings>

settings.xml不支持直接配置repositories和pluginRepositories,可以通过Profile机制在setting.xml中配置仓库,activeProfiles激活profile。当执行Maven构建的时候激活的profile会将仓库配置应用到项目中去。

<settings>
    <profiles>
        <profile>
            <id>nexus<id/>
            <repositories>
                <repository>
                    <id>nexus</id>
                    <name>Nexus<name/>
                    <url>http://localhost:8081/nexus/content/groups/public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </repositories>
            <pluginRepositories>
                <repository>
                    <id>nexus</id>
                    <name>Nexus</name>
                    <url>http://localhost:8081/nexus/content/groups/public/</url>
                    <releases><enabled>true</enabled></releases>
                    <snapshots><enabled>true</enabled></snapshots>
                </repository>
            </pluginRepositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>nexus<activeProfile>
    </activeProfiles>
<settings/>

系统属性激活

<profiles>
    <profile>
        <activation>
            <!-- 当系统属性test存在时,自动激活profile -->
            <property>
                <name>test</test>
            <property>
        </activation>
    </profile>
</profiles>

<profiles>
    <profile>
        <activation>
            <!-- 
            当系统属性test存在时,且值等于x的时候激活profile 
            -->
            <property>
                <name>test</test>
                <value>x</value>
            <property>
        </activation>
    </profile>
</profiles> 
<!-- 
    mvn clean install -Dtest=x
    命令行声明系统属性,激活profile,
    多个profile完全可以使用同一个系统属性来激活。
    也是一种从命令行激活profile的方法。
-->

操作系统环境激活

<profiles>
    <profile>
        <activation>
            <!-- 
                根据操作系统环境激活,family的值包括Widows,UNIX和Mac等,
                其他几项name、arch、version、可以通过查看环境中的系统属性os.name、os.arch、os.version获得。
            -->
            <os>
                <name>test</test>
                <family>x</family>
                <arch>x86</arch>
                <version>5.1.2600</version>
            </os>
        </activation>
    </profile>
</profiles>

文件存在与否激活

<profiles>
    <profile>
        <activation>
            <!-- 
            根据文件存在与否激活
            -->
            <file>
                <missing>xxxx.properties</missing>
                <exists>yyyy.properties</exists>
            </file>
        </activation>
    </profile>
</profiles>

默认激活

<profiles>
    <profile>
        <activation>
            <!-- 
            默认激活,注意:如果POM中有任何一个profile通过以上任意一种方式被激活了,
            所有的默认激活的配置都会失效
            -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
</profiles>

mvn help:active-profiles 查看当前激活的profile

mvn help:all-profiles 查看当前所有的profiles

profile的种类

根据项目需要可以在以下位置声明profile

pom.xml文件,该文件中声明的profile只对当前项目有效。

用户settings.xml,用户目录下.m2/settings.xml中定义的profile对本机上该用户所有的Maven项目有效。

全局settings.xml,Maven安装目录下conf/settings.xml中的profile对本机上所有的Maven项目有效。

profiles.xml(Maven2),可以在项目根目录下使用一个额外的profiles.xml文件来声明profile,该特性在Maven3中被移除,可以将这类profile移到settings.xml中。

profile可以声明的POM元素

不同类型的profile中可以声明的POM元素是不同的,因为pom.xml文件中profile可以能够随着pom.xml一起被提交到代码仓库、Maven本地仓库、Maven远程仓库中,伴随着某个特定的pom.xml一起存在,它可以修改或者增加很多POM元素。

pom profile中可以声明的POM元素

<pluginRepositories></pluginRepositories>
    <distributionManagement></distributionManagement>
    <dependencies></dependencies>
    <dependencyManagement></dependencyManagement>
    <modules></modules>
    <properties></properties>
    <reporting></reporting>
    <build>
        <plugins></plugins>
        <defaultGoal></defaultGoal>
        <resources></resources>
        <restResources></testResources>
        <finalName></finalName>
    </build>
</project>

修改或添加仓库、插件仓库以及部署仓库地址;

修改或者添加项目依赖;

修改聚合项目的聚合配置;

自由添加或修改Maven属性

添加或修改项目报告配置

添加或修改插件配置、项目资源目录和测试资源目录配置以及项目构建的默认名称。

其他profile可以声明的POM元素

pom以外的profile中不允许添加或者修改绝大部分的pom元素,settings.xml的profile中不允许声明依赖或者插件,pom以外的profile只能声明、、三个元素。仅能用来影响到项目的仓库和Maven属性。

<project>
    <repositories></repositories>
    <pluginRepositories></pluginRepositories>
    <properties></properties>
</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螺旋小蜗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值