我知道 Maven 属性可以在不同的位置定义:
-
~/.m2/settings.xml
在本地机器上
-
<properties>
在项目父POM中
-
<properties>
在项目子模块POM中
-
<properties>
在项目父 POM 的 Maven 配置文件中
-
<properties>
在项目子模块 POM 的 Maven 配置文件中
-
-D
直接在命令行上
但目前还不清楚属性的加载顺序。有人可以解释一下它的顺序吗?
根据我的测试,属性的优先级似乎如下,其中 1. 优先于 2.; 2. 优先于 3. 等等。
-
-D
通过命令行属性
-
<properties>
in <profile>
in settings.xml
-
<properties>
in <profile>
在子 pom 中
-
<properties>
直接在子pom中
-
<properties>
in <profile>
在父 pom 中
-
<properties>
直接在父pom中
所以一般来说:
- 一切之前的命令行
- 设置在子级之前,父级之前
- 在直接定义属性之前配置文件
我使用以下设置对其进行了测试。
设置.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<custom.prop>settings</custom.prop>
</properties>
</profile>
</profiles>
</settings>
父pom.xml
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<properties>
<custom.prop>parent</custom.prop>
</properties>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<custom.prop>parent-profile</custom.prop>
</properties>
</profile>
</profiles>
</project>
子pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test-child</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<parent>
<groupId>test</groupId>
<artifactId>test-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>parent/pom.xml</relativePath>
</parent>
<properties>
<custom.prop>child</custom.prop>
</properties>
<profiles>
<profile>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<custom.prop>child-profile</custom.prop>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo message="${custom.prop}" />
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
像这样运行它并删除属性echo
ed,只要还有财产就重复。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)