Apache Groovy Ant Task integration library providing seamless integration between Groovy scripting capabilities and Apache Ant build automation framework
—
The AntBuilder provides a Groovy builder-style interface for executing Apache Ant tasks programmatically, enabling fluent, type-safe access to the complete Ant task ecosystem from Groovy code.
Provides builder-pattern access to Ant tasks with full project integration and stream management.
/**
* Allows Ant tasks to be used with a Groovy builder-style markup.
* Provides programmatic access to all Ant tasks through a fluent interface.
*/
public class AntBuilder extends groovy.util.BuilderSupport {
/** Create AntBuilder with default project */
public AntBuilder();
/** Create AntBuilder with specified Ant project */
public AntBuilder(Project project);
/** Create AntBuilder with project and owning target */
public AntBuilder(Project project, Target owningTarget);
/** Create AntBuilder from parent task context */
public AntBuilder(Task parentTask);
/** Get the underlying Ant project */
public Project getProject();
/** Get the Ant XML context used for task creation */
public AntXMLContext getAntXmlContext();
/** Check if stdin/stdout/stderr streams are saved during task execution */
public boolean isSaveStreams();
/** Control whether streams are saved and redirected during task execution */
public void setSaveStreams(boolean saveStreams);
/** Get the Ant project (alias for getProject) */
public Project getAntProject();
/** Create new Ant project instance with default configuration */
protected static Project createProject();
/** Build XML attributes from a Map for task configuration */
protected static org.xml.sax.Attributes buildAttributes(Map attributes);
}Access to Ant project functionality for build management and property handling.
/**
* Ant Project provides the core context for build execution,
* property management, and task coordination.
*/
public class Project {
/** Set a project property */
public void setProperty(String name, String value);
/** Get a project property value */
public String getProperty(String name);
/** Set a new property (fails if already exists) */
public void setNewProperty(String name, String value);
/** Get all project properties */
public java.util.Hashtable getProperties();
/** Log a message at INFO level */
public void log(String message);
/** Log a message at specified level */
public void log(String message, int msgLevel);
/** Get project base directory */
public File getBaseDir();
/** Set project base directory */
public void setBaseDir(File baseDir);
/** Get project name */
public String getName();
/** Set project name */
public void setName(String name);
/** Execute a target by name */
public void executeTarget(String targetName);
/** Get all defined targets */
public java.util.Hashtable getTargets();
/** Get default target name */
public String getDefaultTarget();
/** Add a build listener */
public void addBuildListener(org.apache.tools.ant.BuildListener listener);
}Control of input/output streams during task execution.
/**
* Stream management for capturing and redirecting task output.
*/
public interface StreamManagement {
/** Demultiplexed input stream for task input */
org.apache.tools.ant.DemuxInputStream getDemuxInputStream();
/** Demultiplexed output stream for task output */
org.apache.tools.ant.DemuxOutputStream getDemuxOutputStream();
/** Demultiplexed error stream for task errors */
org.apache.tools.ant.DemuxOutputStream getDemuxErrorStream();
}import groovy.util.AntBuilder
def ant = new AntBuilder()
// Execute simple tasks
ant.echo(message: "Hello from AntBuilder!")
ant.mkdir(dir: "build/output")
// Copy files
ant.copy(todir: "backup") {
fileset(dir: "src", includes: "**/*.groovy")
}def ant = new AntBuilder()
// Create directory structure
ant.mkdir(dir: "build/classes")
ant.mkdir(dir: "build/resources")
ant.mkdir(dir: "build/test-classes")
// Copy and filter resources
ant.copy(todir: "build/resources") {
fileset(dir: "src/main/resources") {
include(name: "**/*.properties")
include(name: "**/*.xml")
}
filterset {
filter(token: "VERSION", value: "1.0.0")
filter(token: "BUILD_DATE", value: new Date().toString())
}
}
// Delete temporary files
ant.delete {
fileset(dir: "build/temp", includes: "**/*")
}def ant = new AntBuilder()
// Create JAR file
ant.jar(destfile: "dist/myapp.jar") {
fileset(dir: "build/classes") {
exclude(name: "**/*Test.class")
}
manifest {
attribute(name: "Main-Class", value: "com.example.Main")
attribute(name: "Class-Path", value: "lib/groovy-all.jar")
}
}
// Create ZIP archive
ant.zip(destfile: "dist/source.zip") {
fileset(dir: "src", includes: "**/*.groovy,**/*.java")
fileset(dir: ".", includes: "build.xml,README.md")
}
// Extract archive
ant.unzip(src: "downloads/library.zip", dest: "lib/extracted")def ant = new AntBuilder()
// Compile Java sources
ant.javac(srcdir: "src/main/java",
destdir: "build/classes",
includeantruntime: false,
source: "1.8",
target: "1.8") {
classpath {
fileset(dir: "lib", includes: "*.jar")
}
}
// Compile Groovy sources
ant.groovyc(srcdir: "src/main/groovy",
destdir: "build/classes",
includeantruntime: false) {
classpath {
pathelement(location: "build/classes")
fileset(dir: "lib", includes: "*.jar")
}
}def ant = new AntBuilder()
// Run JUnit tests
ant.junit(printsummary: true, haltonfailure: false) {
classpath {
pathelement(location: "build/classes")
pathelement(location: "build/test-classes")
fileset(dir: "lib", includes: "*.jar")
}
formatter(type: "xml")
batchtest(todir: "build/test-reports") {
fileset(dir: "build/test-classes") {
include(name: "**/*Test.class")
}
}
}
// Generate test reports
ant.junitreport(todir: "build/test-reports") {
fileset(dir: "build/test-reports", includes: "TEST-*.xml")
report(todir: "build/test-reports/html")
}def ant = new AntBuilder()
// Set properties
ant.property(name: "app.version", value: "2.0.0")
ant.property(file: "build.properties")
// Load environment variables
ant.property(environment: "env")
// Access properties
def project = ant.getProject()
println "App version: ${project.getProperty('app.version')}"
println "Java home: ${project.getProperty('env.JAVA_HOME')}"
// Conditional execution based on properties
if (project.getProperty('debug.mode') == 'true') {
ant.echo(message: "Running in debug mode")
}def ant = new AntBuilder()
def project = ant.getProject()
// Define and execute targets programmatically
ant.target(name: "clean") {
delete(dir: "build")
}
ant.target(name: "compile", depends: "clean") {
mkdir(dir: "build/classes")
javac(srcdir: "src", destdir: "build/classes")
}
// Execute targets
project.executeTarget("compile")def ant = new AntBuilder()
// Define custom task
ant.taskdef(name: "customtask",
classname: "com.example.CustomTask") {
classpath {
fileset(dir: "lib", includes: "custom-tasks.jar")
}
}
// Use custom task
ant.customtask(param1: "value1", param2: "value2") {
nestedElement(attribute: "value")
}def ant = new AntBuilder()
// Capture task output
def output = new ByteArrayOutputStream()
def error = new ByteArrayOutputStream()
ant.setSaveStreams(false) // Disable automatic stream saving
// Redirect streams for specific task
System.setOut(new PrintStream(output))
System.setErr(new PrintStream(error))
try {
ant.exec(executable: "git", dir: ".", failonerror: false) {
arg(value: "status")
arg(value: "--porcelain")
}
} finally {
System.setOut(System.out)
System.setErr(System.err)
}
def gitStatus = output.toString()
println "Git status: $gitStatus"def ant = new AntBuilder()
def project = ant.getProject()
// Add custom build listener for logging
project.addBuildListener(new org.apache.tools.ant.DefaultLogger() {
void messageLogged(org.apache.tools.ant.BuildEvent event) {
if (event.priority <= org.apache.tools.ant.Project.MSG_WARN) {
println "ANT: ${event.message}"
}
}
})
try {
// Potentially failing task
ant.exec(executable: "nonexistent-command", failonerror: true)
} catch (org.apache.tools.ant.BuildException e) {
println "Build failed: ${e.message}"
// Continue with alternative approach
ant.echo(message: "Falling back to alternative method")
}// In a Gradle task
task customBuild {
doLast {
def ant = new AntBuilder()
// Use Gradle's project properties
ant.property(name: "gradle.version", value: project.version)
ant.property(name: "gradle.buildDir", value: project.buildDir.path)
// Execute Ant-based build steps
ant.echo(message: "Building ${project.name} version ${project.version}")
// Complex file operations not easily done in Gradle
ant.copy(todir: "${project.buildDir}/processed") {
fileset(dir: "src/templates") {
include(name: "**/*.template")
}
mapper(type: "glob", from: "*.template", to: "*.properties")
filterset {
filter(token: "PROJECT_NAME", value: project.name)
filter(token: "VERSION", value: project.version)
}
}
}
}def ant = new AntBuilder()
def project = ant.getProject()
// Configure project settings
project.setName("MyBuildProject")
project.setBaseDir(new File("."))
project.setDefaultTarget("build")
// Add custom properties
project.setProperty("build.timestamp", new Date().toString())
project.setProperty("build.user", System.getProperty("user.name"))def ant = new AntBuilder()
// Create custom task definitions
ant.presetdef(name: "myjavac") {
javac(includeantruntime: false,
source: "1.8",
target: "1.8",
debug: true) {
classpath(refid: "compile.classpath")
}
}
// Use preset task
ant.myjavac(srcdir: "src", destdir: "build/classes")Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-ant