我知道可以在不同的位置定义Maven属性:
~/.m2/settings.xml
<properties>
<properties>
<properties>
<properties>
在项目子模块POM的Maven配置文件中-D
直接在命令行上但是不清楚属性的加载顺序。有人可以解释它的顺序吗?
答案 0 :(得分:4)
根据我的测试,属性的优先级似乎是:
-D
属性<properties>
的<profile>
中的settings.xml
儿童pom中<properties>
的<profile>
<properties>
直接在儿童pom <properties>
中的<profile>
<properties>
直接在父pom 所以一般:
我使用以下设置对其进行了测试。
的settings.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>
child 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属性,只要剩下属性就重复。