Apache Groovy Ant Task integration library providing seamless integration between Groovy scripting capabilities and Apache Ant build automation framework
—
The Groovy Ant task enables execution of Groovy scripts and code snippets directly within Ant build files, providing full access to the Ant project context, properties, and build environment.
Executes Groovy scripts from files or inline text with comprehensive configuration options and build integration.
/**
* Executes a series of Groovy statements within an Ant build context.
* Statements can be read from a file using the src attribute or embedded inline.
*/
public class Groovy extends org.apache.tools.ant.taskdefs.Java {
/** Set the source file containing Groovy script to execute */
public void setSrc(File srcFile);
/** Add inline Groovy code to execute */
public void addText(String txt);
/** Execute script in a forked JVM process */
public void setFork(boolean fork);
/** Use GroovyShell for forked execution instead of Ant's Java task */
public void setUseGroovyShell(boolean useGroovyShell);
/** Include Ant runtime classpath when forking */
public void setIncludeAntRuntime(boolean includeAntRuntime);
/** Enable compiler stack trace reporting on errors */
public void setStacktrace(boolean stacktrace);
/** Set the classpath for script execution */
public void setClasspath(Path classpath);
/** Create a nested classpath element */
public Path createClasspath();
/** Set classpath using a reference */
public void setClasspathRef(Reference ref);
/** Get the current classpath */
public Path getClasspath();
/** Add a fileset of scripts to execute */
public void addFileset(FileSet set);
/** Set output file for script results */
public void setOutput(File output);
/** Append to output file instead of overwriting */
public void setAppend(boolean append);
/** Set configuration script for compiler customization */
public void setConfigscript(String configscript);
/** Enable invokedynamic support */
public void setIndy(boolean indy);
/** Set base class for script compilation */
public void setScriptBaseClass(String scriptBaseClass);
/** Generate parameter metadata for reflection */
public void setParameters(boolean parameters);
/** Check if parameter metadata generation is enabled */
public boolean getParameters();
/** Create command line argument for script */
public org.apache.tools.ant.types.Commandline.Argument createArg();
/** Set context classloader for script execution */
public void setContextClassLoader(boolean contextClassLoader);
/** Execute the Groovy script */
public void execute() throws BuildException;
}Scripts executed through the Groovy task have access to the following built-in variables:
// Available variables in Groovy scripts
public interface GroovyScriptContext {
/** Access to the AntBuilder for executing Ant tasks */
AntBuilder ant;
/** Reference to the current Ant project */
Project project;
/** Delegate for accessing project properties */
AntProjectPropertiesDelegate properties;
/** Reference to the current build target */
Target target;
/** Reference to the current task */
Task task;
/** Command line arguments passed to the script */
String[] args;
/** Maven POM object (when running under Maven) */
Object pom; // Available only in Maven context
}<groovy>
println "Current working directory: ${project.baseDir}"
println "Build timestamp: ${new Date()}"
// Access project properties
properties.each { key, value ->
if (key.startsWith('java.')) {
println "$key = $value"
}
}
</groovy><groovy src="scripts/build-helper.groovy">
<classpath>
<fileset dir="lib" includes="*.jar"/>
<pathelement location="build/classes"/>
</classpath>
</groovy><groovy fork="true"
includeAntRuntime="false"
useGroovyShell="true"
stacktrace="true">
<classpath>
<path refid="compile.classpath"/>
</classpath>
// Script with heavy processing that benefits from forking
def data = (1..1000000).collect { "Item $it" }
println "Processed ${data.size()} items"
</groovy><groovy>
// Use the ant builder to execute tasks dynamically
def sourceFiles = ['src/main/java', 'src/main/groovy']
sourceFiles.each { srcDir ->
if (new File(srcDir).exists()) {
ant.copy(todir: 'backup') {
fileset(dir: srcDir, includes: '**/*')
}
println "Backed up $srcDir"
}
}
// Set properties dynamically
project.setProperty('backup.completed', 'true')
</groovy><groovy output="build/reports/build-info.txt" append="false">
println "Build Information Report"
println "========================"
println "Build time: ${new Date()}"
println "Groovy version: ${GroovySystem.version}"
println "Java version: ${System.getProperty('java.version')}"
// Generate file listing
println "\nSource files:"
new File('src').eachFileRecurse { file ->
if (file.name.endsWith('.groovy') || file.name.endsWith('.java')) {
println " ${file.path}"
}
}
</groovy><groovy stacktrace="true">
try {
// Validate required properties
def requiredProps = ['version', 'build.dir', 'src.dir']
requiredProps.each { prop ->
if (!project.getProperty(prop)) {
throw new BuildException("Required property '$prop' is not set")
}
}
println "All required properties are set"
} catch (Exception e) {
// Log error and re-throw as BuildException
println "Validation failed: ${e.message}"
throw new BuildException("Property validation failed", e)
}
</groovy><groovy>
<fileset dir="src" includes="**/*.properties"/>
// Process properties files
def propsCount = 0
filesets[0].each { file ->
def props = new Properties()
file.withInputStream { props.load(it) }
println "Processing ${file.name}: ${props.size()} properties"
propsCount++
}
project.setProperty('properties.files.count', propsCount.toString())
</groovy>public interface CompilerConfiguration {
/** Set script base class for compilation */
public void setScriptBaseClass(String className);
/** Enable debug information in compiled classes */
public void setDebug(boolean debug);
/** Set target bytecode version */
public void setTargetBytecode(String version);
/** Enable parameter metadata generation */
public void setParameters(boolean parameters);
/** Configure optimization options */
public Map<String, Object> getOptimizationOptions();
}Scripts have access to a rich execution environment including:
Install with Tessl CLI
npx tessl i tessl/maven-org-codehaus-groovy--groovy-ant