Maven properties loading order

Mincong Huang :

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 line

But it's not very clear in which order the properties are loaded. Could somebody explain its order?

SilverNak :

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.

  1. -D property via command line
  2. <properties> in <profile> in settings.xml
  3. <properties> in <profile> in the child pom
  4. <properties> directly in child pom
  5. <properties> in <profile> in the parent pom
  6. <properties> directly in parent pom

So generally:

  • Commandline before everything
  • Settings before child before parent
  • profile before directly defined properties

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 echoed, repeat as long as there is a property left.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459894&siteId=1