Preparation before using ant script
1. Download ant installation package. For example: apache-ant-1.8.4-bin.zip. Unzip to disk E.
2. Configure environment variables. Added ANT_HOME: E:/apache-ant-1.8.4; PATH added: E:/apache-ant-1.8.4/bin.
3. Check whether the configuration of ant is completed. Run -> cmd input: ant -version. Check whether ant's version number is printed.
The essence of packaging
The essence of generating jar package
1. Compile the java file in the project src folder and generate the corresponding class file.
2. Collect all class files into jar packages.
The essence of generating war package
1. Compile the java file in the project src folder and generate the corresponding class file.
2. Copy all files except class files and jar packages under the project WebContent to the corresponding directory of the war package.
3. Copy all the compiled class files to the WEB-INF/classes folder of the war package.
4. Copy all jar packages in the project lib to the WEB-INF/lib of the war package. Finally compress it into a war package.
Ant script code that generates war package
<?xml version="1.0" encoding="UTF-8"?> <!-- Define a project, the default task is warFile. --> <project name="awp" default="warFile" basedir="."> <!-- Define the property and type it into the name of the war package. --> <property name="warFileName" value="awp.war"></property> <!-- Define the path, the jar package used when compiling java files. --> <path id="project.lib"> <fileset dir="${basedir}/lib"> <include name="**/*.jar"/> </fileset> </path> <!-- Define the task, clear the task: clear the original class file, and create a new build path. --> <target name="clean"> <delete dir="${basedir}/src/main/webapp/WEB-INF/classes" /> <mkdir dir="${basedir}/src/main/webapp/WEB-INF/classes" /> </target> <!-- Define the task, compile the java file in the src folder, and place the compiled class file in the created folder. --> <target name="build" depends="clean"> <javac srcdir="${basedir}/src/main/java" destdir="${basedir}/src/main/webapp/WEB-INF/classes" includeantruntime="false" source="1.6" target="1.6"> <classpath refid="project.lib"> </classpath> </javac> <!--Copy non-java files under src/main/java to /src/main/webapp/WEB-INF/classes--> <copy todir="${basedir}/src/main/webapp/WEB-INF/classes"> <fileset dir="${basedir}/src/main/java"> <include name="**/**.*" /> <exclude name="**/*.java"/> </fileset> </copy> </target> <!-- Define the default task and combine the class files into a jar package. --> <target name="warFile" depends="build"> <!--Package the jar in the lib folder to WEB-INF/lib--> <copy todir="${basedir}/src/main/webapp/WEB-INF/lib"> <fileset dir="${basedir}/lib"> </fileset> </copy> <!-- Create a new war package. --> <war destfile="${basedir}/${warFileName}" webxml="${basedir}/src/main/webapp/WEB-INF/web.xml"> <!-- Copy non-jar and non-class files to the corresponding path of the war package. --> <fileset dir="${basedir}/src/main/webapp"> <include name="**/**.*" /> <exclude name="**/*.jar"/> <exclude name="**/*.class"/> </fileset> <!-- Copy the jar and class files to the corresponding path of the war package. --> <lib dir="${basedir}/src/main/webapp/WEB-INF/lib" /> <classes dir="${basedir}/src/main/webapp/WEB-INF/classes" /> </war> </target> </project>The above is all about this article, I hope it will be helpful to everyone's learning.