执行只是如果条件满足Ant任务 [英] Execute ANT task just if a condition is met

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

问题描述

我只需要,如果条件得到满足特定的目标内执行的Ant任务。

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

我发现了一种方法,以限定目标水平的情况下,但不是在任务级别。我还发现,实现了一个如果一个贡献的任务

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服务器。要检测是否要停止我用下面的code:

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} 是假的。正如我所说的,我不想在目标添加一个任务使用,除非属性。

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目标可以有一个可选的如果除非子句。这意味着执行只有在属性设置的任务,用如果子句,或者未设置与除非 1 。有趣的是,如果除非子句检查之后任何相关任务首先执行。

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 clause1. 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来执行目标 my.target 。蚂蚁的通知 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.

如果你想要去的蚂蚁的Contrib路线,有一个简单的方法来设置蚂蚁的Contrib,所以蚂蚁contrib请罐实际上是项目的一部分。如果有人检查出从版本控制系统的项目,他们也将获得蚂蚁contrib请罐,因而不会有安装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.

下载蚂蚁的Contrib罐子,并把它放到一个目录在您的项目名为的antlib / AC 的根。在的antlib 可用于各种可选任务jar文件如 FindBugs的<中/ A>或PMD。只要把每一个可选的Ant JAR在自己的目录的antlib (就像我把蚂蚁的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 ,您指定的蚂蚁的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 这是正确的,在如果/除非子句检查是否属性设置,而不是真实 / 这可能会导致很多混乱。我见过开发商属性设置为没有,然后不知道为什么目标实际上是因为执行如果子句设置为false。

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任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆