How can I use a default value if an environment va

2019-03-09 07:34发布

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

标签: maven maven-3
2条回答
Animai°情兽
2楼-- · 2019-03-09 07:51

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.

查看更多
手持菜刀,她持情操
3楼-- · 2019-03-09 07:57

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>
查看更多
登录 后发表回答