サンプル
[1] Javaコンパイル / Javaの実行(javac/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); } } }
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project basedir="." default="exec" name="SampleCodes"> <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="exec" depends="compile" description="Java実行"> <echo message="execute Java" /> <java classname="com.ant.sample.AntSample" classpath="${class.path}" > <arg value="Mike"/> <arg value="Tom"/> </java> </target> </project>
出力結果
Buildfile: C:\workspace\SampleCodes\build.xml init: [delete] Deleting directory C:\Develop\workspace\SampleCodes\class [mkdir] Created dir: C:\workspace\SampleCodes\class compile: [javac] C:\workspace\SampleCodes\build.xml:9: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds [javac] Compiling 17 source files to C:\workspace\SampleCodes\class exec: [echo] execute Java [java] Hello world, Mike [java] Hello world, Tom BUILD SUCCESSFUL Total time: 1 second
応用 : ダイアログから受け取ったデータをJavaに渡し、実行する
* 以下の関連記事を参照のこと。http://blogs.yahoo.co.jp/dk521123/35292895.html
[2] バッチ・シェルの実行(exec)
* Tomcat の起動と停止
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <project basedir="." default="start_tomcat" name="SampleCodes"> <property name="tomcat.bin" value="C:\tomcat8\bin" /> <!-- Tomcat起動 --> <target name="start_tomcat"> <exec executable="${tomcat.bin}\startup.bat" failonerror="yes" /> </target> <!-- Tomcat停止 --> <target name="stop_tomcat"> <exec executable="${tomcat.bin}\shutdown.bat" failonerror="yes" /> </target> </project>
出力結果
Tomcat の起動 'Buildfile: C:\workspace\SampleCodes\build.xml start_tomcat: [exec] Using CATALINA_BASE: "C:\tomcat8" [exec] Using CATALINA_HOME: "C:\tomcat8" [exec] Using CATALINA_TMPDIR: "C:\tomcat8\temp" [exec] Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0" [exec] Using CLASSPATH: "C:\tomcat8\bin\bootstrap.jar;C:\tomcat8\bin\tomcat-juli.jar" BUILD SUCCESSFUL Total time: 842 millisecondsTomcat の停止
Buildfile: C:\workspace\SampleCodes\build.xml stop_tomcat: [exec] Using CATALINA_BASE: "C:\tomcat8" [exec] Using CATALINA_HOME: "C:\tomcat8" [exec] Using CATALINA_TMPDIR: "C:\tomcat8\temp" [exec] Using JRE_HOME: "C:\Program Files\Java\jdk1.8.0" [exec] Using CLASSPATH: "C:\tomcat8\bin\bootstrap.jar;C:\tomcat8\bin\tomcat-juli.jar" BUILD SUCCESSFUL Total time: 1 second
補足 : 異常終了をしても処理を継続したい場合
=> erroeonfailを"false"に設定する ~~~~ <exec executable=... erroeonfail="false" /> ~~~~ * erroeonfailについては、以下の関連記事を参照のことhttp://blogs.yahoo.co.jp/dk521123/34767731.html
参考文献
[1] Javaコンパイル / Javaの実行
http://www.javadrive.jp/ant/java/index1.htmlhttp://www.atmarkit.co.jp/fjava/javatips/079jakarta005.html
* java実行時に引数を与える
http://www.wakhok.ac.jp/~tomoharu/web2004/text/index_a_c4.html