一个项目通常都会有多个不同的运行环境,例如开发环境,测试环境、生产环境等。而不同环境的构建过程很可能是不同的,例如数据源配置、插件、以及依赖的版本等。每次将项目部署到不同的环境时,都需要修改相应的配置,这样重复的工作,不仅浪费劳动力,还容易出错。为了解决这一问题,Maven 引入了 Profile 的概念,通过它可以为不同的环境定制不同的构建过程。
Profile 可以分为 3 个类型,它们的作用范围也各不相同。
类型 | 位置 | 有效范围 |
---|---|---|
Per Project | Maven 项目的 pom.xml 中 | 只对当前项目有效 |
Per User | 用户主目录(%USER_HOME%)/.m2/settings.xml 中 | 对本机上该用户所有 Maven 项目有效 |
Global | Maven 安装目录(%MAVEN_HOME%)/conf/settings.xml 中 | 对本机上所有 Maven 项目有效 |
<profiles> <profile> <id>profile id</id> .... </profile> <profile> <id>profile id</id> .... </profile> </profiles>
1. 在 pom.xml 中声明的 Profile,由于其能够随着 pom.xml 一起存在,它被提交到代码仓库中,被 Maven 安装到本地仓库或远程仓库中,所以它能够修改或增加很多 POM 元素,其中常用的元素如下表。
一级 | 二级 | 三级 |
---|---|---|
project | repositories | |
pluginRepositories | ||
dependencies | ||
plugins | ||
dependencyManagement | ||
distributionManagement | ||
modules | ||
properties | ||
reporting | ||
build | plugins | |
defaultGoal | ||
resources | ||
testResources | ||
directory | ||
filters | ||
finalName | ||
pluginManagement | ||
filters |
下面我们以一个 Maven 项目为例,分别对以上 6 种激活方式进行介绍。
图1:示例项目的目录结构
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.biancheng.www</groupId> <artifactId>maven</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- junit依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>compile</scope> </dependency> <!-- log4j依赖 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <profiles> <!--test 环境配置 --> <profile> <id>test</id> <activation> <property> <name>env</name> <value>test</value> </property> </activation> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <!--输出 --> <echo>使用 env.test.properties,将其配置信息复制到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中 </echo> <!-- 在 target\calsses 目录下生成user.properties --> <!-- env.test.properties 的内容复制到user.properties中--> <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/user.properties"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <!-- 默认环境配置 --> <profile> <id>normal</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>使用 env.properties,将其配置信息复制到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中 </echo> <!-- 在target\calsses 目录下生成user.properties --> <!-- env.properties 的内容复制到user.properties中--> <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/user.properties"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> <!--生产环境配置 --> <profile> <id>prod</id> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-antrun-plugin</artifactId> <version>1.3</version> <executions> <execution> <phase>test</phase> <goals> <goal>run</goal> </goals> <configuration> <tasks> <echo>使用 env.prod.properties,将其配置信息复制到 D:\eclipse workSpace 3\maven\target\classes\user.properties 中 </echo> <!-- 在target\calsses 目录下生成user.properties --> <!-- env.prod.properties 的内容复制到user.properties中--> <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/user.properties"/> </tasks> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> </project>
mvn clean install -Ptest1,test2
mvn clean test -Ptest
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.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.biancheng.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] ------------------------------------------------------------------------
<activeProfiles> <activeProfile>test</activeProfile> </activeProfiles>打开命令行窗口,跳转到 pom.xml 所在的目录下,执行以下 mvn 命令。
mvn clean test
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.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.biancheng.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] ------------------------------------------------------------------------
<activation> <property> <name>user</name> <value>prod</value> </property> </activation>
mvn clean test -Duser=prod
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.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.biancheng.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] ------------------------------------------------------------------------
<activation> <os> <name>Windows 10</name> <family>Windows</family> <arch>amd64</arch> <version>10.0</version> </os> </activation>
mvn clean test
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.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.biancheng.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] ------------------------------------------------------------------------
<activation> <file> <exists>./src/main/resources/env.prod.properties</exists> <missing>./src/main/resources/env.test.properties</missing> </file> </activation>
mvn clean test
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.www:maven >----------------------- [INFO] Building maven 0.0.1-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ maven --- [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.biancheng.www.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.035 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: 2.197 s [INFO] Finished at: 2021-04-21T15:12:59+08:00 [INFO] ------------------------------------------------------------------------
<activation> <activeByDefault>true</activeByDefault> </activation>
mvn clean test
[INFO] Scanning for projects... [INFO] [INFO] ----------------------< net.biancheng.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.biancheng.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] ------------------------------------------------------------------------
本文链接:http://task.lmcjl.com/news/18672.html