如果未在Maven中为资源过滤设置环境变量,如何使用默认值? [英] How can I use a default value if an environment variable isn't set for resource filtering in maven?

查看:383
本文介绍了如果未在Maven中为资源过滤设置环境变量,如何使用默认值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用资源过滤来替换属性文件中的某些$ {values}.

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

例如该文件包含 PROPERTY = $ {VALUE}

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

我希望将$ {VALUE}替换为环境变量$ VALUE,如果在构建运行时设置了$ VALUE,该变量将很好地工作.惊人的.

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

但是,这些环境变量仅在我们的官方构建环境中(由Jenkins设置),而不是在开发人员构建中设置,因此$ {values}在过滤后会留在属性文件中,这可能会破坏内容.我宁愿不要在开发人员环境中使用env vars,因为它总是会导致脆弱的开发人员构建和脆弱的开发人员.

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.

如果设置了环境变量,如何使用环境变量值;如果未设置env var,如何使用另一个默认属性值?

根据我的测试,默认情况下它会以其他方式工作,因为pom中设置的属性将覆盖环境变量以进行资源过滤.

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.

谢谢

推荐答案

我正在使用配置文件确定为

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>

请注意

  1. activeByDefault设置为true是为了默认启用它.
  2. !myproperty表示此属性丢失或不存在.
  3. 如果myproperty不存在,只需使用在properties处定义的myproperty.
  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.

您可能会在 http://maven.apache中看到更多信息. org/guides/introduction/introduction-to-profiles.html

我希望这可以帮助您达到要求.

I hope this may help to achieve your requirement.

此致

Charlee Ch.

Charlee Ch.

这篇关于如果未在Maven中为资源过滤设置环境变量,如何使用默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆