简体   繁体   English

ant属性文件被属性任务覆盖

[英]ant property file overwritten by property task

I'm trying to set some global properties in an ant project. 我正在尝试在ant项目中设置一些全局属性。

<property file="env/${deploy.servername}.properties" />
<property name="deploy.username" value="${env.USERNAME}" />

As the ant task is being triggered by a jenkins parameterized project env.USERNAME is always set but not always correct. 由于ant任务是由jenkins参数化的项目触发的,因此始终会设置env.USERNAME但并不总是正确的。

My goal with this design is that if the property deploy.username is present in the file at deploy.servername the file should set the property and as ant shouldn't allow the property to be reset the value at env.USERNAME should be ignored. 我的设计目标是,如果文件deploy.servername存在属性deploy.username则文件应设置该属性,而作为ant 不应允许重置该属性,则应忽略env.USERNAME处的值。

Unfortunately the value provided by env.USERNAME is alway the value used by the script. 不幸的是, env.USERNAME提供的值env.USERNAME是脚本使用的值。 Why? 为什么? Does ant evaluate property name=* before property file=* ? ant是否在property file=*之前评估property name=* property file=* Is a property set by property file=* mutable? 通过property file=*设置的property file=*是否可变?

Thanks for the help 谢谢您的帮助

In ant a property once set is immutable by design. 在ant中,一旦设置的属性在设计上是不可变的。 There is no specific order when using different mechanisms to create a property - the first declaration of the property wins. 使用不同的机制创建属性时,没有特定的顺序-属性的第一个声明将获胜。
Given propertyfile foo.properties has : 给定的propertyfile foo.properties具有:

JAVA_HOME=some/path

Example script 示例脚本

<project>

  <property file="foo.properties"/>
  <property name="JAVA_HOME" value="foobar"/>

  <property environment="env"/>
  <property name="JAVA_HOME" value="${env.JAVA_HOME}"/>

  <echo>$${JAVA_HOME} => ${JAVA_HOME}</echo>

</project>

output 产量

[echo] ${JAVA_HOME} => some/path

That means the property is already defined somewhere else. 这意味着该属性已在其他位置定义。
Put in that line : 放在那一行:

<echo>$${deploy.username} => ${deploy.username}</echo>

before : 之前:

<property file="env/${deploy.servername}.properties"/>

to see if the property already exists. 查看该属性是否已经存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM