TERACytE
TERACytE

Reputation: 7853

How to identify and set a missing environment property in Maven?

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?

Upvotes: 10

Views: 13376

Answers (3)

Arnab
Arnab

Reputation: 154

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

Upvotes: 8

tonio
tonio

Reputation: 493

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>

Upvotes: 2

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45576

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>

Upvotes: 14

Related Questions