やること
[1] ビルド
[2] warファイル作成
[3] デプロイ
[4] Tomcat起動
[5] 単体試験
[6] Tomcat停止
サンプル
http://blogs.yahoo.co.jp/dk521123/34052743.html
で使用したJarは、ANT_HOME/lib配下に置いておく。
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="all" name="SampleService">
<property name="warName" value="SampleService" />
<property name="class.path" value="./class" />
<property name="lib.path" value="./lib" />
<property name="java.path" value="./src" />
<property name="junit.path" value="./test" />
<property name="test_report.path" value="./test_report" />
<!-- 環境変数から読み込み -->
<property environment="env" />
<echo message="TOMCAT_HOME=${env.TOMCAT_HOME}"/>
<echo message="ANT_HOME=${env.ANT_HOME}"/>
<property name="tomcat.deploy" value="${env.TOMCAT_HOME}/webapps" />
<!-- 初期化 -->
<target name="init" description="初期化">
<mkdir dir="${class.path}"/>
<mkdir dir="${test_report.path}"/>
</target>
<!-- ビルド -->
<target name="compile" depends="init" description="Javaコンパイル">
<javac srcdir="${java.path}" destdir="${class.path}" includeantruntime="false" />
<javac srcdir="${junit.path}" destdir="${class.path}"
classpath="${env.ANT_HOME}/lib/junit.jar;${env.ANT_HOME}/lib/httpunit.jar;${env.ANT_HOME}/lib/js-1.6R5.jar;${env.ANT_HOME}/lib/jtidy-4aug2000r7-dev.jar"
includeantruntime="false" />
</target>
<!-- WARファイル作成 -->
<target name="war" depends="compile">
<war destfile="${warName}.war"
webxml="WebContent/WEB-INF/web.xml">
<fileset dir="WebContent">
<include name="**/*" />
</fileset>
<classes dir="build/classes" />
</war>
</target>
<!-- デプロイ -->
<target name="deploy" depends="war">
<move file="${warName}.war" tofile="${tomcat.deploy}/${warName}.war"/>
</target>
<!-- Tomcat起動 -->
<target name="start_tomcat" depends="deploy">
<ant inheritAll="false" antfile="build_tomcat.xml" target="start_tomcat" />
</target>
<!-- Tomcat停止 -->
<target name="stop_tomcat" depends="start_tomcat">
<ant inheritAll="false" antfile="build_tomcat.xml" target="stop_tomcat" />
</target>
<!-- httpunit -->
<target name="httpunit_test">
<echo message="ANT_HOME=${env.ANT_HOME}"/>
<junit printsummary="true">
<classpath>
<pathelement path="${class.path}" />
<pathelement path="${env.ANT_HOME}/lib/junit.jar" />
<pathelement path="${env.ANT_HOME}/lib/httpunit.jar" />
<pathelement path="${env.ANT_HOME}/lib/js-1.6R5.jar" />
<pathelement path="${env.ANT_HOME}/lib/jtidy-4aug2000r7-dev.jar" />
</classpath>
<formatter type="plain" />
<batchtest fork="yes" todir="${test_report.path}">
<fileset dir="${junit.path}">
<include name="**/*Test.java" />
</fileset>
</batchtest>
</junit>
</target>
<target name="clean">
<delete dir="${class.path}"/>
<delete dir="${test_report.path}"/>
<delete dir="${tomcat.deploy}/${warName}"/>
<delete file="${tomcat.deploy}/${warName}.war" />
</target>
<!-- All -->
<target name="all">
<antcall target="clean" />
<antcall target="compile" />
<antcall target="war" />
<antcall target="deploy" />
<parallel>
<antcall target="start_tomcat" />
<sequential>
<waitfor maxwait="2" maxwaitunit="minute" checkevery="500">
<http url="http://localhost:8080/${warName}" />
</waitfor>
<antcall target="httpunit_test" />
<antcall target="stop_tomcat" />
</sequential>
</parallel>
</target>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="start_tomcat" name="Tomcat">
<!-- 環境変数から読み込み -->
<property environment="env" />
<echo message="TOMCAT_HOME=${env.TOMCAT_HOME}"/>
<property name="tomcat.bin" value="${env.TOMCAT_HOME}/bin" />
<!-- Tomcat起動 -->
<target name="start_tomcat">
<exec executable="${tomcat.bin}\startup.bat" />
</target>
<!-- Tomcat停止 -->
<target name="stop_tomcat">
<exec executable="${tomcat.bin}\shutdown.bat" />
</target>
</project>