maven怎样去激活不同的profile

本文将介绍在maven中定义了多个profile,我们通过什么样的方式去激活我们想要的profile。激活方式有多种,本文将逐一介绍。

在正式介绍之前,我们先看看 pom.xml 文件中 profiles 部分的配置。如下:

<profiles>
    <!-- 开发环境 -->
    <profile>
        <!-- 不同环境Profile的唯一id -->
        <id>dev</id>
        <properties>
            <!-- profiles.active是自定义的字段,自定义字段可以有多个 -->
            <profiles.active>dev</profiles.active>
        </properties>
        <activation>
            <!-- 标记为默认 profile -->
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!-- 生成环境 -->
    <profile>
        <id>prod</id>
        <properties>
            <profiles.active>prod</profiles.active>
        </properties>
    </profile>
    <!-- 测试环境 -->
    <profile>
        <id>test</id>
        <properties>
            <profiles.active>test</profiles.active>
        </properties>
    </profile>
</profiles>

上面定义了3个profile,id分别为dev(开发环境)、prod(生成环境)、test(测试环境)。profile的激活方式有很多种。

通过maven命令参数

使用 “mvn --help”命令查看帮助信息,找到 -P 的帮助信息,如下:

-P,--activate-profiles <arg>   Comma-delimited list of profiles to activate(使用逗号分隔要激活的profile)

实例:使用 “mvn -P prod clean package” 命令激活 id 等于 prod 的 profile,在该 profile 上面执行 clean 和 package 生命周期。如下:

D:\learn\Maven\workspaces\learn-maven-profile4>mvn -P prod clean package
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.huangx.maven:learn-maven-profile4 >----------------
[INFO] Building learn-maven-profile4 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ learn-maven-profile4 ---
[INFO] Deleting D:\learn\Maven\workspaces\learn-maven-profile4\target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ learn-maven-profile4 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 2 resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ learn-maven-profile4 ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding GBK, i.e. build is platform dependent!
[INFO] Compiling 3 source files to D:\learn\Maven\workspaces\learn-maven-profile4\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ learn-maven-profile4 ---
[WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] skip non existing resourceDirectory D:\learn\Maven\workspaces\learn-maven-profile4\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ learn-maven-profile4 ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ learn-maven-profile4 ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ learn-maven-profile4 ---
[INFO] Building jar: D:\learn\Maven\workspaces\learn-maven-profile4\target\learn-maven-profile4-1.0-SNAPSHOT.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.746 s
[INFO] Finished at: 2019-08-08T08:27:47+08:00
[INFO] ------------------------------------------------------------------------

通过pom文件里的activation属性

activation 也存在多种方式,分别如下:

(1)activeByDefault(默认激活某个profile)

<profile>
    <!-- 不同环境Profile的唯一id -->
    <id>dev</id>
    <properties>
        <!-- profiles.active是自定义的字段,自定义字段可以有多个 -->
        <profiles.active>dev</profiles.active>
    </properties>
    <activation>
        <!-- 标记为默认 profile -->
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

上面定义的 dev profile 将会被默认激活,这一般可以用来指定开发环境。

(2)jdk版本

jdk版本可以根据不同的jdk版本来激活不同的 profile,其中

<profile>
    <id>prod</id>
    <properties>
        <profiles.active>prod</profiles.active>
    </properties>
    <!-- 根据jdk环境来激活 -->
    <activation>
        <!-- 通过jdk版本 -->
        <!-- 当jdk环境版本为1.5时,此profile被激活 -->
        <jdk>1.5</jdk>
        <!-- 当jdk环境版本1.5或以上时,此profile被激活 -->
        <jdk>[1.5,)</jdk>
    </activation>
</profile>

上面,定义 id 为 prod 的 profile 在 jdk 版本为 1.5 或 1.5+ 上时被激活。

(3)操作系统

<profile>
    <id>prod</id>
    <properties>
        <profiles.active>prod</profiles.active>
    </properties>
    <activation>
        <!--根据当前操作系统-->
        <os>
            <name>Windows 8.1</name>
            <family>Windows</family>
            <arch>amd64</arch>
            <version>6.3</version>
        </os>
    </activation>
</profile>

使用操作系统激活指定的profile,你可能不知道os.name、os.arch 和 os.version 该怎么填写,其实很简单,直接写一个简单的 java 程序,获取系统的操作系统信息即可。代码如下:

package com.huangx.maven;

public class SystemInfo {

    public static void main(String[] args) {
        // os.name 操作系统的名称
        System.out.println( System.getProperty("os.name") );

        // os.arch 操作系统的架构
        System.out.println( System.getProperty("os.arch") );

        // os.version 操作系统的版本
        System.out.println( System.getProperty("os.version") );
    }

}

(4)系统环境变量(需要使用 -D 而不是使用 -P 来定义系统环境变量)

<profile>
    <id>prod</id>
    <properties>
        <profiles.active>prod</profiles.active>
    </properties>
    <activation>
        <!--通过系统环境变量,name-value自定义 -->
        <property>
            <name>env</name>
            <value>prod</value>
        </property>
    </activation>
</profile>

注意,我们需要执行“mvn -D env=prod clean package”命令来激活该 profile。

(5)文件的存在或缺失

<profile>
    <id>prod</id>
    <properties>
        <profiles.active>prod</profiles.active>
    </properties>
    <activation>
        <!-- 通过文件的存在或缺失 -->
        <file>
            <!-- 文件不存在激活 -->
            <missing>target/classes/not-file.txt</missing>
            <!-- 文件存在激活 -->
            <exists/>
        </file>
    </activation>
</profile>

实际项目可以根据需要选取一种即可。这种的优先级低于maven命令参数指定的方式。

如果在胜利前却步,往往只会拥抱失败;如果在困难时坚持,常常会获得新的成功。
0 不喜欢
说说我的看法 -
全部评论(
没有评论
目录
热门标签
热门文章
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号