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
Instead of environment variables, we recommend that you either use a YAML configuration file or a shared data source.
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.
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.
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.
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
activeByDefault
is set to true with purpose to enable it by default.!myproperty
means this property is missing or not existed.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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With