10

I have my build set-up so that I pass in my variable via the command line:

mvn clean install -DsomeVariable=data

In my pom I have:

<someTag>${someVariable}</someTag>

This works fine, but I would like to identify if someVariable is not specified on the command line, and then default it so that my script can continue.

Can this be done in Maven?

TERACytE
  • 7,553
  • 13
  • 75
  • 111

3 Answers3

14

You can specify default property value in the properties section of your POM file:

<properties>
  <someVariable>myVariable</someVariable>
</properties>

If you want to make sure that the property value is ALWAYS supplied on a command line, then you can use maven-enforcer-plugin.

Here is a link that shows how to enforce system property presence -> http://maven.apache.org/enforcer/enforcer-rules/requireProperty.html

I'll just copy the XML verbatim here in case the above link goes bad.

<project>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-enforcer-plugin</artifactId>
        <version>1.0.1</version>
        <executions>
          <execution>
            <id>enforce-property</id>
            <goals>
              <goal>enforce</goal>
            </goals>
            <configuration>
              <rules>
                <requireProperty>
                  <property>basedir</property>
                  <message>You must have a basedir!</message>
                  <regex>\d</regex>
                  <regexMessage>You must have a digit in your baseDir!</regexMessage>
                </requireProperty>
                <requireProperty>
                  <property>project.version</property>
                  <message>"Project version must be specified."</message>
                  <regex>(\d|-SNAPSHOT)$</regex>
                  <regexMessage>"Project version must end in a number or -SNAPSHOT."</regexMessage>
                </requireProperty>
              </rules>
              <fail>true</fail>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  [...]
</project>
Alexander Pogrebnyak
  • 44,836
  • 10
  • 105
  • 121
  • Thank-you for the insightful response Alex. Though I am looking to default the property rather than enforce it. So if it is not specified then the script would use the default value. – TERACytE Nov 07 '11 at 22:53
  • @TERACytE. Yes, a value in `properties` section is a default, and you can always overwrite it from command line. – Alexander Pogrebnyak Nov 08 '11 at 03:41
8

You can specify the default value as

<properties>
      <someTag>defaultValue</someTag>
</properties>

When you run maven command, you can override that value like this

mvn clean package -DsomeTag=newSpecificValue
Kai
  • 700
  • 7
  • 33
Arnab
  • 154
  • 2
  • 6
2

You can use profiles instead, but you'll need a profile for each variable.

 <profile>
    <id>default-value-1</id>
    <activation>
          <activeByDefault>false</activeByDefault>
          <property>
             <name>!someVariable</name>
          </property>
    </activation>
    <properties>
        <someVariable>DEFAULT-VALUE</someVariable>
    </properties>
</profile>
tonio
  • 484
  • 5
  • 15