Maven Profile精讲

一个项目通常都会有多个不同的运行环境,例如开发环境,测试环境、生产环境等。而不同环境的构建过程很可能是不同的,例如数据源配置、插件、以及依赖的版本等。每次将项目部署到不同的环境时,都需要修改相应的配置,这样重复的工作,不仅浪费劳动力,还容易出错。为了解决这一问题,Maven 引入了 Profile 的概念,通过它可以为不同的环境定制不同的构建过程。

Profile 的类型

Profile 可以分为 3  个类型,它们的作用范围也各不相同。

类型位置有效范围
Per ProjectMaven 项目的 pom.xml 中只对当前项目有效
Per User用户主目录(%USER_HOME%)/.m2/settings.xml 中对本机上该用户所有 Maven 项目有效
GlobalMaven 安装目录(%MAVEN_HOME%)/conf/settings.xml 中对本机上所有 Maven 项目有效

声明 Profile 

Maven 通过 profiles 元素来声明一组 Profile 配置,该元素下可以包含多个 profile 子元素,每个 profile 元素表示一个 Profile 配置。每个 profile 元素中通常都要包含一个 id 子元素,该元素是调用当前 Profile 的标识。

定义 Profile 的一般形式如下:

  1. <profiles>
  2. <profile>
  3. <id>profile id</id>
  4. ....
  5. </profile>
  6. <profile>
  7. <id>profile id</id>
  8. ....
  9. </profile>
  10. </profiles>

除此之外,Profile 中还可以声明一些其他的 POM 元素,但不同位置的 Profile 所能声明的 POM 元素也是不同的。


1. 在 pom.xml 中声明的 Profile,由于其能够随着 pom.xml 一起存在,它被提交到代码仓库中,被 Maven 安装到本地仓库或远程仓库中,所以它能够修改或增加很多 POM 元素,其中常用的元素如下表。
 

一级二级三级
projectrepositories 
pluginRepositories 
dependencies 
plugins 
dependencyManagement 
distributionManagement 
modules 
properties 
reporting 
buildplugins
defaultGoal
resources
testResources
directory
filters
finalName
pluginManagement
filters

2. 在 setting.xml 中声明的 Profile 是无法保证能够随着 pom.xml 一起被分发的,因此 Maven 不允许用户在该类型的 Profile 修改或增加依赖或插件等配置信息,它只能声明以下范围较为宽泛的元素。

  • repositories:仓库配置。
  • pluginRepositories:插件仓库配置。
  • properties:键值对,该键值对可以在 pom.xml 中使用。

激活 Profile

Profile 能够在项目构建时,修改 POM 中配置或者添加一些额外的配置元素。用户可以通过多种方式激活 Profile,以实现不同环境使用不同的配置,执行不同的构建过程。

Profile 可以通过以下 6 种方式激活:

  • 命令行激活
  • settings.xml 文件显示激活
  • 系统属性激活
  • 操作系统环境激活
  • 文件存在与否激活
  • 默认激活


下面我们以一个 Maven 项目为例,分别对以上 6 种激活方式进行介绍。

准备 Maven 项目

1. 创建一个名为 maven 的项目,并在其 src/main/resources 目录下有 3 个环境 properties 配置文件。

  • env.properties:默认配置文件
  • env.test.properties:测试环境配置文件
  • env.prod.properties:生产环境配置文件

2. 该项目目录结构如图 1 所示。

env

图1:示例项目的目录结构

3. 在 pom.xml 中定义三个不同的 Profile,将 maven-antrun-plugin:run 目标绑定到 default 生命周期的 test 阶段上,以实现在不同的 Profile 中进行不同的操作。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>net.jhelp.www</groupId>
  6. <artifactId>maven</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <dependencies>
  9. <!-- junit依赖 -->
  10. <dependency>
  11. <groupId>junit</groupId>
  12. <artifactId>junit</artifactId>
  13. <version>4.9</version>
  14. <scope>compile</scope>
  15. </dependency>
  16. <!-- log4j依赖 -->
  17. <dependency>
  18. <groupId>log4j</groupId>
  19. <artifactId>log4j</artifactId>
  20. <version>1.2.17</version>
  21. </dependency>
  22. </dependencies>
  23. <profiles>
  24. <!--test 环境配置 -->
  25. <profile>
  26. <id>test</id>
  27. <activation>
  28. <property>
  29. <name>env</name>
  30. <value>test</value>
  31. </property>
  32. </activation>
  33. <build>
  34. <plugins>
  35. <plugin>
  36. <groupId>org.apache.maven.plugins</groupId>
  37. <artifactId>maven-antrun-plugin</artifactId>
  38. <version>1.3</version>
  39. <executions>
  40. <execution>
  41. <phase>test</phase>
  42. <goals>
  43. <goal>run</goal>
  44. </goals>
  45. <configuration>
  46. <tasks>
  47. <!--输出 -->
  48. <echo>使用 env.test.properties,将其配置信息复制到 D:\eclipse workSpace
  49. 3\maven\target\classes\user.properties 中
  50. </echo>
  51. <!-- 在 target\calsses 目录下生成user.properties -->
  52. <!-- env.test.properties 的内容复制到user.properties中-->
  53. <copy file="src/main/resources/env.test.properties"
  54. tofile="${project.build.outputDirectory}/user.properties"/>
  55. </tasks>
  56. </configuration>
  57. </execution>
  58. </executions>
  59. </plugin>
  60. </plugins>
  61. </build>
  62. </profile>
  63. <!-- 默认环境配置 -->
  64. <profile>
  65. <id>normal</id>
  66. <build>
  67. <plugins>
  68. <plugin>
  69. <groupId>org.apache.maven.plugins</groupId>
  70. <artifactId>maven-antrun-plugin</artifactId>
  71. <version>1.3</version>
  72. <executions>
  73. <execution>
  74. <phase>test</phase>
  75. <goals>
  76. <goal>run</goal>
  77. </goals>
  78. <configuration>
  79. <tasks>
  80. <echo>使用 env.properties,将其配置信息复制到 D:\eclipse workSpace
  81. 3\maven\target\classes\user.properties 中
  82. </echo>
  83. <!-- 在target\calsses 目录下生成user.properties -->
  84. <!-- env.properties 的内容复制到user.properties中-->
  85. <copy file="src/main/resources/env.properties"
  86. tofile="${project.build.outputDirectory}/user.properties"/>
  87. </tasks>
  88. </configuration>
  89. </execution>
  90. </executions>
  91. </plugin>
  92. </plugins>
  93. </build>
  94. </profile>
  95. <!--生产环境配置 -->
  96. <profile>
  97. <id>prod</id>
  98. <build>
  99. <plugins>
  100. <plugin>
  101. <groupId>org.apache.maven.plugins</groupId>
  102. <artifactId>maven-antrun-plugin</artifactId>
  103. <version>1.3</version>
  104. <executions>
  105. <execution>
  106. <phase>test</phase>
  107. <goals>
  108. <goal>run</goal>
  109. </goals>
  110. <configuration>
  111. <tasks>
  112. <echo>使用 env.prod.properties,将其配置信息复制到 D:\eclipse workSpace
  113. 3\maven\target\classes\user.properties 中
  114. </echo>
  115. <!-- 在target\calsses 目录下生成user.properties -->
  116. <!-- env.prod.properties 的内容复制到user.properties中-->
  117. <copy file="src/main/resources/env.prod.properties"
  118. tofile="${project.build.outputDirectory}/user.properties"/>
  119. </tasks>
  120. </configuration>
  121. </execution>
  122. </executions>
  123. </plugin>
  124. </plugins>
  125. </build>
  126. </profile>
  127. </profiles>
  128. </project>

命令行激活

我们可以在命令行中使用 mvn 命令行参数 -P 加上 Profile 的 id 来激活 Profile,多个 id 之间使用逗号隔开。例如,激活 test1 和 test2 两个 Profile, 命令如下:

  • mvn clean install -Ptest1,test2

打开命令行窗口,跳转到 pom.xml 所在的目录,执行以下 mvn 命令,激活 id 为 test 的 Profile。

  • mvn clean test -Ptest

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  • [INFO] Building maven 0.0.1-SNAPSHOT
  • [INFO] --------------------------------[ jar ]---------------------------------
  • [INFO]
  • [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  • [INFO] Deleting D:\eclipse workSpace 3\maven\target
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 3 resources
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 0 resource
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
  • [INFO]
  • [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  • [INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
  • -------------------------------------------------------
  • T E S T S
  • -------------------------------------------------------
  • Running net.jhelp.www.Test
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
  • Results :
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • [INFO]
  • [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  • [INFO] Executing tasks
  • [echo] 使用 env.test.properties,将其配置信息复制到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中
  • [copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO] Executed tasks
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 1.968 s
  • [INFO] Finished at: 2021-03-02T09:28:49+08:00
  • [INFO] ------------------------------------------------------------------------

settings.xml 文件显示激活

在本地仓库的 settings.xml 文件中添加如下配置,激活指定的 Profile。

  1. <activeProfiles>
  2. <activeProfile>test</activeProfile>
  3. </activeProfiles>

Maven 本地仓库的 settings.xml 文件,默认位于 %USER_HOME%/.m2 目录下。

打开命令行窗口,跳转到 pom.xml 所在的目录下,执行以下 mvn 命令。

  • mvn clean test

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  • [INFO] Building maven 0.0.1-SNAPSHOT
  • [INFO] --------------------------------[ jar ]---------------------------------
  • [INFO]
  • [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  • [INFO] Deleting D:\eclipse workSpace 3\maven\target
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 3 resources
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 0 resource
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
  • [INFO]
  • [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  • [INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
  • -------------------------------------------------------
  • T E S T S
  • -------------------------------------------------------
  • Running net.jhelp.www.Test
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.04 sec
  • Results :
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • [INFO]
  • [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  • [INFO] Executing tasks
  • [echo] 使用 env.test.properties,将其配置信息复制到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中
  • [copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO] Executed tasks
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 2.012 s
  • [INFO] Finished at: 2021-03-02T09:56:59+08:00
  • [INFO] ------------------------------------------------------------------------

系统属性激活

用户可以配置当某个系统属性存在时,激活指定的 Profile。例如,我们在 id 为 prod 的 profile 元素中添加以下配置,表示当系统属性 user 存在,且值等于 prod 时,自动激活该 Profile。

  1. <activation>
  2. <property>
  3. <name>user</name>
  4. <value>prod</value>
  5. </property>
  6. </activation>

打开命令行窗口,跳转到 pom.xml 所在的目录。执行命令,其中参数 -D 选项用来指定系统临时属性。

  • mvn clean test -Duser=prod

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  • [INFO] Building maven 0.0.1-SNAPSHOT
  • [INFO] --------------------------------[ jar ]---------------------------------
  • [INFO]
  • [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  • [INFO] Deleting D:\eclipse workSpace 3\maven\target
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 3 resources
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 0 resource
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
  • [INFO]
  • [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  • [INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
  • -------------------------------------------------------
  • T E S T S
  • -------------------------------------------------------
  • Running net.jhelp.www.Test
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.039 sec
  • Results :
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • [INFO]
  • [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  • [INFO] Executing tasks
  • [echo] 使用 env.prod.properties,将其配置信息复制到 D:\eclipse workSpace
  • [echo] 3\maven\target\classes\user.properties 中
  • [copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO] Executed tasks
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 2.036 s
  • [INFO] Finished at: 2021-03-02T10:23:19+08:00
  • [INFO] ------------------------------------------------------------------------

操作系统环境激活

Maven 还可以根据操作系统环境自动激活指定的 Profile。例如,将以下配置(本地计算机操作系统环境信息)添加到 pom.xml 文件中的 id 为 normal 的 Profile 中。

  1. <activation>
  2. <os>
  3. <name>Windows 10</name>
  4. <family>Windows</family>
  5. <arch>amd64</arch>
  6. <version>10.0</version>
  7. </os>
  8. </activation>

打开命令行窗口,跳转到 pom.xml 所在的目录下,执行以下 mvn 命令。

  • mvn clean test

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  • [INFO] Building maven 0.0.1-SNAPSHOT
  • [INFO] --------------------------------[ jar ]---------------------------------
  • [INFO]
  • [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  • [INFO] Deleting D:\eclipse workSpace 3\maven\target
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 3 resources
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  • [WARNING] Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] Copying 0 resource
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  • [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 1 source file to D:\eclipse workSpace 3\maven\target\test-classes
  • [INFO]
  • [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  • [INFO] Surefire report directory: D:\eclipse workSpace 3\maven\target\surefire-reports
  • -------------------------------------------------------
  • T E S T S
  • -------------------------------------------------------
  • Running net.jhelp.www.Test
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 sec
  • Results :
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • [INFO]
  • [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  • [INFO] Executing tasks
  • [echo] 使用 env.properties,将其配置信息复制到 D:\eclipse workSpace
  • [echo] 3\maven\target\classes\user.properties 中
  • [copy] Copying 1 file to D:\eclipse workSpace 3\maven\target\classes
  • [INFO] Executed tasks
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 2.025 s
  • [INFO] Finished at: 2021-03-02T10:46:12+08:00
  • [INFO] ------------------------------------------------------------------------

文件存在与否激活

Maven 可以根据某些文件存在与否,来决定是否激活 Profile。例如,在 id 为 prod 的 Profile 中添加以下配置,该配置表示当 env.prod.properties 文件存在,且 env.test.properties 文件不存在时,激活该 Profile。

  1. <activation>
  2. <file>
  3. <exists>./src/main/resources/env.prod.properties</exists>
  4. <missing>./src/main/resources/env.test.properties</missing>
  5. </file>
  6. </activation>

将 env.test.properties 从项目中删除,打开命令行窗口,跳转到 pom.xml 所在目录,执行以下命令。

  1. mvn clean test

执行结果如下。

  1. [INFO] Scanning for projects...
  2. [INFO]
  3. [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  4. [INFO] Building maven 0.0.1-SNAPSHOT
  5. [INFO] --------------------------------[ jar ]---------------------------------
  6. [INFO]
  7. [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  8. [INFO]
  9. [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  10. [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  11. [INFO] Copying 2 resources
  12. [INFO]
  13. [INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ maven ---
  14. [INFO] Changes detected - recompiling the module!
  15. [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  16. [INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\classes
  17. [INFO]
  18. [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  19. [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  20. [INFO] skip non existing resourceDirectory D:\eclipse workSpace4\maven\src\test\resources
  21. [INFO]
  22. [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  23. [INFO] Changes detected - recompiling the module!
  24. [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  25. [INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\test-classes
  26. [INFO]
  27. [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  28. [INFO] Surefire report directory: D:\eclipse workSpace4\maven\target\surefire-reports
  29. -------------------------------------------------------
  30. T E S T S
  31. -------------------------------------------------------
  32. Running net.jhelp.www.AppTest
  33. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 sec
  34. Results :
  35. Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  36. [INFO]
  37. [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  38. [INFO] Executing tasks
  39. [echo] 使用 env.prod.properties,将其配置信息复制到 D:\eclipse workSpace
  40. [echo] 3\maven\target\classes\user.properties 中
  41. [copy] Copying 1 file to D:\eclipse workSpace4\maven\target\classes
  42. [INFO] Executed tasks
  43. [INFO] ------------------------------------------------------------------------
  44. [INFO] BUILD SUCCESS
  45. [INFO] ------------------------------------------------------------------------
  46. [INFO] Total time: 2.197 s
  47. [INFO] Finished at: 2021-04-21T15:12:59+08:00
  48. [INFO] ------------------------------------------------------------------------

默认激活

我们可以在声明 Profile 时,指定其默认激活。例如,在 id 为 normal 的 Profile 中添加以下配置,指定该 Profile 默认激活。

  1. <activation>
  2. <activeByDefault>true</activeByDefault>
  3. </activation>

打开命令行窗口,跳转到 pom.xml 所在目录,执行以下命令。

  1. mvn clean test

执行结果如下。

  • [INFO] Scanning for projects...
  • [INFO]
  • [INFO] ----------------------< net.jhelp.www:maven >-----------------------
  • [INFO] Building maven 0.0.1-SNAPSHOT
  • [INFO] --------------------------------[ jar ]---------------------------------
  • [INFO]
  • [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven ---
  • [INFO] Deleting D:\eclipse workSpace4\maven\target
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ maven ---
  • [WARNING] Using platform encoding (UTF-8 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) @ maven ---
  • [INFO] Changes detected - recompiling the module!
  • [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  • [INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\classes
  • [INFO]
  • [INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ maven ---
  • [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
  • [INFO] skip non existing resourceDirectory D:\eclipse workSpace4\maven\src\test\resources
  • [INFO]
  • [INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ maven ---
  • [INFO] Changes detected - recompiling the module!
  • [WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
  • [INFO] Compiling 1 source file to D:\eclipse workSpace4\maven\target\test-classes
  • [INFO]
  • [INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ maven ---
  • [INFO] Surefire report directory: D:\eclipse workSpace4\maven\target\surefire-reports
  • -------------------------------------------------------
  • T E S T S
  • -------------------------------------------------------
  • Running net.jhelp.www.AppTest
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.038 sec
  • Results :
  • Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
  • [INFO]
  • [INFO] --- maven-antrun-plugin:1.3:run (default) @ maven ---
  • [INFO] Executing tasks
  • [echo] 使用 env.prod.properties,将其配置信息复制到 D:\eclipse workSpace
  • [echo] 3\maven\target\classes\user.properties 中
  • [copy] Copying 1 file to D:\eclipse workSpace4\maven\target\classes
  • [INFO] Executed tasks
  • [INFO] ------------------------------------------------------------------------
  • [INFO] BUILD SUCCESS
  • [INFO] ------------------------------------------------------------------------
  • [INFO] Total time: 1.986 s
  • [INFO] Finished at: 2021-04-21T15:24:59+08:00
  • [INFO] ------------------------------------------------------------------------
腾讯云推出云产品限时特惠抢购活动:2C2G云服务器7.9元/月起
本文链接:https://www.jhelp.net/p/6vSdYnoYAAcNjEBe (转载请保留)。
关注下面的标签,发现更多相似文章