Use logic to determine condition in ant

Source: Internet
Author: User
Tags echo message

I haven't written the ant script for a long time. In the past two days, I have used ant to install and deploy Web applications. To achieve multi-version compatibility with Web servers, logical judgment is required, for example, I want to determine whether it is installed on weblogic8 or weblogic9, but it is really troublesome to process logic judgment in ant. It can only work on tasks. I need to use property to make judgments and use available to set the property. For example:

<? XML version = "1.0" encoding = "gb2312"?>
<Project name = "WebLogic ant task" default = "build">
<Target name = "detect. File">
<Condition Property = "fileisexists">
<And>
<Available file = "C:/123.txt"/>
</And>
</Condition>
</Target>
<Target name = "echodemo" If = "fileisexists" depends = "detect. File">
<Echo message = "Hello ant"/>
</Target>
<Target name = "build">
<Antcall target = "echodemo"/>
</Target>
</Project>
If a file exists, the value of fileisexists is true. Before executing the task of echodemo, the value of fileisexists is true. If the value is not true, the task is not executed. If there is 123.txt under the C drive, the hello ant will be printed; otherwise, the hello ant will not be printed.

There is also a small trap here. I am used to using antcall and do not like depends. However, if I use antcall, there will be problems. For example, I did not write it in the first place.

<? XML version = "1.0" encoding = "gb2312"?>
<Project name = "WebLogic ant task" default = "build">
<Target name = "detect. File">
<Condition Property = "fileisexists">
<And>
<Available file = "C:/123.txt"/>
</And>
</Condition>
</Target>
<Target name = "echodemo" If = "fileisexists">
<Echo message = "Hello ant"/>
</Target>
<Target name = "build">
<Antcall target = "detect. File"/>
<Antcall target = "echodemo"/>
</Target>
</Project>

If antcall is used, the attribute fileisexists is never true when the task echodemo is executed, even after detect is executed. file is true, but it is not passed to the next task. ant has not been studied in depth, so the specific internal implementation is unknown.

The following is ant's official reference document.

For more complex information, see

Http://ant.apache.org/manual/CoreTasks/conditions.html

Conditiondescription

Sets a property if a certain condition holds true-this is a generalization of available and uptodate.

If the condition holds true, the property value is set to true by default; otherwise, the property is not set. You can set the value to something other than the default by specifyingvalueAttribute.

Conditions are specified as nested elements, you must specify exactly one condition.

Parameters

Attribute Description Required
Property The name of the property to set. Yes
Value The value to set the property to. defaults to "true ". No
Else The value to set the property to if the condition evaluatesFalse. By default the property will remain unset.Since ant 1.6.3 No
Parameters specified as nested Elements

All conditions to test are specified as nested elements, for a complete list see here.

Examples
  <condition property="javamail.complete">    <and>      <available classname="javax.activation.DataHandler"/>      <available classname="javax.mail.Transport"/>    </and>  </condition>

Sets the propertyjavamail.completeIf both the JavaBeans activation framework and javamail are available in the classpath.

  <condition property="isMacOsButNotMacOsX">    <and>      <os family="mac"/>      <not>        <os family="unix"/>      </not>    </and>  </condition>

Sets the propertyisMacOsButNotMacOsXIf the current operating system is MacOS, but not MacOS X-which ant considers to be in the Unix family as well.

  <condition property="isSunOSonSparc">    <os name="SunOS" arch="sparc"/>  </condition>

Sets the propertyisSunOSonSparcIf the current operating system is SunOS and if it is running on a iSCSI architecture.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.