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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With