Setting default values for custom Maven 2 properties

Maven 2Properties

Maven 2 Problem Overview


I have a Maven pom.xml with a plugin that I want to be able to control on the command line. Everything works otherwise fine, except even after searching the net a while I can't figure out how to set a default value for my control property:

<plugin>
    ...
    <configuration>
        <param>${myProperty}</param>
    </configuration>
    ...
</plugin>

So if I run Maven with

mvn -DmyProperty=something ...

everything's fine, but I'd like to have a specific value assigned to myProperty also without the -DmyProperty=... switch. How can this be done?

Maven 2 Solutions


Solution 1 - Maven 2

You can have the property default value defined in <build>/<properties> or in a profile like shown below. When you supply the property value on command line with -DmyProperty=anotherValue then it will override the definition from the POM. That is, all definitions of property values in the POM are set only a default value for the properties.

<profile>
    ...
    <properties>
        <myProperty>defaultValue</myProperty>            
    </properties>
    ...
       <configuration>
          <param>${myProperty}</param>
       </configuration>
    ...
</profile>

Solution 2 - Maven 2

Taylor L's approach works fine, but you don't need the extra profile. You can just declare property values in the POM file.

<project>
  ...
  <properties>
    <!-- Sets the location that Apache Cargo will use to install containers when they are downloaded. 
         Executions of the plug-in should append the container name and version to this path. 
         E.g. apache-tomcat-5.5.20 --> 
    <cargo.container.install.dir>${user.home}/.m2/cargo/containers</cargo.container.install.dir> 
  </properties> 
</project>

You can also set properties in your user settings.xml file in the event that you want each user to be able to set their own defaults. We use this approach to hide credentials that the CI server uses for some plug-ins from regular developers.

Solution 3 - Maven 2

You could use something like below:

<profile>
    <id>default</id>
    <properties>
        <env>default</env>
        <myProperty>someValue</myProperty>            
    </properties>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
</profile>

Solution 4 - Maven 2

@akostadinov's solution works great for common usage... But if the desired property shall be used by reactor component during dependency resolution phase (very early in mvn pom hierarchy processing...) you should use profile "none activation" test mechanism to ensure the optional command line provided value is always prioritized regarding the value provided inside pom.xml. And this whatever deep is your pom hierarchy.

To do so, add this kind of profile in your parent pom.xml :

 <profiles>
    <profile>
      <id>my.property</id>
      <activation>
        <property>
          <name>!my.property</name>
        </property>
      </activation>
      <properties>
        <my.property>${an.other.property} or a_static_value</my.property>
      </properties>
    </profile>
  </profiles>

Solution 5 - Maven 2

This might work for you:

<profiles>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
     <plugin>
       <configuration>
        <param>Foo</param>
       </configuration>
     </plugin>
    </build>
    ...
  </profile>
  <profile>
    <id>notdefault</id>
    ...
     <build>
      <plugin>
        <configuration>
            <param>${myProperty}</param>
        </configuration>
     </plugin>
     </build>
    ...
  </profile>
</profiles>

That way,

mvn clean will use "foo" as your default param. In cases when you need to override, use mvn -P notdefault -DmyProperty=something

Solution 6 - Maven 2

I took sal's approach but flatten it a bit.

<profiles>
  <profile>
    <id>default</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    <build>
     <plugin>
       <configuration>
        <version>LATEST</version>
       </configuration>
     </plugin>
    </build>
  </profile>
</profiles>

Now you have 2 options:

  1. Using default value: MVN install (all $version will be replaced with LATEST)

  2. Using own value: MVN install -P! Default -Dversion=0.9 (all $version will be 0.9)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEemeli KantolaView Question on Stackoverflow
Solution 1 - Maven 2akostadinovView Answer on Stackoverflow
Solution 2 - Maven 2DavidValeriView Answer on Stackoverflow
Solution 3 - Maven 2Taylor LeeseView Answer on Stackoverflow
Solution 4 - Maven 2Franck BoninView Answer on Stackoverflow
Solution 5 - Maven 2salView Answer on Stackoverflow
Solution 6 - Maven 2DaveView Answer on Stackoverflow