2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2020

10/03/2003: Creating an Ant Task with Eclipse

Creating an Ant Task with Eclipse

  1. Add ant.jar to your eclipse project.
  2. Create package to hold task classes. I recommend xxx.yyy.ant.tasks
  3. Create task class. Here is a simple one:
    package com.affy.ant.tasks;
    import org.apache.tools.ant.*;
    public class HelloWorld extends Task {
        public void execute() throws BuildException {
            System.out.println("Hello World!");
        }
    }
    
  4. Select Window, Preferences, Ant, Classpath
  5. Set ANT_HOME
  6. Add the folder where your class files are stored (xxx/build/classes, for example)
  7. Select Task, Add Task
  8. Assign a task name (ie, HelloWorld) to use in build.xml
  9. Select location (the folder xxx/build/classes for example) </ol>

    That's how easy it is to start writing your own Ant tasks. If you're going to invoke build.xml from within Eclipse, then you only need the following lines:

        <target name="HelloWorld">
            <HelloWorld/>
        </target>
    

    Otherwise, you need to tell Ant where to find the class file. For example,

        <path id="cp">
            <pathelement path="xxx/build/classes"/>
        </path>
        <target name="HelloWorld">
            <taskdef name="HelloWorld" classname="org.wwre.ant.tasks.HelloWorld" classpathref="cp"/>
            <HelloWorld/>
        </target>
    

05/21/2003: A Simple Extensible Command-Line Interpreter in Java

I’ve found command-line interpreters are useful tools. Especially interpreter’s that can read commands from both files and keyboard. I’ve used such interpreter’s for printer manipulation, database and object creation, and other tasks. I can hear someone saying, ‘Use Ant’, but creating Ant tasks can be a daunting proposition at first. So the class presented here is simple, simple, simple so that beginning java developers can use it.

This simple command-line interpreter is extensible and easy-to-use as shown in test code below. The source code for the command line class is about 500 lines and is available at http://www.codebits.com/java/CommandLine.java.


CommandLineTest.java


package com.affy.utils.cmdline;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StreamTokenizer;
import java.util.Vector;

/**
 * This class shows how the CommandLine class
 * can be used. It initialized a CommandLine object
 * and defines a FOO command. Notice that this class
 * (CommandLineTest) acts as both test harness and
 * command executor. When run, you'll be presented with
 * a command-line prompt. Type 'foo bar baz' and you'll
 * see the output from the doIt() method. Type 'aaa aaa aaa'
 * and you'll see an error message. Command-line parameters
 * with spaces need to be quoted.
 *
 * @author medined
 * Created on May 20, 2003
 */
public class CommandLineTest
implements CommandLine.ICommand {

	/**
	 * Shows how to use the CommandLine class.
	 * @param args The command-line parameters
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args)
	throws FileNotFoundException {
		// This technique of reading from either
		// a script file or
		// from the console was 'borrowed' from BeanShell.
		Reader inputSrc = null;
		if ( args.length > 0 ) {
			inputSrc = new BufferedReader(
				new FileReader(args[0])
			);
		} else {
			inputSrc = new InputStreamReader(System.in);
		}

		// initialize the command line object.
		CommandLine jr = new CommandLine();
		jr.setCommandLinePrompt("Command> ");
		jr.setCommandLineVersion("Command Line v.01");
		jr.assignClassToCommnd("foo",
			"com.affy.utils.cmdline.CommandLineTest");
		jr.init();
		if ( args.length > 0 ) {
			jr.setIsInteractive(false);
		}

		// parse and execute commands.
		jr.parseStream(new StreamTokenizer(inputSrc));

		System.out.println("\nDone.");
	}

	/**
	 * This method is invoked when the FOO command is
	 * used.
	 */
	public boolean doIt(Vector v) {
		System.out.println(
			"Inside CommandLineTest.doIt(); v="
			+ v
		);
		return true;
	}
}