Apache Ant – Writing a Custom Ant Task
This tutorial shows you how to write a custom Apache Ant task. We will create a simple custom Ant task that will replace the content of the specified file.
This custom Ant task will require the user to provide a Regular expression pattern via match
attribute, a replacement content via replace
attribute, and file
attribute where the user will need to provide the path of the file that user wants to change its content.
Here is the result we will get in this tutorial:
<?xml version="1.0"?>
<project name="MyReplacerExample" default="main" basedir=".">
<taskdef
name="myreplacer"
classname="com.bytenota.build.MyReplacerTask"
classpath="my-replacer-task.jar"
/>
<target name="main">
<myreplacer
match="tutorial$"
replace="TUTORIAL."
file="content.txt"
/>
</target>
</project>
OK, here is step by step how to write a custom replacer ant task:
1. Create a new replacer task project in your IDE
– Let’s create MyReplacerTask
class as shown right below. In this class we need to write a public void execute()
method, with no arguments, this method implements the task itself. And add setter methods for supporting the attributes.
package com.bytenota.build;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class MyReplacerTask {
String match;
public void setMatch(String match) {
this.match = match;
}
String replace;
public void setReplace(String replace) {
this.replace = replace;
}
String file;
public void setFile(String file) {
this.file = file;
}
public void execute() throws Exception {
if ((match == null) || (replace == null) || (file == null)) {
throw new IllegalArgumentException("Not found: math, replace, and file attributes");
}
String originalContent = getFileContent();
String newContent = replaceContent(originalContent);
writeContentToFile(newContent);
}
protected String replaceContent(String originalContent) {
String newContent = originalContent.replaceAll(match, replace);
return newContent;
}
protected String getFileContent() {
File f = new File(this.file);
if (!f.exists()) {
throw new IllegalArgumentException("File not found: " + this.file);
}
StringBuilder contentBuilder = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(f));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
contentBuilder.append(sCurrentLine).append("\n");
}
} catch (IOException e){
e.printStackTrace();
}
return contentBuilder.toString();
}
protected void writeContentToFile(String content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter(this.file);
bw = new BufferedWriter(fw);
bw.write(content);
System.out.println("The content is written to the file.");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
– Set the MyReplacerTask
class to Main-Class
header in MANIFEST.MF
file.
Manifest-Version: 1.0
Main-Class: com.bytenota.build.MyReplacerTask
2. Build the project to jar file
Assuming that, you already built the project and the jar file is my-replacer-task.jar
.
3. Create a text file contains content for replacement
An Ant Task tutorial,
This file contains sample content for this tutorial
4. Use the custom replacer Ant task in the build project
<?xml version="1.0"?>
<project name="MyReplacerExample" default="main" basedir=".">
<taskdef
name="myreplacer"
classname="com.bytenota.build.MyReplacerTask"
classpath="my-replacer-task.jar"
/>
<target name="main">
<myreplacer
match="tutorial$"
replace="TUTORIAL."
file="content.txt"
/>
</target>
</project>
We define the custom replacer Ant task via <taskdef>
, then use it as shown right above.
5. Run the task and check out the result
– Run the task:
$ ant main
– Final result:
An Ant Task tutorial,
This file contains sample content for this TUTORIAL.