44

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

John Russell
  • 786
  • 1
  • 7
  • 12
  • You can see my reply here for a similar question [How to identify and set a missing environment property in Maven?][1] [1]: http://stackoverflow.com/a/24456751/1877108 – Arnab Jun 27 '14 at 16:37
  • Possible duplicate of [Setting default values for custom Maven 2 properties](http://stackoverflow.com/questions/899274/setting-default-values-for-custom-maven-2-properties) – AlikElzin-kilaka Jun 29 '16 at 04:54

2 Answers2

44

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.

Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
Charlee Chitsuk
  • 8,847
  • 2
  • 56
  • 71
  • 9
    Thanks a lot for your comment. I removed the true so that it would activate based solely on the property value and I changed it to !env.myproperty so it would pick up the environment variable. But now it works perfectly. Thanks a lot for your help. – John Russell Jun 30 '11 at 19:14
  • 6
    Unfortunately, this approach does not work when you have other profiles invoked explicitly. In this case implicit profile invocations are not triggered... – Anton Dec 16 '13 at 09:08
  • 1
    It's just a default value. Why is maven so complicated? – AlikElzin-kilaka Apr 17 '16 at 11:35
  • 3
    What if I want to set default values for multiple variables? I've tried repeating the tag, which didn't work – One Two Three Jan 18 '17 at 21:28
4

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>
AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Using a profile has a bit disadvantage - won't run when running another profile, even when set as default. You need to explicitly request this profile when running other profiles. – AlikElzin-kilaka Jun 29 '16 at 04:56
  • 1
    A better solution would be to use ant, as it exports the variable for all to know. See here: http://stackoverflow.com/a/36770198/435605 – AlikElzin-kilaka Jun 29 '16 at 04:57