Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pom.xml environment variable with default fallback

Tags:

I would like to be able to use an environment variable if it's set or a default fallback value that I set in pom.xml similar to ${VARIABLE:-default} in bash. Is it possible? Something like:

${env.BUILD_NUMBER:0} 
like image 892
Gavriel Avatar asked Jan 12 '16 14:01

Gavriel


People also ask

How do you pass an environment variable in POM XML?

To refer to environment variables from the pom. xml, we can use the ${env. VARIABLE_NAME} syntax. We should remember to pass the Java version information via environment variables.

Do we need to set environment variables for Maven?

Note that if you want to use Maven, you need to have Java installed and an environment variable set up. Open Google and search for maven download.

What are maven properties?

Maven properties are value placeholders, like properties in Ant. Their values are accessible anywhere within a POM by using the notation ${X}, where X is the property. Or they can be used by plugins as default values, for example: In your case you have defined properties as version of java.


1 Answers

I wasn't really satisfied with the accepted approach, so I simplified it a little.

Basically set a default property in the normal properties block, and only override when appropriate (instead of an effective switch statement):

<properties>      <!-- Sane default -->     <buildNumber>0</buildNumber>     <!-- the other props you use --> </properties>  <profiles>     <profile>         <id>ci</id>         <activation>             <property>                 <name>env.buildNumber</name>             </property>         </activation>         <properties>             <!-- Override only if necessary -->             <buildNumber>${env.buildNumber}</buildNumber>         </properties>     </profile> </profiles> 
like image 85
nerdwaller Avatar answered Oct 08 '22 17:10

nerdwaller