ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gruenewald, Terry E" <>
Subject RE: Need help on creating a task
Date Fri, 31 Jan 2003 03:47:42 GMT
Here's the ant XML and a task that implements a C++ compiler.   It shows you
how the different pieces in the XML file relate to the pieces that ant
expects in the Java code that ant will run.  The line:

<property environment="env"/>

Sets up "env" to be able to reference environment variables.  

<?xml version="1.0" encoding="UTF-8"?>
<project name="any_name" default="compile" basedir=".">
     Just some description   

    <property environment="env"/>
    <property name="acc.include" value="/opt/aCC/include"/>
    <property name="usr.include" value="/usr/include"/>
    <property name="java.class.dir" value="${env.DEVBASE}/classes"/>
    <property name="java.src.base"

    <path id="project.class.path">
        <pathelement path="${java.src.base}:${java.class.path}"/>

    <!--CUSTOM TASKS-->

    <taskdef name="my_compile"

    <target name="compile"
       <my_compile compiler="/opt/aCC/bin/aCC"
           <define value="NO_STD"/>
           <define value="NO_TUXEDO"/>
           <include value="${acc.include}"/>
           <include value="${usr.include}"/>
           <fileset dir="${env.SRC}" includes="no">
               <include name="**/*.cpp"/>
               <include name="**/*.C"/>

With the code, this should give you enough info to piece a lot of it
together.  Just remember that ant takes your attirbutes (i.e. compiler in
this example, and then expects there to be a method in your task called
Let me (well, the group that is) if you have any questions.
Here's the code:
package ant_tasks;

import java.util.*;
import java.lang.*;

public class MyCompile extends Task
    public void execute() throws BuildException
        int count = 0;
            for(int i = 0; i < m_filesets.size(); i++)
                log("Analyzing fileset", Project.MSG_VERBOSE);
                FileSet fs = (FileSet) m_filesets.elementAt(i);
                DirectoryScanner ds = fs.getDirectoryScanner(project);
                String[] files = ds.getIncludedFiles();
                File dirBase = fs.getDir(project);
                for(int j = 0; j < files.length; j++)
                    File srcFile = new File(dirBase, files[j]);
                    log("Determining if " + files[j] + " needs to be
compiled.", Project.MSG_VERBOSE);

                    // This is the guts, where I determine what is
                    // out of date and then compile it.  I leave it 
                    // out as an exercise to the user.
        catch(Exception e)
            throw new BuildException("MyCompile failed: ");
            long duration = (System.currentTimeMillis() - start) / 1000;
            System.out.println("Processed " + count + " source files in " +
duration + " seconds"); 

    public void addDefine(Element define)

    public void addInclude(Element include)

    public void setDebug(String sDebug)
            m_bDebug = true;
            m_bDebug = false;

    public void setCompiler(String sCompiler)
        m_sCompiler = sCompiler;

    public void addFileset(FileSet set)

    protected boolean           m_bDebug = false; 
    protected LinkedList        m_listDefines = new LinkedList();
    protected LinkedList        m_listIncludes = new LinkedList();
    protected File              m_dirDest;
    protected String            m_sCompiler;
    protected Vector            m_filesets = new Vector();

-----Original Message-----
From: Janet Abdul-Karim [] 
Sent: Thursday, January 30, 2003 9:17 PM
To: Ant Users List
Subject: Re: Need help on creating a task

This is what i am trying to do I have to create an htm formatted report that
includes the following header and title project name project description
current date user name ant version ant build filename/path java vm version
java vm name cpu info
What is have is this 
/** Imporing necessary classes**/
/** Creating Task for ant **/
public class Lab2Task extends Task{
 /** Method that executes the task **/
 public void execute() throws BuildException{
 }/** End of void execute() **/
 /** Method to setName for Task **/
 public void set 
}/** End of class **/
I have seen methods on how to get the things but don't really understand it.
I am looking for a more detailed example to help me out.  
 Erik Hatcher <> wrote:Your best reference
for Ant tasks is Ant's own source code. Check 
Jakarta's CVS for details or download the Ant source distribution.

If you shared your example details with us, I'm sure we could help get 
you on the right track with it.

Chapter 19 of Java Development with Ant has this review about its Ant 
internals, and I of course wholeheartedly recommend it too:

"I'm planning on extending ANT to do specialized testing, so I intend 
to write custom tasks. I've learned more about ANT's internals reading 
4 PAGES (467-470)..." [than one of the other Ant books, won't mention 
here, no need go there]


On Thursday, January 30, 2003, at 09:17 PM, Janet Abdul-Karim wrote:
> I am having problems with creating my own task using ant. I have
> looked at examples but they are not detailed enough to give me a 
> better idea of what I should do. I am suppose to create a java file 
> to extend class which I understand, but the other requirments like 
> getting the project name and description i don't understand. in my 
> .xml file i have the name of the project does the java class extract 
> the name from the .xml file.
> If someone can show me a better example of creating an ant task. I
> would greatly appreciate it.
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now

To unsubscribe, e-mail:
For additional commands, e-mail:

Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message