简体   繁体   中英

Conditional task execution in ANT Scripts

My requirement is pretty simple, I have an ANT task which is handling exceptions internally and not throwing any exception, instead it is throwing custom messages [these are not exceptions] to the console. A sample is shown below with the test "The workspace with the specified name does not exist".

在此处输入图片说明

My requirement here is, if there is any such message apart from "Build Successful", I should make sure my ANT script is failed so that it won't go further. But I am unable to do so as I don't know how do I read that custom message which was written to console.

I tried exploring on 'Record' task but I was unsuccessful as this log was written only to console and not to the file (don't know why). But even if it was written to a file I should ideally read each line of file to find out a particular text present or not.

Is there a simple way to try and read things from console which were executed before?

<target name="build">
    <record name="test.txt" action="start" append="true" loglevel="verbose" />
    <echo>Welcome to Apache Ant! Building the project in Cordys Middleware</echo>
    <property environment="env"/>
    <property name="bop.install.dir" value="${env.CORDYS_HOME}"/>
    <exec executable="${bop.install.dir}/components/cws/scripts/linux/CWSPackage.sh" failonerror="true" resultproperty="output">        
        <env key="CLASSPATH" value="/opt/Cordys/Oracle_Jar/ojdbc6.jar"/>
        <arg value="${ORG_NAME}"/>
        <arg value="${WORKSPACE_NAME}"/>
        <arg value="${PROJECT_NAME}"/>      
    </exec>
    <echo>Finishing the build</echo>
    <record name="test.txt" action="stop"/>
    <echo>${output}</echo>
    <fail>Something wrong here.</fail> <!-- I want to throw this error conditionally -->
</target>

What you're looking for is the exec task's outputproperty attribute.

You can do something like this:

<exec executable="${my.executable}" outputproperty="exec.output">
    <arg value="${my.arg}" />
</exec>

<fail message="Invalid output from exec task">
    <condition>
        <contains string="${exec.output}" substring="The workspace with the specified string does not exist." />
    </condition>
</fail>

Multiple conditions (any level of complexity in the boolean is allowed):

<fail message="Invalid output from exec task">
    <condition>
        <and>
            <not>
                <contains string="${exec.output}" substring="SUCCESS" />
            </not>
            <or>
                <contains string="${exec.output}" substring="ERROR" />
                <contains string="${exec.output}" substring="FAILED" />
            <or>
        </and>
    </condition>
</fail>

Regex:

<fail message="Invalid output from exec task">
    <condition>
        <matches string="${exec.output}" pattern="The .* does not exist." />
    </condition>
</fail>
<!-- *  This is an ANT script to build the project in development environment.
        Steps involved in this are
            * Building the project
            * Publishing the project
            * Creating the package for the project
 -->

<!-- 
    Sample command to execute this 
    ant build -DORG_NAME=businessservices3 -DWORKSPACE_NAME=ConfigurationManagement -DPROJECT_NAME='ConfigurationManagement'
 -->
<project name="Building Project" default="build">
    <property file="${PROJECT}" /> 
    <target name="build">
        <echo>Welcome to Apache Ant! Building the project in Cordys Middleware</echo>
        <property environment="env"/>
        <property name="bop.install.dir" value="${env.CORDYS_HOME}"/>

        <exec executable="${bop.install.dir}/components/cws/scripts/linux/CWSPackage.sh" outputproperty="exec.output">      
            <env key="CLASSPATH" value="/opt/Cordys/Oracle_Jar/ojdbc6.jar"/>
            <arg value="${ORG_NAME}"/>
            <arg value="${WORKSPACE_NAME}"/>
            <arg value="${PROJECT_NAME}"/>      
        </exec>
        <fail message="Build not successful for the project ${PROJECT_NAME}">
            <condition>
                <not>
                    <contains string="${exec.output}" substring="Operation  completed successful" />
                </not>
            </condition>
        </fail>

    </target>
</project>

This worked for me after so much of trail and error methods. Thanks Austin. Even though this is the ANT which is working, I would accept your answer only as this is a modified version of what you told :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM