Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven properties loading order

Tags:

java

maven

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?

like image 747
Mincong Huang Avatar asked Apr 01 '18 13:04

Mincong Huang


People also ask

What is properties file in Maven?

The Properties Maven Plugin is here to make life a little easier when dealing with properties. It provides goals to read properties from files and URLs and write properties to files, and also to set system properties. It's main use-case is loading properties from files or URLs instead of declaring them in pom.

Where do Maven properties go?

Properties can be defined in a POM or in a Profile. The properties set in a POM or in a Maven Profile can be referenced just like any other property available throughout Maven. User-defined properties can be referenced in a POM, or they can be used to filter resources via the Maven Resource plugin.

What is the difference between plugin and pluginManagement tags?

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.

What do artifactId attributes define?

artifactId. This is an Id of the project. This is generally name of the project. For example, consumer-banking. Along with the groupId, the artifactId defines the artifact's location within the repository.


1 Answers

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.

like image 114
SilverNak Avatar answered Oct 13 '22 00:10

SilverNak