トッカンソフトウェア

Apache Ant Webプロジェクトをビルド、デプロイ

前回はAntで通常のビルドを行いましたが、今回はWebプロジェクトのビルドとデプロイをやります。
build.xmlはEclipseから出力できるので、今回はそれを試してみます。

WebプロジェクトはEclipseでJsp、Servlet作成(web.xml)で作成したものを使用します。
事前に用意しておいて下さい。

build.xmlファイルの作成

Eclipseのプロジェクトを右クリックしてExport→Export...を選択します。


Ant Buildfilesを選択します。


プロジェクトを選択します。あとEclipseのコンパイラを使うのチェックボックスを外します(個人の好み)。


上記までの操作でbuild.xmlが作成されます。

build.xmlファイルの編集

そのままの状態でビルドまでは出来ますが、warファイルを作る部分を追記してみました(下記の黄色部分)。

build.xml
				
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="test">
	<property environment="env" />
	<property name="debuglevel" value="source,lines,vars" />
	<property name="target" value="1.8" />
	<property name="source" value="1.8" />
	<path id="Apache Tomcat v8.0 [Apache Tomcat v8.0].libraryclasspath">
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/annotations-api.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/catalina-ant.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/catalina-ha.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/catalina-storeconfig.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/catalina-tribes.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/catalina.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/ecj-4.4.2.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/el-api.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/jasper-el.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/jasper.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/javax.servlet.jsp.jstl-1.2.1.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/javax.servlet.jsp.jstl-api-1.2.1.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/jsp-api.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/postgresql-9.4-1204.jdbc4.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/servlet-api.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-api.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-coyote.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-dbcp.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-i18n-es.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-i18n-fr.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-i18n-ja.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-jdbc.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-jni.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-util-scan.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-util.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/tomcat-websocket.jar" />
		<pathelement location="../../../../apache-tomcat-8.0.24/lib/websocket-api.jar" />
	</path>
	<path id="Web App Libraries.libraryclasspath" />
	<path id="EAR Libraries.libraryclasspath" />
	<path id="test.classpath">
		<pathelement location="build/classes" />
		<path refid="Apache Tomcat v8.0 [Apache Tomcat v8.0].libraryclasspath" />
		<path refid="Web App Libraries.libraryclasspath" />
		<path refid="EAR Libraries.libraryclasspath" />
	</path>
	<target name="init">
		<mkdir dir="build/classes" />
		<copy includeemptydirs="false" todir="build/classes">
			<fileset dir="src">
				<exclude name="**/*.java" />
			</fileset>
		</copy>
	</target>
	<target name="clean">
		<delete dir="build/classes" />
	</target>
	<target depends="clean" name="cleanall" />
	<target depends="build-subprojects,build-project" name="build" />
	<target name="build-subprojects" />
	<target depends="init" name="build-project">
		<echo message="${ant.project.name}: ${ant.file}" />
		<javac debug="true" debuglevel="${debuglevel}" destdir="build/classes" includeantruntime="false" source="${source}" target="${target}">
			<src path="src" />
			<classpath refid="test.classpath" />
		</javac>
	</target>
	<target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects" />
	<target depends="build" name="deploy">
		<war destfile="../../../../apache-tomcat-8.0.24/webapps/test.war" webxml="WebContent/WEB-INF/web.xml">
			<fileset dir="WebContent" />
			<classes dir="build/classes" />
		</war>
	</target>
</project>


			
destfileで出力パスを指定します。クラスパスのjarファイル指定でTomcatのパスの相対パスがわかるので、それを参考に出力パスを記述します。
※webappsフォルダにwarファイルを出力するとTomcatが自動的に読み込んでデプロイしてくれます。

主な指定内容
指定箇所 指定内容
destfile warファイルの出力パス
webxml web.xmlが存在する相対パス
fileset jspファイルの存在する相対パス
classes クラスファイルの存在する相対パス


実行は以下のように実行します。
				
set ANT_HOME=C:\apache-ant-1.9.7
set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60
set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;%PATH%

cd C:\Users\aaa\workspace2\test

ant deploy


			
実際に実行すると以下のように出力されます。
				
C:\>set ANT_HOME=C:\apache-ant-1.9.7

C:\>set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_60

C:\>set PATH=%ANT_HOME%\bin;%JAVA_HOME%\bin;%PATH%

C:\>
C:\>cd C:\Users\aaa\workspace2\test

C:\Users\aaa\workspace2\test>
C:\Users\aaa\workspace2\test>ant deploy
Buildfile: C:\Users\aaa\workspace2\test\build.xml

build-subprojects:

init:
    [mkdir] Created dir: C:\Users\aaa\workspace2\test\build\classes

build-project:
     [echo] test: C:\Users\aaa\workspace2\test\build.xml
    [javac] Compiling 1 source file to C:\Users\aaa\workspace2\test\build\classes

build:

deploy:
      [war] Building war: C:\apache-tomcat-8.0.24\webapps\test.war

BUILD SUCCESSFUL
Total time: 2 seconds


			

ページのトップへ戻る