本文介绍了只要满足条件就执行 Ant 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有在满足条件时,我才需要在特定目标内执行 Ant 任务.

I need to execute an Ant task within a specific target only if a condition is met.

我找到了一种在目标级别而不是在任务级别定义条件的方法.我还发现了一个实现 IF 任务的贡献.

I found a way to define the condition at the target level, but not at the task level. I have also found a contribution that implements an IF task.

我的问题是,您是否知道通过标准 Ant 任务实现此目标的任何方法?

My question is, are you aware of any way to achieve this objective with standard Ant tasks?

更长的解释:我正在尝试启动 Tomcat 服务器,以防它停止.要检测它是否停止,我使用以下代码:

Longer explanation: I am trying to start Tomcat Server in case it is stopped. To detect if it is stopped I use following code:

<echo message="Checking whether Tomcat is running"/>
<condition property="tomcat.running">
  <socket server="${tomcat.host}" port="${tomcat.port}"/>
</condition>

所以我在这个目标中的下一个任务是一个 exec 任务,只有当 ${tomcat.running} 为 false 时才应该执行.正如我所说,我不想在目标中添加单个任务来使用除非属性.

So my next task in this target, is an exec task that should be executed only if ${tomcat.running} is false. And as I said, I don't want to add a single task in a target to use the unless property.

推荐答案

Ant 目标可以有一个可选的 ifunless 子句.这意味着仅当使用 if 子句设置属性或未使用 unless 子句 设置属性时才执行任务.有趣的是,ifunless 子句在 after 任何依赖任务首先执行后被检查.

An Ant target can have an optional if or unless clause. This means to execute the task only if the property is set, with an if clause, or is unset with the unless clause. Interestingly, that if or unless clause is checked after any dependent task is first executed.

这意味着,您可以在标准 Ant 中执行此操作,作为仅在满足特定条件时执行 Ant 任务的一种方式:

This means, you can do this in standard Ant as a way of executing an Ant task only if a particular condition is met:

 <target name="test.if.tomcat.is.running">
      <condition property="tomcat.running">
          <socket server="${tomcat.host}" port="${tomcat.port}"/>
      </condition>
</target>

<target name="my.target"
    if="tomcat.running"
    depends="test.if.tomcat.is.running">
    <yaddah/>
    <yaddah/>
    <yaddah/>
</target>

您指定希望 Ant 执行 Target my.target.Ant 注意到 my.target 依赖于 test.if.tomcat.is.running 目标,并将首先执行它.如果 Tomcat 实际正在运行,test.if.tomcat.is.running 任务将设置 tomcat.running 属性.否则,不会设置该属性.

You specify that you want Ant to execute Target my.target. Ant notices that my.target depends upon the test.if.tomcat.is.running target, and will execute that first. The test.if.tomcat.is.running task will set the tomcat.running property if Tomcat is actually running. Otherwise, that property is not set.

最后,Ant 会回到my.target 目标,看看是否设置了属性tomcat.running,并且只会执行目标my.target 如果已设置.

Finally, Ant will go back to the my.target target and see if the property tomcat.running is set, and will only execute the target my.target if it is set.

或者,您可以使用 Ant-contrib 任务,这可能会使您的整个构建过程更容易理解.

Or, you can use the Ant-contrib tasks which may make your entire build process easier to understand.

如果你想走 Ant-Contrib 路线,有一个简单的方法来设置 Ant-Contrib,所以 Ant-contrib jar 实际上是你项目的一部分.如果有人从版本控制系统检出您的项目,他们也会获得 Ant-contrib jar,因此不必自己安装 Ant-Contrib.

If you want to go the Ant-Contrib route, there's an easy way to setup Ant-Contrib, so the Ant-contrib jar is actually part of your project. If someone checks out your project from the version control system, they'll also get the Ant-contrib jar, and thus won't have to install Ant-Contrib themselves.

下载 Ant-Contrib jar,并将其放入项目根目录中名为 antlib/ac 的目录中.antlib 可用于各种可选任务 jar,例如 Findbugs 或 PMD.只需将每个可选的 Ant jar 放在 antlib 下各自的目录中(就像我将 Ant-Contrib 放在 ac 目录下一样).

Download the Ant-Contrib jar, and put it into a directory in the root of your project called antlib/ac. The antlib can be used for all sorts of optional task jars such as Findbugs or PMD. Just put each optional Ant jar in their own directory under antlib (Like I put Ant-Contrib under the ac directory).

然后,在您的 build.xml 中,您以这种方式指定 Ant-Contrib 任务:

Then, in your build.xml, you specify the Ant-Contrib tasks this way:

<taskdef resource="net/sf/antcontrib/antlib.xml">
   <classpath>
       <fileset dir="${basedir}/antlib/ac"/>
   </classpath>
</taskdef>

现在,您可以使用 Ant-Contrib 任务而不必担心它们是否安装在特定机器上.您签出您的项目,并且您可以自动访问这些任务.

Now, you can use the Ant-Contrib tasks without worrying whether or not they're installed on a particular machine or not. You checkout your project, and you have access to those tasks automatically.

1. 没错,if/unless 子句检查属性是否已设置,而不是 true/false 这可能会引起很多混乱.我见过开发人员将属性设置为 falseno,然后想知道为什么目标实际上在执行,因为 if 子句设置为假的.

1. That's right, the if/unless clause checks if a property is set and not true/false which can cause a lot of confusion. I've seen developers set a property to false or no, and then wonder why the target is actually executing since the if clause is set to false.

这篇关于只要满足条件就执行 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 18:38