Ant 更新属性文件后重新加载属性
我正在尝试在使用propertyfile任务更新属性后读取它。差不多Ant 更新属性文件后重新加载属性,ant,properties,Ant,Properties,我正在尝试在使用propertyfile任务更新属性后读取它。差不多 <property file="test.properties" /> <echo>before :: ${modules}</echo> <propertyfile file="test.properties" > <entry key="modules" type="string" operation="+" value="foo" /> </pro
<property file="test.properties" />
<echo>before :: ${modules}</echo>
<propertyfile file="test.properties" >
<entry key="modules" type="string" operation="+" value="foo" />
</propertyfile>
<property file="${status.path}/test.properties" />
<echo>after :: ${modules}</echo>.
之前:${modules}
之后:${modules}。
它似乎没有第二次加载。但是属性文件是更新的。Ant属性是不可变的-一旦设置,它们就被修复了。因此,重新加载属性文件不会刷新已设置属性的值。正如前面提到的sudocode,核心Ant属性是不可变的,这是有充分理由的。
使用来自的任务,您可以使用一行代码取消设置文件中设置的所有属性:
<unset file="test.properties"/>
之后<propertyfile file="test.properties" >
<entry key="modules" type="string" operation="+" value="foo" />
</propertyfile>
会有用的
提示:该任务仅适用于普通属性,不适用于xmlproperties。但是有一个简单的解决方法,只需使用
和之后的
如果您不想仅将Antelope用于该特定任务,您可以编写一个宏定义或具有类似功能的自己的任务。此宏允许您在修复一个宏后更改属性值
<macrodef name="set" >
<attribute name="name"/>
<attribute name="value"/>
<sequential>
<script language="javascript">
<![CDATA[
project.setProperty("@{name}", "@{value}");
]]>
</script>
</sequential>
</macrodef>
对于这种情况,当整个属性文件加载两次时,我建议在第一次和第二次加载时使用不同的前缀。第一个加载前缀
属性等于第一个
。使用此前缀访问属性,即属性foo
将作为first.foo
访问。然后保存属性文件并再次加载,但这次没有前缀。您将在适当的位置获得修改的属性
如果不使用前缀,第二次加载将不起任何作用,因为ant会阻止属性重写。其他人已经指出了这一点。您可以通过忽略主目标运行时属性的antcall
任务调用新的ant运行时-只需确保包含inheritAll=“false”
之前:${modules}
您可以创建新的属性文件,并将属性保存在新文件中
在下一行中提供文件的引用
完成:)
<target name="main">
<property file="test.properties"/>
<echo>before :: ${modules}</echo>
<propertyfile file="test.properties">
<entry key="modules" type="string" operation="+" value="foo" />
</propertyfile>
<antcall target="second-runtime" inheritAll="false"/>
</target>
<target name="second-runtime">
<property file="${status.path}/test.properties" />
<echo>after :: ${modules}</echo>
</target>