开发者

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

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 开发者_StackOverflow中文版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


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜