Creating a war file using Ant script in Java
This post will discuss a simple and efficient ant build file for creating a war file in web applications in Java.
This post assumes the web module follows the standard structure:
- All
Javaclasses should be present in the src directory. - The
WebContent(aka document root) should contain JSP pages, CSS, and static web resources, such as images. - The
WebContentcontains a subdirectory named WEB-INF, which contains web.xml (web application deployment descriptor), the Tag library descriptor files, classes directory, tags directory, and the lib directory (containing required libraries).
The complete build.xml file is shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?xml version="1.0" encoding="UTF-8"?> <project name="sample" default="war" basedir="."> <delete dir="./build/*"/> <property name="project-name" value="${ant.project.name}" /> <property name="builder" value="sample" /> <property environment="env"/> <property name="java.home" value="${env.JAVA_HOME}" /> <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="WebContent" /> <property name="deployment.dir" value="." /> <property name="web-xml-file" value="WebContent/WEB-INF/web.xml" /> <property name="lib-home" value="WebContent/WEB-INF/lib" /> <property name="build-directory" value="build" /> <path id="classpath"> <fileset dir="${lib-home}" includes="*.jar" /> <pathelement location="bin" /> </path> <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" /> </tstamp> <target name="check-runtime"> <fail unless="lib-home"> lib directory not found </fail> </target> <target name="compile" depends="check-runtime" > <echo message="Compiling Java source code" /> <mkdir dir="bin" /> <javac srcdir="${basedir}/src" includeantruntime="true" executable="{java.home}/bin/javac" destdir="bin" debug="yes" nowarn="true"> <include name="**/*.java"/> <classpath refid="classpath" /> </javac> </target> <target name="war" depends="check-runtime,compile"> <echo message="************************ compiling *************" /> <echo message="Ant running on Java version ${ant.java.version}"/> <javac includes="bin" includeantruntime="true" executable="${java.home}/bin/javac" debug="yes" nowarn="true" debuglevel="lines,vars,source"> <src path="${basedir}" /> <classpath> <fileset dir="lib"> <include name="**/*.zip" /> <include name="**/*.jar" /> </fileset> </classpath> </javac> <mkdir dir="${build-directory}" /> <delete file="${build-directory}/*" /> <copy todir="${web-directory}/WEB-INF/lib"> <fileset dir="${basedir}/${lib-home}" includes="*"/> </copy> <war warfile="${build-directory}/${war-file-name}" webxml="${web-xml-file}"> <classes dir="${classes-directory}" /> <fileset dir="${web-directory}"></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> <copy todir="${deployment.dir}"> <fileset dir="${basedir}/${build-directory}" includes="${war-file-name}"/> </copy> </target> </project> |
That’s all about creating a war file using Ant script in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)