Maven 构建配置文件

构建配置文件是一系列的配置项的值,可以用来设置或者覆盖 Maven 构建默认值。

使用构建配置文件,你可以为不同的环境,比如说生产环境(Production)和开发(Development)环境,定制构建方式。

配置文件在 pom.xml 文件中使用 activeProfiles 或者 profiles 元素指定,并且可以通过各种方式触发。配置文件在构建时修改 POM,并且用来给参数设定不同的目标环境(比如说,开发(Development)、测试(Testing)和生产环境(Production)中数据库服务器的地址)。


构建配置文件的类型

构建配置文件大体上有三种类型:

类型在哪定义
项目级(Per Project)定义在项目的POM文件pom.xml中
用户级 (Per User)定义在Maven的设置xml文件中 (%USER_HOME%/.m2/settings.xml)
全局(Global)定义在 Maven 全局的设置 xml 文件中 (%M2_HOME%/conf/settings.xml)

配置文件激活

Maven的构建配置文件可以通过多种方式激活。

  • 使用命令控制台输入显式激活。
  • 通过 maven 设置。
  • 基于环境变量(用户或者系统变量)。
  • 操作系统设置(比如说,Windows系列)。
  • 文件的存在或者缺失。

配置文件激活实例

假定项目结构如下:

其中在src/main/resources文件夹下有三个用于测试文件:

文件名描述
env.properties如果未指定配置文件时默认使用的配置。
env.test.properties当测试配置文件使用时的测试配置。
env.prod.properties当生产配置文件使用时的生产配置。

注意:这三个配置文件并不是代表构建配置文件的功能,而是用于本次测试的目的;比如,我指定了构建配置文件为 prod 时,项目就使用 envprod.properties文件。

注意:下面的例子仍然是使用 AntRun 插件,因为此插件能绑定 Maven 生命周期阶段,并通过 Ant 的标签不用编写一点代码即可输出信息、复制文件等,经此而已。其余的与本次构建配置文件无关。

1、配置文件激活

profile 可以让我们定义一系列的配置信息,然后指定其激活条件。这样我们就可以定义多个 profile,然后每个 profile 对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果。

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

pom.xml 文件如下:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.jsoft.test</groupId>
  5. <artifactId>testproject</artifactId>
  6. <packaging>jar</packaging>
  7. <version>0.1-SNAPSHOT</version>
  8. <name>testproject</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. <profiles>
  19. <profile>
  20. <id>test</id>
  21. <build>
  22. <plugins>
  23. <plugin>
  24. <groupId>org.apache.maven.plugins</groupId>
  25. <artifactId>maven-antrun-plugin</artifactId>
  26. <version>1.8</version>
  27. <executions>
  28. <execution>
  29. <phase>test</phase>
  30. <goals>
  31. <goal>run</goal>
  32. </goals>
  33. <configuration>
  34. <tasks>
  35. <echo>Using env.test.properties</echo>
  36. <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  37. </tasks>
  38. </configuration>
  39. </execution>
  40. </executions>
  41. </plugin>
  42. </plugins>
  43. </build>
  44. </profile>
  45. <profile>
  46. <id>normal</id>
  47. <build>
  48. <plugins>
  49. <plugin>
  50. <groupId>org.apache.maven.plugins</groupId>
  51. <artifactId>maven-antrun-plugin</artifactId>
  52. <version>1.8</version>
  53. <executions>
  54. <execution>
  55. <phase>test</phase>
  56. <goals>
  57. <goal>run</goal>
  58. </goals>
  59. <configuration>
  60. <tasks>
  61. <echo>Using env.properties</echo>
  62. <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  63. </tasks>
  64. </configuration>
  65. </execution>
  66. </executions>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </profile>
  71. <profile>
  72. <id>prod</id>
  73. <build>
  74. <plugins>
  75. <plugin>
  76. <groupId>org.apache.maven.plugins</groupId>
  77. <artifactId>maven-antrun-plugin</artifactId>
  78. <version>1.8</version>
  79. <executions>
  80. <execution>
  81. <phase>test</phase>
  82. <goals>
  83. <goal>run</goal>
  84. </goals>
  85. <configuration>
  86. <tasks>
  87. <echo>Using env.prod.properties</echo>
  88. <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  89. </tasks>
  90. </configuration>
  91. </execution>
  92. </executions>
  93. </plugin>
  94. </plugins>
  95. </build>
  96. </profile>
  97. </profiles>
  98. </project>

注意:**构建配置文件采用的是 ** 节点。

说明:上面新建了三个 ,其中 区分了不同的 ** 执行不同的 AntRun 任务;而 AntRun 的任务可以这么理解,AntRun 监听 test 的 Maven 生命周期阶段,当 Maven 执行 test 时,就除了发 AntRun 的任务,任务里面为输出文本并复制文件到指定的位置;而至于要执行哪个 AntRun 任务,此时构建配置文件起到了传输指定的作用,比如,通过命令行参数输入指定的 **

执行命令:

  1. mvn test -Ptest

提示:第一个 test 为 Maven 生命周期阶段,第 2 个 test 为构建配置文件指定的 <id> 参数,这个参数通过 -P 来传输,当然,它可以是 prod 或者 normal 这些由你定义的**

运行的结果如下:

 

可以看出成功的触发了AntRun的任务。并且是对应构建配置文件下的 为 test 的任务。

再测试其余两个命令,结果如下:

2、通过Maven设置激活配置文件

打开 %USER_HOME%/.m2 目录下的 settings.xml 文件,其中 %USER_HOME% 代表用户主目录。如果 setting.xml 文件不存在就直接拷贝 %M2_HOME%/conf/settings.xml 到 .m2 目录,其中 %M2_HOME% 代表 Maven 的安装目录。

配置 setting.xml 文件,增加 属性:

  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. ...
  6. <activeProfiles>
  7. <activeProfile>test</activeProfile>
  8. </activeProfiles>
  9. </settings>

执行命令:

  1. mvn test

提示 1:此时不需要使用 -Ptest 来输入参数了,上面的 setting.xml 文件的 <activeprofile> 已经指定了 test 参数代替了。

提示 2:同样可以使用在 %M2_HOME%/conf/settings.xml 的文件进行配置,效果一致。

执行结果:

3、通过环境变量激活配置文件

先把上一步测试的 setting.xml 值全部去掉。

然后在 pom.xml 里面的 <id> 为 test 的 <profile> 节点,加入 <activation> 节点:

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.jsoft.test</groupId>
  5. <artifactId>testproject</artifactId>
  6. <packaging>jar</packaging>
  7. <version>0.1-SNAPSHOT</version>
  8. <name>testproject</name>
  9. <url>http://maven.apache.org</url>
  10. <dependencies>
  11. <dependency>
  12. <groupId>junit</groupId>
  13. <artifactId>junit</artifactId>
  14. <version>3.8.1</version>
  15. <scope>test</scope>
  16. </dependency>
  17. </dependencies>
  18. <profiles>
  19. <profile>
  20. <id>test</id>
  21. <activation>
  22. <property>
  23. <name>env</name>
  24. <value>test</value>
  25. </property>
  26. </activation>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.apache.maven.plugins</groupId>
  31. <artifactId>maven-antrun-plugin</artifactId>
  32. <version>1.8</version>
  33. <executions>
  34. <execution>
  35. <phase>test</phase>
  36. <goals>
  37. <goal>run</goal>
  38. </goals>
  39. <configuration>
  40. <tasks>
  41. <echo>Using env.test.properties</echo>
  42. <copy file="src/main/resources/env.test.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  43. </tasks>
  44. </configuration>
  45. </execution>
  46. </executions>
  47. </plugin>
  48. </plugins>
  49. </build>
  50. </profile>
  51. <profile>
  52. <id>normal</id>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.apache.maven.plugins</groupId>
  57. <artifactId>maven-antrun-plugin</artifactId>
  58. <version>1.8</version>
  59. <executions>
  60. <execution>
  61. <phase>test</phase>
  62. <goals>
  63. <goal>run</goal>
  64. </goals>
  65. <configuration>
  66. <tasks>
  67. <echo>Using env.properties</echo>
  68. <copy file="src/main/resources/env.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  69. </tasks>
  70. </configuration>
  71. </execution>
  72. </executions>
  73. </plugin>
  74. </plugins>
  75. </build>
  76. </profile>
  77. <profile>
  78. <id>prod</id>
  79. <build>
  80. <plugins>
  81. <plugin>
  82. <groupId>org.apache.maven.plugins</groupId>
  83. <artifactId>maven-antrun-plugin</artifactId>
  84. <version>1.8</version>
  85. <executions>
  86. <execution>
  87. <phase>test</phase>
  88. <goals>
  89. <goal>run</goal>
  90. </goals>
  91. <configuration>
  92. <tasks>
  93. <echo>Using env.prod.properties</echo>
  94. <copy file="src/main/resources/env.prod.properties" tofile="${project.build.outputDirectory}/env.properties" overwrite="true"/>
  95. </tasks>
  96. </configuration>
  97. </execution>
  98. </executions>
  99. </plugin>
  100. </plugins>
  101. </build>
  102. </profile>
  103. </profiles>
  104. </project>

执行命令:

  1. mvn test -Denv=test

提示 1:上面使用 -D 传递环境变量,其中 evn 对应刚才设置的 <name> 值,test 对应<value>

提示 2:在 Windows 10 上测试了系统的环境变量,但是不生效,所以,只能通过 -D 传递。

执行结果:

4、通过操作系统激活配置文件

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. mvn test

5、通过文件的存在或者缺失激活配置文件

现在使用 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. mvn test
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
mavensettings配置文件是用于配置Maven构建工具的设置和参数的文件。在Maven项目中,该文件通常命名为settings.xml,并位于Maven安装目录的conf文件夹中。 Mavensettings配置文件包含了一些重要的配置项,用于指定Maven的本地仓库路径、远程仓库的URL、验证信息、代理服务器设置、镜像仓库等。通过编辑这个文件,我们可以定制化Maven的各种行为。 首先,Mavensettings配置文件用于指定Maven的本地仓库路径。本地仓库是Maven用于存储下载的依赖库和插件的地方,可以通过设置<localRepository>元素来指定本地仓库的路径。 另外,远程仓库的配置也是Mavensettings配置文件的一部分。其中,<repositories>元素用于指定Maven从哪些远程仓库下载依赖库。我们可以在<repositories>元素下使用<repository>子元素来配置远程仓库的URL和其他相关信息。 Mavensettings配置文件还可以用来存储验证信息,如开发者的用户名和密码。这些凭据可以用于访问受身份验证保护的远程仓库或发布构件到远程仓库。我们可以使用<servers>元素来设置验证信息。 此外,Mavensettings配置文件还支持设置代理服务器,以便在Maven访问远程仓库时使用代理。可以通过<proxies>元素来配置代理服务器的信息。 最后,我们还可以使用<mirrors>元素来设置镜像仓库。镜像仓库可以加速构建过程,通过设置<mirrors>元素,我们可以指定一个可用的镜像仓库来替代默认的中央仓库。 综上所述,Mavensettings配置文件是一个非常重要的配置文件,对于定制化Maven构建工具非常有用。通过编辑这个文件,我们可以指定本地仓库路径、远程仓库的URL、验证信息、代理服务器设置、镜像仓库等,从而使Maven按照我们的要求进行构建工作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

智慧浩海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值