Kohei Nozaki's blog 

Running AntUnit in Maven

Posted on Tuesday Jan 27, 2015 at 11:11PM in Technology

These days I’m writing a build.xml and some custom tasks which manipulates OS X’s tmutil (Time Machine utility) and vmrun (VMware utility). testing of custom tasks are bit annoying to me, so I’ve had some googling around and I found AntUnit which is unit test framework for custom Ant tasks. it enables us to test custom tasks in more realistic way than plain JUnit testing. it actually runs the custom tasks in test case which wrote as build.xml. it looks little similar to JUnit test case as follows:

<project xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant">
    <taskdef name="mytask" classname="org.nailedtothex.antunitpractice.MyTask">
            <pathelement location="${project.build.outputDirectory}"/>

    <target name="setUp">

    <target name="test1">
        <mytask property="prop"/>
            <equals arg1="Hello from MyTask" arg2="${prop}"/>

    <target name="tearDown">

Targets that beginning their name with test will be automatically executed. we can do assertion with some elements (e.g. au:assertTrue) that provided by AntUnit. target named setUp will be executed before executing every test cases. tearDown also the same as JUnit.

The function of a custom task which I wrote is very simple. it simply set the constant string Hello from MyTask to specified property (prop).

package org.nailedtothex.antunitpractice;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class MyTask extends Task {

    private String property;

    public String getProperty() {
        return property;

    public void setProperty(String property) {
        this.property = property;

    public void execute() throws BuildException {
        getProject().setNewProperty(property, "Hello from MyTask");

So, how to run the test? I used Maven’s maven-antrun-plugin. it doesn’t need annoying preparation such as standalone installation of Ant and AntUnit because it collects annoying dependencies automatically. we only need to run mvn clean test as follows:

kyle-no-MacBook:antunitpractice kyle$ mvn clean test
Picked up _JAVA_OPTIONS: -Dfile.encoding=UTF-8
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building antunitpractice 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ antunitpractice ---
[INFO] Deleting /Users/kyle/src/antunitpractice/target
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ antunitpractice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) @ antunitpractice ---
[INFO] Compiling 1 source file to /Users/kyle/src/antunitpractice/target/classes
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ antunitpractice ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 2 resources
[INFO] --- maven-compiler-plugin:2.5.1:testCompile (default-testCompile) @ antunitpractice ---
[INFO] Nothing to compile - all classes are up to date
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ antunitpractice ---
[INFO] --- maven-antrun-plugin:1.8:run (default-cli) @ antunitpractice ---
[INFO] Executing tasks

[au:antunit] Build File: /Users/kyle/src/antunitpractice/target/test-classes/myTaskTest.xml
[au:antunit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 0.018 sec
[au:antunit] Target: test1 took 0.016 sec
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.413s
[INFO] Finished at: Wed Jan 28 09:38:19 JST 2015
[INFO] Final Memory: 16M/212M
[INFO] ------------------------------------------------------------------------

I haven’t apply this method to actual projects yet but looks useful for some occasions.

More information can be found via the official site of AntUnit. the entire project using in this entry can be found in my GitHub repository.

No one has commented yet.

Leave a Comment

HTML Syntax: NOT allowed