java uses Ant script to generate war package

Keywords: Java Apache xml Eclipse

 

Preparation before using ant script (this part can be ignored by ant in eclipse)
1. Download an ant installation package. For example: apache-ant-1.8.4-bin.zip. Unzip to disk E.

2. Configure environment variables. Add ANT_HOME: E:\apache-ant-1.8.4; add PATH: E:\apache-ant-1.8.4\bin.

3. Check whether ant configuration is complete. Run - > CMD input: ant -version. Check to see if ant's version number is printed.

The essence of packaging
The essence of generating jar package

1. Compile the java files under the src folder of the project and generate the corresponding class files.

2. Assemble all class files into jar package.

The essence of generating war package

1. Compile the java files under the src folder of the project and generate the corresponding class files.

2. Copy all files under WebContent except class files and jar packages to the corresponding directory of war package.

3. Copy all the class files generated by compilation to the war package WEB-INF/classes folder.

4. Copy all the jar packages in the project lib to the WEB-INF/lib of the war package. Finally, it is compressed into war package.

An ant script code for generating war package

<?xml version="1.0" encoding="UTF-8"?> 
  
<!-- Define a project. The default task is warFile.  --> 
<project name="awp" default="warFile" basedir="."> 
   
 <!-- Define attribute, type war The name of the package. --> 
 <property name="warFileName" value="awp.war"></property> 
   
 <!-- Defining paths, compiling java Documents used jar Bag. --> 
 <path id="project.lib"> 
  <fileset dir="${basedir}/lib"> 
   <include name="**/*.jar"/> 
  </fileset> 
 </path> 
   
  <!-- Define task, clear 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 put 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 assemble the class file into jar package. > 
 <target name="warFile" depends="build"> 
  <!--Package the jar under the Lib folder into 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 the 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> 
   <!-- take jar and class File copy to war Under the corresponding path of the package. --> 
    <lib dir="${basedir}/src/main/webapp/WEB-INF/lib" /> 
    <classes dir="${basedir}/src/main/webapp/WEB-INF/classes" /> 
  </war> 
 </target> 
   
</project>

Original text: https://www.jb51.net/article/80527.htm

Posted by cairesdesigns on Mon, 09 Dec 2019 01:54:12 -0800