How to use the mvn -D to set (multiple) properties in Maven via command line?
43,836
The proper way to set a property via command-line using -D
is:
mvn -DpropertyName=propertyValue clean package
- If
propertyName
doesn't exist in thepom.xml
, it will be set. - If
propertyName
already exists in thepom.xml
, its value will be overwritten by the one passed as argument via-D
.
To send multiple variables, use multiple space delimited -D
s:
mvn -DpropA=valueA -DpropB=valueB -DpropC=valueC clean package
You can check more details about properties in Maven: The Complete Reference. More specifically, in section: 6.1. Maven Command Line Options/6.1.1. Defining Properties.
Example:
If you have in your pom.xml
:
<properties>
<theme>myDefaultTheme</theme>
</properties>
Then mvn -Dtheme=halloween clean package
would overwrite theme
s value during this execution, having the effect as if you had:
<properties>
<theme>halloween</theme>
</properties>
Author by
jiafu
I am a Java and C++ fans. Every day I encounter huge accounts of puzzles when coding. I hope I can make faster progress with your helps!
Updated on June 01, 2020Comments
-
jiafu almost 4 years
How to use the
mvn -D
in maven? How to set a property (or multiple properties) using it?Are there any official articles for
mvn -D
?I couldn't find one. Thanks.
-
jiafu almost 11 yearsif I want to set more the one property how to write it? -Dxx=11 -Dyy=2?
-
earcam almost 11 years@jiafu yes, just space delimited
-
acdcjunior almost 11 yearsYes, as #earcam said, multiple space separated
-D
s is the correct way, just like you did:-Dxx=11 -Dyy=2
. Now if you have several variables that are usually used together, you should consider using profiles. Check the item "6.1.3. Using Build Profiles" of that reference link I posted in the answer. -
Olivier Boissé over 6 yearsDo I need to place the system properties before the clean package command ? I don't understand why
mvn clean package -DskipTests
works, whereas the skipTests is placed after the command clean package -
acdcjunior over 6 years@OlivierBoissé It works because it doesn't matter if you put the variables before or after the goals, simples as that.