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 the pom.xml, it will be set.
  • If propertyName already exists in the pom.xml, its value will be overwritten by the one passed as argument via -D.

To send multiple variables, use multiple space delimited -Ds:

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 themes value during this execution, having the effect as if you had:

<properties>
    <theme>halloween</theme>
</properties>
Share:
43,836
jiafu
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, 2020

Comments

  • jiafu
    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
    jiafu almost 11 years
    if I want to set more the one property how to write it? -Dxx=11 -Dyy=2?
  • earcam
    earcam almost 11 years
    @jiafu yes, just space delimited
  • acdcjunior
    acdcjunior almost 11 years
    Yes, as #earcam said, multiple space separated -Ds 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é
    Olivier Boissé over 6 years
    Do 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
    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.