Ant 简明教程

Ant - Create WAR Files

使用 Ant 创建 Web 归档 (WAR) 文件极其简单,且与创建 JAR 文件任务非常类似。毕竟,WAR 文件与 JAR 文件一样,都只是另一个 ZIP 文件。

Creating Web Archive (WAR) files with Ant is extremely simple, and very similar to the creating JAR files task. After all, WAR file, like JAR file is just another ZIP file.

WAR 任务是 JAR 任务的一个扩展,但它有一些不错的补充内容,可以操作进入 WEB-INF/classes 文件夹的内容,并生成 web.xml 文件。WAR 任务对于指定 WAR 文件的特定布局非常有用。

The WAR task is an extension to the JAR task, but it has some nice additions to manipulate what goes into the WEB-INF/classes folder, and generating the web.xml file. The WAR task is useful to specify a particular layout of the WAR file.

由于 WAR 任务是 JAR 任务的扩展,因此 JAR 任务的所有属性都适用于 WAR 任务。

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

Sr.No

Attributes & Description

1

webxml Path to the web.xml file.

2

lib A grouping to specify what goes into the WEB-INF\lib folder.

3

classes A grouping to specify what goes into the WEB-INF\classes folder.

4

metainf Specifies the instructions for generating the MANIFEST.MF file.

继续我们的 Hello World Fax Application 项目,让我们添加一个新目标以生成 jar 文件。但在添加目标之前,让我们考虑一下 war 任务。

Continuing our Hello World Fax Application project, let us add a new target to produce the jar files. But before that, let us consider the war task.

考虑以下示例 −

Consider 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>

如同之前的示例中提到的, web.dir 变量指源 web 文件夹,即包含 JSP、CSS、javascript文件等的文件夹。

As per the previous examples, the web.dir variable refers to the source web folder, i.e., the folder that contains the JSP, css, javascript files etc.

build.dir 变量指输出文件夹。此处可以找到 WAR 软件包的类。通常情况下,类将被捆绑到 WAR 文件的 WEB-INF/classes 文件夹中。

The build.dir variable refers to the output folder. This is where the classes for the WAR package can be found. Typically, the classes will be bundled into the WEB-INF/classes folder of the WAR file.

在此示例中,我们创建一个名为 fax.war 的 war 文件。WEB.XML 文件是从 web 源文件夹获取的。web 下的“WebContent”文件夹中的所有文件都会被复制到 WAR 文件中。

In this example, we are creating a war file called fax.war. The WEB.XML file is obtained from the web source folder. All files from the 'WebContent' folder under web are copied into the WAR file.

WEB-INF/lib 文件夹使用 thirdpartyjars 文件夹中的 jar 文件填充。然而,我们排除了 portlet.jar,因为它已经出现在应用程序服务器的 lib 文件夹中。最后,我们从 build 目录的 web 文件夹中复制所有类,并将它们放入 WEB-INF/classes 文件夹中。

The WEB-INF/lib folder is populated with the jar files from the thirdpartyjars folder. However, we are excluding the portlet.jar as this is already present in the application server’s lib folder. Finally, we are copying all classes from the build directory’s web folder and putting them into the WEB-INF/classes folder.

将 war 任务包装在 Ant 目标(通常是软件包)中并运行它。这将在指定位置创建 WAR 文件。

Wrap the war task inside an Ant target (usually package) and run it. This will create the WAR file in the specified location.

完全有可能嵌套类、lib、metainf 和 webinf 目录,以便他们居住在项目结构中任何地方的分散文件夹中。但是,最佳实践建议您的 Web 项目的 Web 内容结构应类似于 WAR 文件的结构。Fax 应用程序项目使用此基本原则概述了其结构。

It is entirely possible to nest the classes, lib, metainf and webinf directors, so that they live in scattered folders anywhere in the project structure. But, best practices suggest that your Web project should have the Web Content structure that is similar to the structure of the WAR file. The Fax Application project has its structure outlined using this basic principle.

要执行 war 任务,请将其包装在目标(最常见的是 build 或软件包目标)中,然后运行它们。

To execute the war task, wrap it inside a target, most commonly, the build or package target, and 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>

在此文件中运行 Ant 将为我们创建 fax.war 文件。

Running Ant on this file will create the fax.war file for us.

运行 Ant 文件后,会产生以下结果 −

The following outcome is the result of running the Ant file −

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

BUILD SUCCESSFUL
Total time: 12.3 seconds

fax.war 文件现在放置在输出文件夹中。war 文件的内容将如下所示 −

The fax.war file is now placed in the output folder. The contents of the war file will be as mentioned below −

fax.war:
   +---jsp This folder contains the jsp files
   +---css This folder contains the stylesheet files
   +---js This folder contains the javascript files

   +---images This folder contains the image files
   +---META-INF This folder contains the Manifest.Mf
   +---WEB-INF
      +---classes This folder contains the compiled classes
      +---lib Third party libraries and the utility jar files
      WEB.xml Configuration file that defines the WAR package