alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Ant example source code file (exec.xml)

This example Ant source code file (exec.xml) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Ant tags/keywords

cygwin, cygwin, error, files, files, java, java, os, output, unix, unix, utf8, win9x, zx81

The exec.xml source code

<project name="exec-test" default="spawn" basedir=".">
    <target name="init">
        <!-- this property can be overriden programatically in the Java test case -->
        <property name="timeToWait" value="10"/>
        <!-- this property can be overriden programatically in the Java test case -->
        <property name="logFile" value="${java.io.tmpdir}/spawn.log"/>
        <property environment="env"/>
        <!-- UNIX -->
        <available file="sh" filepath="${env.PATH}" property="sh.executable"/>
        <!-- CYGWIN -->
        <available file="sh.exe" filepath="${env.PATH}" property="sh.exe.executable"/>
        <condition property="test.can.run">
            <or>
                <isset property="sh.executable"/>
                <isset property="sh.exe.executable"/>
            </or>
        </condition>
        <!-- UNIX -->
        <available file="wc" filepath="${env.PATH}" property="wc.executable"/>
        <!-- CYGWIN -->
        <available file="wc.exe" filepath="${env.PATH}" property="wc.exe.executable"/>
        <condition property="wc.can.run">
            <or>
                <isset property="wc.executable"/>
                <isset property="wc.exe.executable"/>
            </or>
        </condition>
        <!-- UNIX -->
        <available file="cat" filepath="${env.PATH}" property="cat.executable"/>
        <!-- CYGWIN -->
        <available file="cat.exe" filepath="${env.PATH}" property="cat.exe.executable"/>
        <condition property="cat.can.run">
            <or>
                <isset property="cat.executable"/>
                <isset property="cat.exe.executable"/>
            </or>
        </condition>
    </target>

    <target name="spawn" depends="init" if="test.can.run">
        <exec executable="sh" spawn="true">
            <arg value="spawn.sh"/>
            <arg value="${timeToWait}" />
            <arg value="${logFile}" />
        </exec>
    </target>

    <target name="no-redirect" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect1" depends="init" if="test.can.run">
        <exec executable="sh" output="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect2" depends="init" if="test.can.run">
        <exec executable="sh" output="redirect.out" error="redirect.err">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect3" depends="init" if="test.can.run">
        <exec executable="sh" logerror="true"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect4" depends="init" if="test.can.run">
        <exec executable="sh"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirect5" depends="init" if="wc.can.run">
        <exec executable="wc" inputstring="x y z"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirect6" depends="init" if="wc.can.run">
        <echo file="redirect.in">x y z
        <exec executable="wc" input="redirect.in"
              error="redirect.err" errorproperty="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirect7" depends="init" if="wc.can.run">
        <exec executable="wc" inputstring="x y z"
              error="redirect.err"
              output="redirect.out" outputproperty="redirect.out">
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirector1" description="fail"
            depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" />
            <redirector output="whocares" />
        </exec>
    </target>

    <target name="redirector2" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" />
        </exec>
    </target>

    <target name="redirector3" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" error="redirector.err" />
        </exec>
    </target>

    <target name="redirector4" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector output="redirector.out" logerror="true"
                        outputproperty="redirector.out" />
        </exec>
    </target>

    <target name="redirector5" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector error="redirector.err" errorproperty="redirector.err"
              output="redirector.out" outputproperty="redirector.out" />
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector6" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector7" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
                <errorfilterchain>
                    <replacestring from="err" to="ERROR!!!" />
                </errorfilterchain>
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector8" depends="init" if="wc.can.run">
        <echo file="redirector.in">x y z
        <exec executable="wc">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <inputmapper type="merge" to="redirector.in" />
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="-w"/>
        </exec>
    </target>

    <target name="redirector9" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <inputfilterchain>
                    <replacestring from="before" to="after" />
                </inputfilterchain>
                <inputmapper type="merge" to="redirector.in" />
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
        </exec>
    </target>

    <target name="redirector10" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
            <arg value="redirector.in"/>
        </exec>
    </target>

    <target name="redirector11" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err"
                        inputstring="blah before blah">
                <inputfilterchain>
                    <replacestring from="before" to="after" />
                </inputfilterchain>
                <outputmapper type="merge" to="redirector.out" />
                <errormapper type="merge" to="redirector.err" />
            </redirector>
        </exec>
    </target>

    <target name="redirector12" depends="init" if="cat.can.run">
        <echo file="redirector.in">blah before blah
        <exec executable="cat" output="redirector.out" error="redirector.err">
            <redirector outputproperty="redirector.out"
                        errorproperty="redirector.err">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="glob" from="nomatch" to="nomatchout" />
                <errormapper type="glob" from="nomatch" to="nomatcherr" />
            </redirector>
            <arg value="redirector.in"/>
        </exec>
    </target>

    <target name="redirector13" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector>
                <outputfilterchain>
                    <replacestring from="out" to="OUTPUT???" />
                </outputfilterchain>
                <errorfilterchain>
                    <replacestring from="err" to="ERROR!!!" />
                </errorfilterchain>
            </redirector>
            <arg value="parrot.sh" />
            <arg value="${ant.file}" />
        </exec>
    </target>

    <target name="redirector14" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector inputstring="blah before blah">
                <outputfilterchain>
                    <replacestring from="before" to="after" />
                </outputfilterchain>
                <outputmapper type="glob" from="nomatch" to="nomatchout" />
                <errormapper type="glob" from="nomatch" to="nomatcherr" />
            </redirector>
        </exec>
    </target>

    <target name="redirector15" depends="init" if="cat.can.run">
        <exec executable="cat">
            <redirector input="input/iso8859-1" output="redirector.out"
                        inputencoding="ISO8859_1" outputencoding="UTF8" />
        </exec>
    </target>

    <target name="redirector16" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector inputstring="exit"
                        output="redirector16.out" error="redirector16.err" />
        </exec>
        <condition property="16pass">
            <and>
                <available file="redirector16.out" type="file" />
                <available file="redirector16.err" type="file" />
            </and>
        </condition>
        <fail unless="16pass">Files were not created.
    </target>

    <target name="redirector17" depends="init" if="test.can.run">
        <exec executable="sh">
            <redirector inputstring="exit" createemptyfiles="false"
                        output="redirector17.out" error="redirector17.err" />
        </exec>
        <condition property="17fail">
            <or>
                <available file="redirector17.out" type="file" />
                <available file="redirector17.err" type="file" />
            </or>
        </condition>
        <fail if="17fail">Files were created.
    </target>

    <target name="redirector18" depends="init" if="test.can.run">
        <exec executable="sh">
            <arg value="parrot.sh"/>
            <arg value="${ant.file}" />
            <redirector alwayslog="true" logerror="true"
                        outputproperty="redirector.out" />
        </exec>
        <fail message="property redirector.out has unexpected content">
            <condition>
                <not>
                    <equals arg1="${ant.file} out" arg2="${redirector.out}" />
                </not>
            </condition>
        </fail>
    </target>

    <!-- test will succeed as the OS wont match-->
    <target name="testExecUnknownOS">
      <exec executable="nonexistent-program-we-expect"
        failonerror="true"
        os="ZX81">
      </exec>
    </target>

    <target name="testExecOSFamily">
      <exec executable="uptime"
        failonerror="true"
        osFamily="unix">
      </exec>
      <exec executable="cmd.exe"
        failonerror="true"
        osFamily="winnt">
        <arg value="/c" />
        <arg value="time /t" />
      </exec>
    </target>    
    
    <target name="testExecInconsistentSettings">
      <exec executable="nonexistent-program-we-expect"
        failonerror="true"
        osFamily="WIN9X"
        os="linux unix">
      </exec>
    </target>
    
    <target name="cleanup">
        <delete>
            <fileset file="${logFile}" />
            <fileset dir="${basedir}" includes="redirect*" />
            <fileset dir="${basedir}" includes="redirector*" />
        </delete>
    </target>
</project>

Other Ant examples (source code examples)

Here is a short list of links related to this Ant exec.xml source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.