Mamat Zone

HOME | SITEMAP

Build War menggunakan Ant

Posted on 2011-11-25 00:52:18 by mamat, 0 Comments


Build War dengan menggunakan Ant sangat dinamis, contoh pada kasus yang saya punya adalah dimana enviroment development kita akan secara otomatis melakukan build war dan langsung mendeploy web server(*kasus ini web server menggunakan tomcat).

Karena kita melakukan development menggunakan Eclipse, awalnya sangat susah jika melakukan Build War pada ant default. Akhirnya dengan mencotek configurasi dari Eclipse builder dan menambahkan lib Eclipse ke server, bisa jalan build War secara smooth. Alhamdulillah... hehe.

Agar build war dan deployment bisa dilakukan secara otomatis, command pada ant ditanam pada conjob diserver. Berikut ini command ant dengan script yang sudah saya buat:

Clean Class - ini akan membersihkan compailer yang lama (*.class)

				ant clean
			


Create War - membuat War
				ant create
			


Deploy War - untuk melakukan deploy ke web server(*tomcat), untuk web server configurasi autodeploy jika ada file baru yang masuk.
				ant deploy
			


Berikut ini adalah detil script ant yang sudah dibuat:

				
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project basedir="." default="build" name="WebAppTest">
	<property environment="env" />
	<property name="ECLIPSE_HOME" value="/opt/eclipselib" />
	<property name="debuglevel" value="source,lines,vars" />
	<property name="target" value="1.5" />
	<property name="source" value="1.5" />
	<property name="warfile" value="WebAppTest" />

	<path id="J2EE 1.4 Library Container.libraryclasspath">
		<pathelement location="/opt/eclipselib/1.4/activation.jar" />
		<pathelement location="/opt/eclipselib/1.4/javax.servlet.jar" />
		<pathelement
			location="/opt/eclipselib/1.4/javax.servlet.jsp.jar" />
		<pathelement location="/opt/eclipselib/1.4/jboss-j2ee.jar" />
		<pathelement location="/opt/eclipselib/1.4/jboss-jaxrpc.jar" />
		<pathelement location="/opt/eclipselib/1.4/jboss-jsr77.jar" />
		<pathelement location="/opt/eclipselib/1.4/jboss-saaj.jar" />
		<pathelement location="/opt/eclipselib/1.4/mail.jar" />
		<pathelement location="/opt/eclipselib/1.4/namespace.jar" />
		<pathelement location="/opt/eclipselib/1.4/xml-apis.jar" />
	</path>
	<path id="WebAppTest.classpath">
		<pathelement location="WebRoot/WEB-INF/classes" />
		<path refid="J2EE 1.4 Library Container.libraryclasspath" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-collections-2.1.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-fileupload.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-lang-2.0.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-logging-1.0.2.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-net-1.4.1.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/cos.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/itext-1.1.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/jakarta-oro-2.0.8.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/jasperreports-3.0.0.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/jodd.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/log4j-1.2.8.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/mail.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/oracle.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/poi-2.5.1.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/smart.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-fileupload-1.2.1-javadoc.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-fileupload-1.2.1-sources.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-fileupload-1.2.1.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-io-1.3.2.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-lang-2.3.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-logging-1.0.4.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-pool-1.3.jar" />
		<pathelement
			location="WebRoot/WEB-INF/lib/commons-validator-1.3.0.jar" />
		<pathelement location="WebRoot/WEB-INF/lib/activation.jar" />
	</path>
	<target name="init">
		<mkdir dir="WebRoot/WEB-INF/classes" />
		<copy includeemptydirs="false"
			todir="WebRoot/WEB-INF/classes">
			<fileset dir="src" excludes="**/*.launch, **/*.java" />
		</copy>
	</target>
	<target name="clean">
		<delete dir="WebRoot/WEB-INF/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="WebRoot/WEB-INF/classes" source="${source}"
			target="${target}">
			<src path="src" />
			<classpath refid="WebAppTest.classpath" />
		</javac>
	</target>
	<target
		description="Build all projects which reference this project. Useful to propagate changes."
		name="build-refprojects" />
	<target
		description="copy Eclipse compiler jars to ant lib directory"
		name="init-eclipse-compiler">
		<copy todir="${ant.library.dir}">
			<fileset dir="${ECLIPSE_HOME}/plugins"
				includes="org.eclipse.jdt.core_*.jar" />
		</copy>
		<unzip dest="${ant.library.dir}">
			<patternset includes="jdtCompilerAdapter.jar" />
			<fileset dir="${ECLIPSE_HOME}/plugins"
				includes="org.eclipse.jdt.core_*.jar" />
		</unzip>
	</target>
	<target description="compile project with Eclipse compiler"
		name="build-eclipse-compiler">
		<property name="build.compiler"
			value="org.eclipse.jdt.core.JDTCompilerAdapter" />
		<antcall target="build" />
	</target>

	<target name="create">
		<war destfile="${warfile}.war" webxml="WebRoot/WEB-INF/web.xml"
			update="true">
			<classes dir="build/classes" />
			<fileset dir="WebRoot">
				<exclude name="WEB-INF/web.xml" />
			</fileset>
		</war>
	</target>

	<target name="copy">
		<copy todir="/opt/apache-tomcat-6.0.29/webapps" overwrite="true">
			<fileset dir=".">
				<include name="*.war" />
			</fileset>
		</copy>
	</target>
	
	<target name="deploy">
		<antcall target="create"/>
		<antcall target="copy"/>
	</target>

</project>

			

Other Articles