サンプル1 : Inputダイアログ
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="input.sample" name="SampleCode">
<target name="input.sample">
<input message="Please input your IP."
defaultvalue="127.0.0.1" addproperty="answer.result" />
<echo message="Your IP is ${answer.result}!" />
</target>
</project>
出力結果
input.sample:
[echo] Your IP is 127.0.0.1!
BUILD SUCCESSFUL
Total time: 5 seconds
サンプル2 : Yes/Noダイアログ
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="input.sample" name="SampleJavaCodes">
<target name="input.sample">
<input message="Are you hungry? (y/n)"
validargs="y,n" addproperty="answer.result" />
<condition property="answer.no">
<equals arg1="n" arg2="${answer.result}"/>
</condition>
<fail if="answer.no">Don't eat any more...</fail>
<echo message="Eat!!" />
</target>
</project>
出力結果
* "y" を選択時
input.sample:
[echo] Eat!!
BUILD SUCCESSFUL
Total time: 3 seconds
* "n" を選択時
input.sample:
BUILD FAILED
C:\workspace\SampleJavaCodes\build.xml:20: Don't eat any more...
Total time: 6 seconds
サンプル3 : ダイアログから受け取ったデータをJavaに渡し、実行する
com.ant.sample.AntSample.java
package com.ant.sample;
public class AntSample {
public static void main(String[] args) {
for (String arg : args) {
System.out.println("Hello world, " + arg);
}
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="run.java" name="SampleJava">
<target name="init" description="初期化">
<property name="class.path" value="./class" />
<delete dir="${class.path}"/>
<mkdir dir="${class.path}"/>
</target>
<target name="compile" depends="init" description="Javaコンパイル">
<javac srcdir="./src" destdir="./class" />
</target>
<target name="run.java" depends="compile" description="Java実行">
<echo message="execute Java" />
<input message="名前を入力してください:"
addproperty="input.data" />
<java classname="com.ant.sample.AntSample" classpath="${class.path}" >
<arg value="${input.data}"/>
</java>
</target>
</project>
出力結果
* "Mike" を入力しOKボタン押下
Buildfile: C:\eclipse\workspace\SampleJava\build.xml
init:
[delete] Deleting directory C:\eclipse\workspace\SampleJava\class
[mkdir] Created dir: C:\eclipse\workspace\SampleJava\class
compile:
[javac] C:\eclipse\workspace\SampleJava\build.xml:9: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 3 source files to C:\eclipse\workspace\SampleJava\class
run.java:
[echo] execute Java
[java] Hello world, Mike
BUILD SUCCESSFUL
Total time: 13 seconds