java 使用条件取消 Ant 中的属性和删除 Ant 中的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10644499/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:57:24  来源:igfitidea点击:

Unsetting Property in Ant and Removing Property in Ant using a condition

javaant

提问by Code Droid

How do you unset a property in ant? so that is is completely removed as a property?

你如何在 ant 中取消设置一个属性?所以那是完全删除作为一个属性?

<condition property="proguard.config" value="proguard.cfg">
      <isset property="proguarded"/>
</condition>


 <condition property="proguard.config" value="">
      <not>
      <isset property="proguarded"/>
      </not>
</condition>

This would seem to work. However proguard runs even if there is such a property as proguard.config in existence. So how do I remove proguard.config as a property altogether conditionally? I know that if proguard sees that there is a proguard.config property at all in the .properties file it is going to run.

这似乎行得通。然而,即使存在 proguard.config 这样的属性,proguard 也会运行。那么如何有条件地将 proguard.config 作为一个属性完全删除?我知道如果 proguard 发现 .properties 文件中有一个 proguard.config 属性,它就会运行。

回答by Christopher Peisert

The Ant manual Property Taskstates:

Ant手册物业工作状态:

Properties are immutable: whoever sets a property first freezes it for the rest of the build; they are most definitely not variables.

属性是不可变的:首先设置属性的人会在构建的其余部分冻结它;它们绝对不是变量。

That being said, there are a couple of workarounds:

话虽如此,有几种解决方法:

  1. Local Task- A local property at a given scope "shadows" properties of the same name at higher scopes (especially useful within Macrodefs)

  2. Ant-Contrib Variable Task- Ant-Contrib offers flexibility, but it also adds a dependency and can sometimes tempt you to write procedural Ant code that may be better expressed in an Ant script or a custom Ant task

  1. 本地任务- 给定范围内的本地属性“隐藏”更高范围内的同名属性(在 Macrodefs 中特别有用)

  2. Ant-Contrib 变量任务- Ant-Contrib 提供了灵活性,但它也增加了依赖项,有时会诱使您编写程序性 Ant 代码,这些代码可能会在 Ant 脚本或自定义 Ant 任务中更好地表达



In your example above, if the property proguardeddoes not change during the execution of your Ant project, then there is no need to unset the property. For example, you could conditionally execute targets as follows:

在上面的示例中,如果proguarded在执行 Ant 项目期间该属性没有更改,则无需取消设置该属性。例如,您可以按如下方式有条件地执行目标:

<target name="proguarded-target" if="proguarded">
  ...
</target>

<target name="not-proguarded-target" unless="proguarded">
  ...
</target>