java Ant 和可用任务 - 如果某些东西不可用怎么办?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/133710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ant and the available task - what if something is not available?
提问by Alex Miller
When I use the task, the property is only set to TRUE if the resource (say file) is available. If not, the property is undefined.
当我使用任务时,如果资源(比如文件)可用,则该属性仅设置为 TRUE。如果不是,则该属性未定义。
When I print the value of the property, it gives true if the resource was available, but otherwise just prints the property name.
当我打印属性的值时,如果资源可用,它会给出 true,否则只打印属性名称。
Is there a way to set the property to some value if the resource is notavailable? I have tried setting the property explicitly before the available check, but then ant complains:
如果资源不可用,有没有办法将属性设置为某个值?我已经尝试在可用检查之前明确设置属性,但是蚂蚁抱怨:
[available] DEPRECATED - used to override an existing property. [available] Build file should not reuse the same property name for different values.
回答by Alex Miller
You can use a condition in combination with not:
您可以将条件与 not 结合使用:
http://ant.apache.org/manual/Tasks/condition.html
http://ant.apache.org/manual/Tasks/condition.html
<condition property="fooDoesNotExist">
<not>
<available filepath="path/to/foo"/>
</not>
</condition>
回答by Alex Miller
<available filepath="/path/to/foo" property="foosThere" value="true"/>
<property name="foosThere" value="false"/>
The assignment of foosThere will only be successful if it has not already been set to true by your availability check.
foosThere 的分配只有在您的可用性检查尚未设置为 true 时才会成功。
回答by Mnementh
The reason for this behaviour are the if/unless-attributes in targets. The target with such an attribute will be executed if/unless a property with the name is set. If it is set to false or set to true makes no difference. So you can use the available-task to set (or not) a property and based on this execute (or not) a task. Setting the property before the available-task is no solution, as properties in ant are immutable, they cannot be changed once set.
这种行为的原因是目标中的 if/unless 属性。如果/除非设置了具有名称的属性,则将执行具有此类属性的目标。如果设置为 false 或设置为 true 没有区别。因此,您可以使用 available-task 来设置(或不设置)属性并基于此执行(或不执行)任务。在available-task之前设置属性不是解决方案,因为ant中的属性是不可变的,一旦设置就无法更改。
There are three possible solutions, to set a property to a value if unset before:
有三种可能的解决方案,如果之前未设置,则将属性设置为值:
- You use the available-task in combination with not.
- You create a task setting the property, that will be executed only if the property is unset (unless-attribute of task).
- You simply set the property afterthe call to available. As the property will only be changed if unset, this will do what you want.
- 您可以将 available-task 与 not 结合使用。
- 您创建一个设置属性的任务,只有在未设置属性(除非任务的属性)时才会执行该任务。
- 您只需在调用 available之后设置属性。由于该属性仅在未设置时才会更改,因此这将执行您想要的操作。

