Coding With Fun
Home Docker Django Node.js Articles FAQ

Ant generates the WAR file


May 25, 2021 Apache Ant


Table of contents


Ant generates the WAR file

Creating a WAR file with Ant is extremely simple. T his is very similar to the task of creating a JAR file. After all, war files and JAR files are just two different ZIP files.

The WAR task is an extension of the JAR task, but it does some good things to control which files enter the WEB-INF/classes folder and generate .xml web-based files. WAR tasks are useful for specifying the layout of WAR files.

Since the WAR task is an extension of the JAR task, all the properties of the JAR task apply to the WAR task.

Property Describe
webxml The web .xml path to the file
Lib Specify what files can go to a group in the WEB-INF-lib folder
classes Specify what files can enter a group in the WEB-INF\classes folder
metainf Specify to build MANIFEST. The instruction for the MF file

Continuing with our Hello World fax app project, let's add a new target to generate the jar file. B ut before we do, we need to think about the war task. Take a look at the following example:

<war destfile = "fax.war" webxml = "${web.dir}/web.xml">

   <fileset dir = "${web.dir}/WebContent">
      <include name = "**/*.*"/>
   </fileset>

   <lib dir = "thirdpartyjars">
      <exclude name = "portlet.jar"/>
   </lib>

   <classes dir = "${build.dir}/web"/>

</war>

In the previous example, the web.dir variable points to the source web folder, which contains JSP, css, javascript files, and so on.

The build.dir variable points to the output folder, under which WAR's package can be found. Typically, the class is bound to the WEB-INF/classes folder under the WAR file.

In this example, we created a war file called fax.war. W eb. X ML files can be obtained from the web source file. All files from "WebContent" under the web are copied to the WAR file.

The WEB-INF/lib folder stores jar files from third-party jar folders. H owever, we excluded the portlet .jar because the jar file already exists in the lib folder of the application server. Finally, we copy all the classes from a web folder in the build directory and put all the copied classes under the WEB-INF/classes folder.

Encapsulate a war task into an Ant task and run it. This creates a WAR file at the specified location.

Classes, libraries, metainf, and webinf directories can all be nested so that they can all exist in scattered folders under the project structure. H owever, the best practice suggestion is that the web content schema of your web project should be similar to the WAR file. The architecture of the fax application project uses this basic principle.

To perform a war task, encapsulate it in a single target, most commonly, build a target or package target, and then run them.

<target name="build-war">

   <war destfile="fax.war" webxml="${web.dir}/web.xml">
      <fileset dir="${web.dir}/WebContent">
         <include name="**/*.*"/>
      </fileset>

      <lib dir="thirdpartyjars">
         <exclude name="portlet.jar"/>
      </lib>

      <classes dir="${build.dir}/web"/>
   </war>

</target>

Running Ant on this file creates a fax.war file for us.

The output below is the result of running the Ant file:

>C:\>ant build-war
>Buildfile: C:\build.xml

>BUILD SUCCESSFUL
>Total time: 12.3 seconds

The fax.war file is currently placed in the output folder. The contents of the war file are as follows:

>fax.war:
   >+---jsp :这个文件夹包含了 jsp 文件
   >+---css :这个文件夹包含了 stylesheet 文件
   >+---js :这个文件夹包含了  javascript 文件
   >+---images:这个文件夹包含了  image 文件
   >+---META-INF:这个文件夹包含了  Manifest.Mf
   >+---WEB-INF
           >+---classes :这个文件夹包含了编译好的类
          >+---lib :第三方库和使用程序 jar 文件
          >WEB.xml :定义 WAR 包的配置文件