Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 825
TERACytE Avatar asked Nov 07 '11 22:11

TERACytE


2 Answers

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>
like image 185
Alexander Pogrebnyak Avatar answered Sep 29 '22 11:09

Alexander Pogrebnyak


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
like image 34
Arnab Avatar answered Sep 29 '22 11:09

Arnab