Ant + JavaFront: start Ant tasks from the command line

Reading the blog Converting maven pom to ivy I asked myself, why it should not be possible to start Ant tasks from the command line.

Stefan on the other side was bored always to here that Ant relies on XML and he presented a way for writing Buildfiles in plain (annotated) Java using JavaFront, a library in Ants sandbox.

Ant provides a way to override the class to start by providing an implementation of AntStart and specifying that class by -main option. So I wrote a new start class which parses the command line parameters and executes that task. The first parameter is the taskname and following parameters are attributes with their values. (There is no support for nested elements yet.)


public class TaskExec implements AntMain {

    public void startAnt(String[] args, Properties additionalUserProperties, ClassLoader coreLoader) {
        Map<String, String> attributes = new Hashtable<String, String>();

        // Analyzing the command line arguments
        String taskname = args[0];
        for(int i=1; i<args.length; ) {
            String attrName  = args[i++];
            String attrValue = args[i++];
            attributes.put(attrName, attrValue);
        }

        // Initializing
        Project    project = initProject();
        TagBuilder builder = TagBuilder.forProject(project);

        // Initializing the Task
        Tag tag = builder.tag(taskname);
        for (String key : attributes.keySet()) {
            tag.withAttribute(key, attributes.get(key));
        }

        // Run the task
        tag.execute();
    }

    private Project initProject() {
        DefaultLogger logger = new DefaultLogger();
        logger.setOutputPrintStream(System.out);
        logger.setErrorPrintStream(System.err);
        logger.setMessageOutputLevel(Project.MSG_INFO);

        Project rv = new Project();
        rv.addBuildListener(logger);
        rv.init();

        return rv;
    }

}

So the Hello World is

ant -lib build\classes -main org.apache.ant.javafront.TaskExec echo message "Hello World"

Other examples are in the launcher script.

Explore posts in the same categories: Ant, Blogroll, en

Tags: , , ,

You can comment below, or link to this permanent URL from your own site.

One Comment on “Ant + JavaFront: start Ant tasks from the command line”


  1. [...] Jan’s Blog My personal blog about all and nothing … « Ant + JavaFront: start Ant tasks from the command line [...]


Comment: