课程表

Maven课程

工具箱
速查手册

Maven 构建配置文件

当前位置:免费教程 » 软件/图像 » Maven

什么是构建配置文件?

构建配置文件是一组配置的集合,用来设置或者覆盖 Maven 构建的默认配置。使用构建配置文件,可以为不同的环境定制构建过程,例如 Producation 和 Development 环境。

Profile 在 pom.xml 中使用 activeProfiles / profiles 元素指定,并且可以用很多方式触发。Profile 在构建时修改 POM,并且为变量设置不同的目标环境(例如,在开发、测试和产品环境中的数据库服务器路径)。

Profile 类型

Profile 主要有三种类型。

类型 在哪里定义
Per Project 定义在工程 POM 文件 pom.xml 中
Per User 定义在 Maven 设置 xml 文件中 (%USER_HOME%/.m2/settings.xml)
Global 定义在 Maven 全局配置 xml 文件中 (%M2_HOME%/conf/settings.xml)

Profile 激活

Maven 的 Profile 能够通过几种不同的方式激活。

  • 显式使用命令控制台输入
  • 通过 maven 设置
  • 基于环境变量(用户 / 系统变量)
  • 操作系统配置(例如,Windows family)
  • 现存 / 缺失 文件

Profile 激活示例

我们假定你的工程目录像下面这样:

Maven Build Profile

现在,在 src/main/resources 目录下有三个环境配置文件:

文件名称 描述
env.properties 没有配置文件时的默认配置
env.test.properties 使用测试配置文件时的测试配置
env.prod.properties 使用产品配置文件时的产品配置

显式 Profile 激活

在接下来的例子中,我们将 attach maven-antrun-plugin:run 目标添加到测试阶段中。这样可以我们在不同的 Profile 中输出文本信息。我们将使用 pom.xml 来定义不同的 Profile,并在命令控制台中使用 maven 命令激活 Profile。

假定,我们在 C:\MVN\project 目录下创建了以下的 pom.xml 文件。

  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
  4. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.companyname.projectgroup</groupId>
  7. <artifactId>project</artifactId>
  8. <version>1.0</version>
  9. <profiles>
  10. <profile>
  11. <id>test</id>
  12. <build>
  13. <plugins>
  14. <plugin>
  15. <groupId>org.apache.maven.plugins</groupId>
  16. <artifactId>maven-antrun-plugin</artifactId>
  17. <version>1.1</version>
  18. <executions>
  19. <execution>
  20. <phase>test</phase>
  21. <goals>
  22. <goal>run</goal>
  23. </goals>
  24. <configuration>
  25. <tasks>
  26. <echo>Using env.test.properties</echo>
  27. <copy file="src/main/resources/env.test.propertiestofile
  28. ="${project.build.outputDirectory}/env.properties"/>
  29. </tasks>
  30. </configuration>
  31. </execution>
  32. </executions>
  33. </plugin>
  34. </plugins>
  35. </build>
  36. </profile>
  37. </profiles>
  38. </project>

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行以下 mvn 命令。使用 -P 选项指定 Profile 的名称。

  1. C:\MVN\project>mvn test -Ptest

Maven 将开始处理并显示 test Profile 的结果。

  1. [INFO] Scanning for projects...
  2. [INFO] ------------------------------------------------------------------
  3. [INFO] Building Unnamed - com.companyname.projectgroup:project:jar:1.0
  4. [INFO] task-segment: [test]
  5. [INFO] ------------------------------------------------------------------
  6. [INFO] [resources:resources {execution: default-resources}]
  7. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
  8. i.e. build is platform dependent!
  9. [INFO] Copying 3 resources
  10. [INFO] [compiler:compile {execution: default-compile}]
  11. [INFO] Nothing to compile - all classes are up to date
  12. [INFO] [resources:testResources {execution: default-testResources}]
  13. [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
  14. i.e. build is platform dependent!
  15. [INFO] skip non existing resourceDirectory C:\MVN\project\src\test\resources
  16. [INFO] [compiler:testCompile {execution: default-testCompile}]
  17. [INFO] Nothing to compile - all classes are up to date
  18. [INFO] [surefire:test {execution: default-test}]
  19. [INFO] Surefire report directory: C:\MVN\project\target\surefire-reports
  20. -------------------------------------------------------
  21. T E S T S
  22. -------------------------------------------------------
  23. There are no tests to run.
  24. Results :
  25. Tests run: 0, Failures: 0, Errors: 0, Skipped: 0
  26. [INFO] [antrun:run {execution: default}]
  27. [INFO] Executing tasks
  28. [echo] Using env.test.properties
  29. [INFO] Executed tasks
  30. [INFO] ------------------------------------------------------------------
  31. [INFO] BUILD SUCCESSFUL
  32. [INFO] ------------------------------------------------------------------
  33. [INFO] Total time: 1 second
  34. [INFO] Finished at: Sun Jul 08 14:55:41 IST 2012
  35. [INFO] Final Memory: 8M/64M
  36. [INFO] ------------------------------------------------------------------

现在我们练习一下,你可以按照下面的步骤做:

  • 在 pom.xml 的 profiles 元素中添加另一个 profile 元素(拷贝已有的 profile 元素并粘贴到 profiles 元素结尾)。
  • 将此 profile 元素的 id 从 test 修改为 normal。
  • 将任务部分修改为 echo env.properties,以及 copy env.properties 到目标目录
  • 再次重复以上三个步骤,修改 id 为 prod,修改 task 部分为 env.prod.properties
  • 全部就这些了。现在你有了三个构建配置文件(normal / test / prod)。

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。使用 -P 选项指定 Profile 的名称。

  1. C:\MVN\project>mvn test -Pnormal
  2. C:\MVN\project>mvn test -Pprod

检查构建的输出看看有什么不同。

通过 Maven 设置激活 Profile

打开 Maven 的 settings.xml 文件,该文件可以在 %USER_HOME%/.m2 目录下找到,%USER_HOME% 表示用户主目录。如果 settings.xml 文件不存在则需要创建一个。

像在下面例子中展示的一样,使用 activeProfiles 节点添加 test 配置作为激活的 Profile。

  1. <settings 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
  4. http://maven.apache.org/xsd/settings-1.0.0.xsd">
  5. <mirrors>
  6. <mirror>
  7. <id>maven.dev.snaponglobal.com</id>
  8. <name>Internal Artifactory Maven repository</name>
  9. <url>http://repo1.maven.org/maven2/</url>
  10. <mirrorOf>*</mirrorOf>
  11. </mirror>
  12. </mirrors>
  13. <activeProfiles>
  14. <activeProfile>test</activeProfile>
  15. </activeProfiles>
  16. </settings>

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。

  1. C:\MVN\project>mvn test

通过环境变量激活 Profile

现在从 maven 的 settings.xml 中删除激活的 Profile,并更新 pom.xml 中的 test Profile。将下面的内容添加到 profile 元素的 activation 元素中。

当系统属性 “env” 被设置为 “test” 时,test 配置将会被触发。创建一个环境变量 “env” 并设置它的值为 “test”。

  1. <profile>
  2. <id>test</id>
  3. <activation>
  4. <property>
  5. <name>env</name>
  6. <value>test</value>
  7. </property>
  8. </activation>
  9. </profile>

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。

  1. C:\MVN\project>mvn test

通过操作系统激活 Profile

activation 元素包含下面的操作系统信息。当系统为 windows XP 时,test Profile 将会被触发。

  1. <profile>
  2. <id>test</id>
  3. <activation>
  4. <os>
  5. <name>Windows XP</name>
  6. <family>Windows</family>
  7. <arch>x86</arch>
  8. <version>5.1.2600</version>
  9. </os>
  10. </activation>
  11. </profile>

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。

  1. C:\MVN\project>mvn test

通过现存 / 缺失的文件激活 Profile

现在使用 activation 元素包含下面的操作系统信息。当 target/generated-sources/axistools/wsdl2java/com/companyname/group 缺失时,test Profile 将会被触发。

  1. <profile>
  2. <id>test</id>
  3. <activation>
  4. <file>
  5. <missing>target/generated-sources/axistools/wsdl2java/
  6. com/companyname/group</missing>
  7. </file>
  8. </activation>
  9. </profile>

现在打开命令控制台,跳转到 pom.xml 所在目录,并执行下面的 mvn 命令。不要使用 -P 选项指定 Profile 的名称。Maven 将显示被激活的 test Profile 的结果。

  1. C:\MVN\project>mvn test
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号