Reputation: 5552
I know that Maven properties can be defined in different locations:
~/.m2/settings.xml
on the local machine<properties>
in the project parent POM<properties>
in the project child module POM<properties>
in Maven profile of the project parent POM<properties>
in Maven profile of the project child module POM-D
directly on the command lineBut it's not very clear in which order the properties are loaded. Could somebody explain its order?
Upvotes: 15
Views: 5997
Reputation: 3381
Based on my tests, the precedence of properties seems to be the following, where 1. takes precedence over 2.; 2. takes precedence over 3. and so on.
-D
property via command line<properties>
in <profile>
in settings.xml
<properties>
in <profile>
in the child pom<properties>
directly in child pom<properties>
in <profile>
in the parent pom<properties>
directly in parent pomSo generally:
I tested it with the following setup.
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>
parent 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>
Run it like this and delete the property which is echo
ed, repeat as long as there is a property left.
Upvotes: 25