简体   繁体   English

如何在ANT中重置属性?

[英]How to reset a property in ANT?

I'm writing a velocity macro within which I have some ant tasks. 我正在编写一个速度宏,其中有一些蚂蚁任务。 Within a #foreach loop in the velocity macro, I have a pathconvert task: 在速度宏的#foreach循环中,我有一个pathconvert任务:

#foreach(<iterate through something>)        
        <pathconvert property='filename' refid='swf.file'>
          <mapper>
            <chainedmapper>
                <flattenmapper/>
                <globmapper from='*-d.swf' to='*'/>
            </chainedmapper>
          </mapper>
        </pathconvert>
#end

The problem I have is that the 'filename' property gets set only once, during the first iteration, since properties in ANT are immutable. 我的问题是,由于ANT中的属性是不可变的,因此在第一次迭代期间,“ filename”属性仅设置了一次。 But I need the filename to be set during each iteration. 但是我需要在每次迭代期间设置文件名。 Is there a way to get this done? 有办法完成这项工作吗?

If there was a way to reset the property, I could do that at the end of each iteration. 如果有一种方法可以重置该属性,则可以在每次迭代结束时执行此操作。 Or is there a better way to do this? 还是有更好的方法来做到这一点? Any help would be highly appreciated! 任何帮助将非常感谢!

Thanks in advance, Anand 在此先感谢,阿南德

You could use ant-contrib's variables. 您可以使用ant-contrib的变量。 They act like mutable properties. 它们的行为就像可变属性。

http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html http://ant-contrib.sourceforge.net/tasks/tasks/variable_task.html

Use the new lexically scoped properties in Ant 1.8: 在Ant 1.8中使用新的词法范围属性:

"Lexically scoped local properties, ie properties that are only defined inside a target, sequential block or similar environment." “按词法定义的局部属性,即仅在目标,顺序块或类似环境中定义的属性。”

Annoucement. Annoucement。

Properties in Ant were designed to be immuatable, but they gave in to popular demand and gave us variables. Ant中的属性设计为不可变的,但它们满足了大众的需求并为我们提供了变量。 Your alternative is to write a custom task ( in Java or a Dynamic Language) but this seems like a good compromise. 您的替代方法是编写一个自定义任务(使用Java或动态语言),但这似乎是一个不错的折衷方案。

The following snippet illustrates an ant property which I guess is not documented. 以下代码段说明了一个蚂蚁属性,我猜它没有记录。 Properties are immutable, but references are mutable . 属性是不可变的,但引用是可变的 So any data type which has no name, but a reference, is mutable. 因此,没有名称但有引用的任何数据类型都是可变的。 For example a fileset . 例如一个fileset But today I found a way to have a kind of mutable property. 但是今天,我找到了一种具有可变属性的方法。 Connected with local task or some other tricks it may be a way of having variables in ant. local任务或其他技巧联系在一起,这可能是在ant中添加变量的一种方式。

<property name="a" value="aaa" id="refa" />
<property name="b" refid="refa" />
<echo>${b}</echo>
<property name="c" value="ccc" id="refa" />
<property name="d" refid="refa" />
<echo>${d}</echo>

The output is: 输出为:

aaa
ccc

Although in both cases a reference refa is printed. 尽管在两种情况下refa会打印参考refa

Here is a post about it . 这是有关它的帖子 And another one . 还有一个

Use a combination of for + let task from Ant Plugin Flaka to overwrite existing properties. 使用Ant Plugin Flaka中的for + let任务的组合来覆盖现有属性。
See some snippets here . 在此处查看一些摘要

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

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