开发者

Ant loadfile override property

I'm trying to use the Ant task <loadfile> in a loop to parse the contents of a file. I have something like

<loadfile srcFile="@{some.input}" property="my.property">

Since Ant properties are immutable, this doesn't work for me. I need 'my.property' to update on every iteration. Is there a way to achieve this? I know An开发者_高级运维t-contrib has a <var> task but I'm not sure how to use <loadfile> with it.

Any recommendations?

Thanks.


<loadfile property="foo" srcfile="bar.txt"/>

 ... do some actions, perhaps in a <for> loop ...

<var name="foo" unset="true"/>

You can then use foo again in <loadfile>


The Ant plugin Flaka provides a let task, allowing to overwrite existing properties or variables like that =

<project xmlns:fl="antlib:it.haefelinger.flaka">

<property name="my.property" value="value"/>
<fl:let> my.property ::= 'anothervalue'</fl:let>

</project>

So no need to unset first and set afterwards. btw. Flaka has a unset task also ;-)


Ant contrib also has a var task that unsets.

Lucks: It is convention to accept one of the answers so people know the question is resolved. I recommend you accept Gilbert's since he post a correct answer first.


One of the built-in tasks that are able to override the property value is script.

Below is a script and the output that proves the property value changed.

<project name="test">
  <property name="bshJar" value="C:\lang\java\bsh-1.3.0.jar:C:\lang\java\bsf.jar:C:\lang\java\commons-logging-1.1.1.jar" />
  <property name="a" value="first" />
  <echo>a=${a}</echo>
  <script manager="bsf" language="beanshell" classpath="${bshJar}"><![CDATA[
  project.setProperty("a", "fourth");
  ]]></script>
  <echo>a=${a}</echo>
</project>

Output:

a=first
a=fourth


I just ended up using the <unset> task provided by Antelope http://antelope.tigris.org/


Did you try the script in ANT.

<script language="javascript">
project.setProperty("my.property", "somevalue");
</script>


You could create a new property in your MacroDef for each srcFile:

<loadfile srcFile="@{some.input}" property="@{some.input}_Prop">

<echo message="@{some.input} Contents: ${@{some.input}_Prop}"/>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜