org.oddjob.ant
Class AntJob

java.lang.Object
  extended by org.oddjob.framework.BaseComponent
      extended by org.oddjob.framework.BasePrimary
          extended by org.oddjob.framework.SimpleJob
              extended by org.oddjob.framework.SerializableJob
                  extended by org.oddjob.ant.AntJob
All Implemented Interfaces:
Serializable, Runnable, ArooaContextAware, ArooaSessionAware, Forceable, PropertyChangeNotifier, Iconic, LogEnabled, Resetable, Stateful, Stoppable

public class AntJob
extends SerializableJob
implements Stoppable

Description

Run a series of Ant tasks.

Oddjob creates it's own Ant project to use internally this project can be shared between different AntJob jobs using the 'project' attribute. This allows taskdefs and properties to be defined in one place and shared in many jobs.

Oddjob component properties can be referenced inside an Ant tasks using the ${id.property} notation. Ant will look up the Oddjob property before it looks up properties defined with the <property> tag. The oddjob derived properties of an Ant task aren't constant. Oddjob variables can change unlike Ant properties.

Not all tasks have been tested.

Note: Ant looks up properties beginning with 'ant.' - Therefore no component can have an id of 'ant' as the lookup will fail to retrieve the properties from that component (unless of course the 'ant' component implements all the properties that Ant requires!).

Example

Running an Ant Echo Task. The resultant output is captured in a buffer.
<oddjob id="this">
    <job>
        <ant id="an-ant">
            <output>
                <identify id="result">
                    <value>
                        <buffer/>
                    </value>
                </identify>
            </output>
            <tasks>
                <xml>
                    <tasks>
                        <echo message="${this.args[0]}"/>
                    </tasks>
                </xml>
            </tasks>
        </ant>
    </job>
</oddjob>

Example

Defining Ant properties in an Ant Job.
<ant>
    <tasks>
        <xml>
            <tasks>
                <property name="test.thing" value="Test"/>
                <echo message="${test.thing}"/>
            </tasks>
        </xml>
    </tasks>
</ant>

Example

Using Oddjob variables. Variables and properties defined in Oddjob are available in the Ant tasks.
<oddjob id="this">
    <job>
        <sequential>
            <jobs>
                <variables id="v">
                    <fruit>
                        <value value="Apples"/>
                    </fruit>
                </variables>
                <ant>
                    <tasks>
                        <xml>
                            <tasks>
                                <taskdef name="result" classname="org.oddjob.ant.AntJobTest$ResultTask"/>
                                <property name="our.fruit" value="${v.fruit}"/>
                                <property name="v.fruit" value="Pears"/>
                                <result key="one" result="${our.fruit}"/>
                                <result key="two" result="${v.fruit}"/>
                            </tasks>
                        </xml>
                    </tasks>
                </ant>
            </jobs>
        </sequential>
    </job>
</oddjob>
Note that the property defined in Ant does not override that defined in Oddjob (as per the rules of Ant). the result of both one and two is 'Apples'

Example

Sharing a project.
<oddjob>
    <job>
        <sequential>
            <jobs>
                <variables id="v">
                    <fruit>
                        <value value="Apples"/>
                    </fruit>
                </variables>
                <ant id="defs">
                    <tasks>
                        <xml>
                            <tasks>
                                <taskdef name="result" classname="org.oddjob.ant.AntJobTest$ResultTask"/>
                                <property name="our.fruit" value="${v.fruit}"/>
                                <property name="v.fruit" value="Pears"/>
                            </tasks>
                        </xml>
                    </tasks>
                </ant>
                <ant project="${defs.project}">
                    <tasks>
                        <xml>
                            <tasks>
                                <property name="our.fruit" value="Pears"/>
                                <result key="three" result="${our.fruit}"/>
                                <result key="four" result="${v.fruit}"/>
                            </tasks>
                        </xml>
                    </tasks>
                </ant>
            </jobs>
        </sequential>
    </job>
</oddjob>
The first Ant job declares a task and properties that the second Ant project can access.

Example

Working with files in Ant.
<oddjob>
    <job>
        <sequential name="Using Ant to Manipulate Files">
            <jobs>
                <properties>
                    <values>
                        <value key="our.test.file.name" value="test.txt"/>
                    </values>
                </properties>
                <ant baseDir="${work.dir}">
                    <tasks>
                        <xml>
                            <tasks>
                                <patternset id="file.test">
                                    <include name="${our.test.file.name}"/>
                                </patternset>
                                <touch file="${our.test.file.name}"/>
                                <copy todir="..">
                                    <fileset dir=".">
                                        <patternset refid="file.test"/>
                                    </fileset>
                                </copy>
                                <delete file="../${our.test.file.name}"/>
                                <delete file="${our.test.file.name}"/>
                                <echo message="Done."/>
                            </tasks>
                        </xml>
                    </tasks>
                </ant>
            </jobs>
        </sequential>
    </job>
</oddjob>
See Also:
Serialized Form
Author:
rob

Field Summary
 
Fields inherited from class org.oddjob.framework.SimpleJob
stateHandler, stop
 
Fields inherited from class org.oddjob.framework.BaseComponent
iconHelper
 
Constructor Summary
AntJob()
           
 
Method Summary
protected  int execute()
          Execute the tasks.
 File getBaseDir()
           
 ClassLoader getClassLoader()
           
 String getMessageLevel()
           
 OutputStream getOutput()
          Return the ant tasks output.
 org.apache.tools.ant.Project getProject()
          Get the project used for the tasks.
 String getTasks()
           
 String getVersion()
           
 boolean isException()
           
protected  void onReset()
          Allow sub classes to do something on reset.
protected  void onStop()
          Allow sub classes to do something on stop.
 void setBaseDir(File baseDir)
           
 void setClassLoader(ClassLoader classLoader)
           
 void setException(boolean exception)
           
 void setMessageLevel(String messageLevel)
           
 void setOutput(OutputStream out)
           
 void setProject(org.apache.tools.ant.Project project)
          Set the project to use for the tasks.
 void setTasks(String xml)
           
 
Methods inherited from class org.oddjob.framework.SimpleJob
fireDestroyedState, force, getStateChanger, hardReset, onDestroy, run, sleep, softReset, stateHandler, stop
 
Methods inherited from class org.oddjob.framework.BasePrimary
configure, getName, logger, logger, loggerName, save, setName, toString
 
Methods inherited from class org.oddjob.framework.BaseComponent
addIconListener, addPropertyChangeListener, addStateListener, configure, destroy, firePropertyChange, getArooaSession, iconForId, initialise, lastStateEvent, onConfigured, onInitialised, removeIconListener, removePropertyChangeListener, removeStateListener, save, setArooaContext, setArooaSession
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.oddjob.Stoppable
stop
 
Methods inherited from interface org.oddjob.Stateful
addStateListener, lastStateEvent, removeStateListener
 

Constructor Detail

AntJob

public AntJob()
Method Detail

setOutput

public void setOutput(OutputStream out)

getOutput

public OutputStream getOutput()
Return the ant tasks output.

Returns:
The output as a string.

getProject

public org.apache.tools.ant.Project getProject()
Get the project used for the tasks.

Returns:
The project.

setProject

public void setProject(org.apache.tools.ant.Project project)
Set the project to use for the tasks. It is assumed the project is already initialised.

Parameters:
project - The project.

setMessageLevel

public void setMessageLevel(String messageLevel)

getMessageLevel

public String getMessageLevel()

execute

protected int execute()
               throws Exception
Execute the tasks.

Specified by:
execute in class SimpleJob
Returns:
0 if the job is complete, anything else otherwise.
Throws:
Exception - If the unexpected occurs.

onStop

protected void onStop()
Description copied from class: SimpleJob
Allow sub classes to do something on stop.

Overrides:
onStop in class SimpleJob

onReset

protected void onReset()
Description copied from class: SimpleJob
Allow sub classes to do something on reset.

Overrides:
onReset in class SimpleJob

isException

public boolean isException()

setException

public void setException(boolean exception)

getBaseDir

public File getBaseDir()

setBaseDir

public void setBaseDir(File baseDir)

getTasks

public String getTasks()

setTasks

public void setTasks(String xml)

getClassLoader

public ClassLoader getClassLoader()

setClassLoader

public void setClassLoader(ClassLoader classLoader)

getVersion

public String getVersion()

Property: version

Description: The ant version.

Returns:
The ant version or nothing if the job hasn't run yet.