How do I create a jar file using Ant?
Author: Deron Eriksson
Description: This Ant tutorial describes how to use the jar task to build a jar file.
Tutorial created using:
Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1
(Continued from page 2) This tutorial consisted of a total of three AntSW files. The team-cakes-utilities build.xml file points to the build-main.properties and build-main.xml files within another project, ant-utilities, which is a general Ant build project that contains targets that multiple projects can use. The team-cakes-utilities build.xml file is shown below: team-cakes-utilities build.xml<?xml version="1.0" encoding="UTF-8"?> <project name="team-cakes-utilities" default="" basedir="."> <property name="project-name" value="${ant.project.name}" /> <property file="../ant-utilities/build-main.properties" /> <import file="../ant-utilities/build-main.xml" /> </project> The ant-utilities and team-cakes-utilities projects can be seen here. Notice that in Eclipse's Ant view, the team-cakes-utilities project has available all the targets within the ant-utilities build-main.xml file. The build-jar target in the Ant view is highlighted. ![]() Here is the ant-utilities build-main.properties file: ant-utilities build-main.properties#build-main.properties file project-name=ant-utilities builder=TeamCakes ftp-server=-- ftp-userid=-- ftp-password=-- tomcat-manager-url=http://--/manager tomcat-manager-username=-- tomcat-manager-password=-- server-home-directory=--/-- email-to=-- email-from=-- tomcat-home=/apache-tomcat-5.5.20 Here is the ant-utilities build-main.xml file that contains the targets we mentioned above and many other useful targets. ant-utilities build-main.xml<?xml version="1.0" encoding="UTF-8"?> <project name="ant-utilities" default="war" basedir="."> <property file="build-main.properties" /> <property name="war-file-name" value="${project-name}.war" /> <property name="source-directory" value="src" /> <property name="classes-directory" value="bin" /> <property name="web-directory" value="web" /> <property name="web-xml-file" value="web/WEB-INF/web.xml" /> <tstamp prefix="build-info"> <format property="current-date" pattern="d-MMMM-yyyy" locale="en" /> <format property="current-time" pattern="hh:mm:ss a z" locale="en" /> <format property="year-month-day" pattern="yyyy-MM-dd" locale="en" /> </tstamp> <property name="build-directory" value="build" /> <property name="ftp-remotedir" value="uploaded-wars" /> <path id="project-classpath"> <fileset dir="web/WEB-INF/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/bin" includes="*.jar" /> <fileset dir="${tomcat-home}/common/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/server/lib" includes="*.jar" /> </path> <path id="jar-project-classpath"> <fileset dir="lib" includes="*.jar" /> <fileset dir="${tomcat-home}/bin" includes="*.jar" /> <fileset dir="${tomcat-home}/common/lib" includes="*.jar" /> <fileset dir="${tomcat-home}/server/lib" includes="*.jar" /> </path> <taskdef name="start" classname="org.apache.catalina.ant.StartTask" /> <taskdef name="stop" classname="org.apache.catalina.ant.StopTask" /> <taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask" /> <taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask" /> <target name="project-name-test"> <echo>hello, the project is: ${project-name}</echo> </target> <target name="stop" description="stop application in tomcat"> <stop url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" /> </target> <target name="start" description="start application in tomcat"> <start url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" /> </target> <target name="undeploy" description="undeploy from tomcat"> <undeploy failonerror="no" url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" /> </target> <target name="deploy" description="deploy to tomcat"> <echo>deploying from client</echo> <deploy url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" war="file:/projects/workspace/${project-name}/${build-directory}/${war-file-name}" /> </target> <target name="deploy-from-server"> <echo>deploying server war file</echo> <sleep seconds="5" /> <deploy url="${tomcat-manager-url}" username="${tomcat-manager-username}" password="${tomcat-manager-password}" path="/${project-name}" localwar="file:/${server-home-directory}/uploaded-wars/${war-file-name}" /> <echo>done deploying</echo> </target> <target name="war"> <mkdir dir="${build-directory}" /> <delete file="${build-directory}/${war-file-name}" /> <war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}"> <classes dir="${classes-directory}" /> <fileset dir="${web-directory}"> <!-- Need to exclude it since webxml is an attribute of the war tag above --> <exclude name="WEB-INF/web.xml" /> </fileset> <manifest> <attribute name="Built-By" value="${builder}" /> <attribute name="Built-On" value="${build-info.current-date}" /> <attribute name="Built-At" value="${build-info.current-time}" /> </manifest> </war> </target> <target name="ftp" depends="" description="upload war file to server"> <ftp server="${ftp-server}" remotedir="${ftp-remotedir}" userid="${ftp-userid}" password="${ftp-password}" action="mkdir" verbose="yes"> </ftp> <ftp server="${ftp-server}" remotedir="${ftp-remotedir}" userid="${ftp-userid}" password="${ftp-password}" action="send" verbose="yes" depends="yes"> <fileset file="${build-directory}/${war-file-name}" /> </ftp> </target> <target name="pscp" depends="" description="securely upload war file to server"> <echo message="uploading war file to server using pscp" /> <exec executable="pscp.exe"> <arg value="-v" /> <arg value="-pw" /> <arg value="${ftp-password}" /> <arg value="${build-directory}/${war-file-name}" /> <arg value="${ftp-userid}@${ftp-server}:${ftp-remotedir}/${war-file-name}" /> </exec> <echo message="done uploading war file to server using pscp" /> </target> <target name="mail-build-and-deploy"> <mail from="${email-from}" tolist="${email-to}" subject="${war-file-name} was uploaded to the server and deployed" message="The ${war-file-name} file was uploaded to ${ftp-server} in ${ftp-remotedir} and deployed to the /${project-name} context" /> </target> <target name="clean"> <delete dir="bin" /> <mkdir dir="bin" /> </target> <target name="copy-non-java-files"> <copy todir="bin" includeemptydirs="false"> <fileset dir="src" excludes="**/*.java" /> </copy> </target> <target name="compile" depends="clean,copy-non-java-files"> <javac srcdir="src" destdir="bin" classpathref="project-classpath" /> </target> <target name="compile-jar-classes" depends="clean,copy-non-java-files"> <javac srcdir="src" destdir="bin" classpathref="jar-project-classpath" /> </target> <target name="clean-jar"> <delete dir="build" /> <mkdir dir="build" /> </target> <target name="jar"> <jar basedir="bin" destfile="build/${project-name}.jar"> <manifest> <attribute name="Built-By" value="${builder}" /> <attribute name="Built-On" value="${build-info.current-date}" /> <attribute name="Built-At" value="${build-info.current-time}" /> </manifest> </jar> </target> <target name="build-jar"> <antcall target="compile-jar-classes" /> <antcall target="clean-jar" /> <antcall target="jar" /> </target> <target name="build-and-deploy-from-server" depends="war,ftp,undeploy,deploy-from-server,mail-build-and-deploy" /> <target name="build-and-deploy-from-server (secure)" depends="war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" /> <target name="kitchen-sink" depends="compile,war,pscp,undeploy,deploy-from-server,mail-build-and-deploy" /> </project> Related Tutorials: |