Skip to content

Instantly share code, notes, and snippets.

@TakayukiKando
Last active November 27, 2015 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TakayukiKando/340aca3925007c00ee73 to your computer and use it in GitHub Desktop.
Save TakayukiKando/340aca3925007c00ee73 to your computer and use it in GitHub Desktop.
ANT programming samples (Java sources would be placed into src/sample/ directory of your project. Ant files would be placed into test/ directory of your project. Before trying to run the other build files, you should try to run build_greeting.xml.)
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<target name="main">
<taskdef name="dir" classname="sample.Dir" classpath="${export}/sample.jar" />
<echo>Default</echo>
<dir/>
<echo>Specified base directory to src</echo>
<dir dir="${basedir}/../src" />
<echo>Specified "include" element</echo>
<dir dir="${basedir}/..">
<include name="**/*.class"/>
</dir>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<property name="sample" location="SAMPLE"/>
<target name="main">
<taskdef name="failure" classname="sample.Failure" classpath="${export}/sample.jar" />
<failure/>
<failure failonerror="true"/>
<failure/>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="src" location="../src"/>
<property name="output" location="../output"/>
<property name="export" location="../export"/>
<target name="main" depends="jar">
<taskdef name="greeting" classname="sample.Greeting" classpath="${export}/sample.jar" />
<!-- You may use classpath element instead of classpass attribute. -->
<greeting/>
</target>
<target name="jar" depends="compile">
<jar destfile="${export}/sample.jar" basedir="${output}" />
</target>
<target name="compile">
<mkdir dir="${output}"/>
<mkdir dir="${export}"/>
<javac srcdir="${src}" destdir="${output}">
<include name="**/*.java"/>
</javac>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<target name="main">
<taskdef name="log" classname="sample.Log" classpath="${export}/sample.jar" />
<log/>
<log>Log task samples:</log>
<log loglevel="4">((DEBUG))</log>
<log loglevel="3">...VERBOSE.</log>
<log loglevel="2">INFO.</log>
<log loglevel="1">WARN!</log>
<log loglevel="0">ERROR!!</log>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<target name="main">
<taskdef name="nest" classname="sample.Nest" classpath="${export}/sample.jar" />
<nest name="root">
<nested name="a1">
<nested name="b1">
</nested>
<nested name="b2">
</nested>
</nested>
<nested name="a2">
<nested name="b3">
<nested name="c1">
</nested>
</nested>
</nested>
</nest>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<property name="sample" location="SAMPLE"/>
<target name="main">
<taskdef name="prop" classname="sample.DumpProperties" classpath="${export}/sample.jar" />
<echo>Display single property value</echo>
<prop key="sample"/>
<echo>Dump all properties</echo>
<prop/>
</target>
</project>
<?xml version="1.0"?>
<project default="main">
<property name="export" location="../export"/>
<target name="main">
<taskdef name="run" classname="sample.Run" classpath="${export}/sample.jar" />
<run executable="C:\\Windows\\system32\\notepad.exe">
<arg arg="${basedir}/build_run.xml"/>
</run>
</target>
</project>
package sample;
import java.io.File;
import java.util.Arrays;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.taskdefs.MatchingTask;
public class Dir extends MatchingTask {
private File dir = new File(System.getProperty("user.dir"));//Default
//Invoked by ANT system
public void setDir(File dir){
this.dir = dir;
}
//Invoked by ANT system
public void execute() throws BuildException {
DirectoryScanner scanner = this.getDirectoryScanner(this.dir);
String[] files = scanner.getIncludedFiles();
System.out.println("At \""+this.dir+"\":");
Arrays.stream(files).map(s->new File(this.dir, s)).forEach(f->System.out.println("\t\""+this.dir.toPath().relativize(f.toPath())+"\" : "+f.length()+" bytes"));
}
}
package sample;
import java.util.Map;
import org.apache.tools.ant.Task;
public class DumpProperties extends Task {
private static void print(String key, Object value) {
System.err.println(String.format("\tkey: \"%s\", value: \"%s\"", key, value));
}
private String key = null;//Default
//Invoked by ANT system
public void setKey(String key){
this.key = key;
}
//Invoked by ANT system
public void execute(){
@SuppressWarnings("unchecked")
Map<String,Object> props = this.getProject().getProperties();
if(key != null){
print(key, props.get(key));
return;
}
props.entrySet().stream().forEach(e->print(e.getKey(), e.getValue()));
}
}
package sample;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
public class Failure extends Task {
private static final String message = "Task \"failure\" should fail anytime.";
private boolean failonerror = false;//Default
//Invoked by ANT system
public void setFailOnerror(boolean failonerror){
this.failonerror = failonerror;
}
//Invoked by ANT system
public void execute() throws BuildException {
this.log(message, Project.MSG_INFO);
if(failonerror){
this.log("Going to terminate whole build.", Project.MSG_ERR);
throw new BuildException(message);
}
this.log("Ignore failure.", Project.MSG_WARN);
}
}
package sample;
public class Greeting {
//Invoked by ANT system
public void execute(){
System.out.println("Hello.");
}
}
package sample;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
public class Log extends Task {
private int loglevel = Project.MSG_INFO;//Default
private String content = "";//default
//Invoked by ANT system
public void addText(String content){
this.content = this.content + content;
}
//Invoked by ANT system
public void setLoglevel(int loglevel){
this.loglevel = loglevel;
}
//Invoked by ANT system
public void execute() throws BuildException {
if(content.length() < 1){
this.log("Log levels:", Project.MSG_INFO);
this.log("\tMSG_ERR: "+Project.MSG_ERR, Project.MSG_INFO);
this.log("\tMSG_WARN: "+Project.MSG_WARN, Project.MSG_INFO);
this.log("\tMSG_INFO: "+Project.MSG_INFO, Project.MSG_INFO);
this.log("\tMSG_VERBOSE: "+Project.MSG_VERBOSE, Project.MSG_INFO);
this.log("\tMSG_DEBUG: "+Project.MSG_DEBUG, Project.MSG_INFO);
return;
}
this.log("Log task: ["+this.content+"]", this.loglevel);
}
}
package sample;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
public class Nest extends Task {
private static String expand(List<Nested> nesteds){
return nesteds.stream().map(n->n.getName()+"("+expand(n.getNested())+")").collect(Collectors.joining(", "));
}
public class Nested{
private String name = null;//Default
private final List<Nested> nesteds = new ArrayList<>();//Default
public Nested(){
//Nothing to do.
}
//Invoked by ANT system
public void setName(String name){
this.name = name;
}
public String getName(){
if(this.name == null){
throw new IllegalStateException("\"name\" attribute of \"nested\" element should not be null.");
}
return this.name;
}
//Invoked by ANT system
public Nested createNested(){
Nested nested = new Nested();
this.nesteds.add(nested);
return nested;
}
public List<Nested> getNested(){
return Collections.unmodifiableList(this.nesteds);
}
}
private String name = null;
private List<Nested> nesteds = new ArrayList<>();//Default
//Invoked by ANT system
public void setName(String name){
this.name = name;
}
public String getName(){
if(this.name == null){
throw new IllegalStateException("\"name\" attribute of \"nested\" element should not be null.");
}
return this.name;
}
//Invoked by ANT system
public Nested createNested(){
Nested nested = new Nested();
this.nesteds.add(nested);
return nested;
}
public List<Nested> getNested(){
return Collections.unmodifiableList(this.nesteds);
}
//Invoked by ANT system
public void execute() throws BuildException {
System.out.println(expand(this.getNested()));
}
}
package sample;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.types.Commandline;
public class Run extends Task{
public class Arg{
private String arg = null;
public void setArg(String arg){
this.arg = arg;
}
//Invoked by ANT system
public String getArg(){
return this.arg;
}
}
private String exe = null;
private List<Arg> args = new ArrayList<>();
//Invoked by ANT system
public void setExecutable(String exe){
this.exe = exe;
}
//Invoked by ANT system
public Arg createArg(){
Arg arg = new Arg();
this.args.add(arg);
return arg;
}
//Invoked by ANT system
public void execute() throws BuildException{
Commandline cmdln = new Commandline();
cmdln.setExecutable(this.exe);
args.stream().filter(a->(a.getArg()!=null)).map(Arg::getArg).forEach(a->cmdln.createArgument().setValue(a));
this.log("Going to execute: \""+cmdln.toString()+"\"", Project.MSG_INFO);
Execute runner = new Execute();
runner.setCommandline(cmdln.getCommandline());
try{
runner.execute();
}catch(IOException e){
this.log("Execution failed.", e, Project.MSG_ERR);
throw new BuildException("Execution failed.", e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment