Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use a default value if an environment variable isn't set for resource filtering in maven?

Tags:

maven

maven-3

I am using resource filtering to replace some ${values} in a property file.

e.g. the file contains PROPERTY=${VALUE}

I want ${VALUE} to be replaced with environment variable $VALUE which works well if $VALUE is set when the build runs. Awesome.

However, these env vars are only set in our official build environment (by Jenkins) and not in developer builds so the ${values} are left in the property file after filtering which can break stuff. I'd rather not require env vars in developer environments as that always leads to fragile dev builds and whiny devs.

How can I use the environment variable value if its set and use another default property value if the env var isn't set?

From my testing it works the other way around by default, in that properties set in the pom will override environment variables for the purpose of resource filtering.

Thanks

like image 326
John Russell Avatar asked Jun 30 '11 01:06

John Russell


People also ask

What can I use instead of environment variables?

Instead of environment variables, we recommend that you either use a YAML configuration file or a shared data source.

How can we set default value to the variable?

You can set the default values for variables by adding ! default flag to the end of the variable value. It will not re-assign the value, if it is already assigned to the variable.

Why do we need to set environment variables?

Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings.

When should we set environment variables?

The primary use case for environment variables is to limit the need to modify and re-release an application due to changes in configuration data.


2 Answers

I'm using the profile for determining as

<profiles>   <profile>     <activation>       <activeByDefault>true</activeByDefault>       <property>         <name>!myproperty</name>       </property>     </activation>     ...     <properties>        <myproperty>some value</myproperty>     </properties>   </profile>   ... </profiles> 

Please note

  1. The activeByDefault is set to true with purpose to enable it by default.
  2. The !myproperty means this property is missing or not existed.
  3. If the myproperty is not existed, just use the myproperty defined at the properties instead.

You may see further information at http://maven.apache.org/guides/introduction/introduction-to-profiles.html

I hope this may help to achieve your requirement.

Regards,

Charlee Ch.

like image 198
Charlee Chitsuk Avatar answered Oct 17 '22 07:10

Charlee Chitsuk


Have the same issue in our development group when using an environment value to denote a file system path - specifically difference between linux and windows.

Based on the other solution on the same question:

<profile>     <id>MY_VAR default value</id>     <activation>         <property>             <name>!env.MY_VAR</name>         </property>     </activation>     <properties>         <env.MY_VAR>default value</env.MY_VAR>     </properties> </profile> 
like image 36
AlikElzin-kilaka Avatar answered Oct 17 '22 09:10

AlikElzin-kilaka