Browse Source

Converted the Ant1 Howto write a task to ant2

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@271697 13f79535-47bb-0310-9956-ffa450edef68
master
Peter Donald 23 years ago
parent
commit
6309813d01
1 changed files with 112 additions and 0 deletions
  1. +112
    -0
      proposal/myrmidon/src/xdocs/task.xml

+ 112
- 0
proposal/myrmidon/src/xdocs/task.xml View File

@@ -0,0 +1,112 @@
<document>

<properties>
<author email="peter@apache.org">Peter Donald</author>
<title>Writing a task</title>
</properties>

<body>

<section name="Writing a Task">

<p>In ant1 it was very easy to write your own task. In Ant2 we plan
to make it even easier. To write a basic task simply follow the following
formula.</p>

<ol>
<li>
Create a Java class that extends
<code>org.apache.myrmidon.api.AbstractTask</code>
</li>
<li>
For each attribute, write a setter method. The setter method
must be a public void method that takes a single argument. The name
of the method must begin with "set", followed by the attribute name, with
the first character of the name in uppercase, and the rest in lowercase.
The type of the attribute can be:
<ul>
<li>String</li>
<li>
Any primitive type - they are converted for you from their
String-representation in the buildfile
</li>
<li>
File - the string representation will be interpreted relative to
the project's basedir.
</li>
</ul>
</li>
<li>
For each nested element create a public void method that takes a single
argument. The name of the method must begin with "add", followed by the
attribute name, with the first character of the name in uppercase, and
the rest in lowercase. The type of the parameter is an object with a
no-arguement constructor. It is configured in exactly the same was a
task is configured (via setters and adders) and then added to the task.
</li>
<li>
Write a public void method named "execute" with no arguments that
throws a TaskException. This is the method called to do the
actual work of the task.
</li>
</ol>

<subsection name="A Basic Example">

<p>So a basic task that has one attribute named "message" and just prints
out this message is as simple as;</p>

<source>
package org.realityforge.tasks;

import org.apache.myrmidon.api.AbstractTask;
import org.apache.myrmidon.api.TaskException;

public class SystemOutPrinterTask
extends Task
{
private String m_message;

// The setter for the "message" attribute
public void setMessage( final String message )
{
m_message = message;
}

// The method executing the task
public void execute()
throws TaskException
{
System.out.println( m_message );
}
}
</source>

<p>To use this task you <em>could</em> create a library but instead we will
just use &lt;taskdef&gt; to define the task. An example usage would be;</p>

<source>
<![CDATA[

<?xml version="1.0"?>

<project version="2.0">

<target name="main">
<taskdef name="printer"
classname="org.realityforge.tasks.SystemOutPrinterTask"
classpath="build/classes"/>

<printer message="Hello World!"/>
</target>
</project>
]]>

</source>

</subsection>

</section>
</body>

</document>

Loading…
Cancel
Save