Ant 简明教程

Ant - Building Projects

既然我们已学习了 Ant 中的数据类型,是时候将这份知识付诸实践了。我们将在本章中构建一个项目。本章的目标是构建一个 Ant 文件,编译 Java 类并将其置于 WEB-INF\classes 文件夹中。

Now that we have learnt about the data types in Ant, it is time to put that knowledge into practice. We will build a project in this chapter. The aim of this chapter is to build an Ant file that compiles the java classes and places them in the WEB-INF\classes folder.

考虑以下项目结构:

Consider the following project structure −

  1. The database scripts are stored in the db folder.

  2. The java source code is stored in the src folder.

  3. The images, js, META-INF, styles (css) are stored in the war folder.

  4. The Java Server Pages (JSPs) are stored in the jsp folder.

  5. The third party jar files are stored in the lib folder.

  6. The java class files are stored in the WEB-INF\classes folder.

该项目构成了本教程其余部分的 Hello World 传真应用程序。

This project forms the Hello World Fax Application for the rest of this tutorial.

C:\work\FaxWebApplication>tree
Folder PATH listing
Volume serial number is 00740061 EC1C:ADB1
C:.
+---db
+---src
. +---faxapp
. +---dao
. +---entity
. +---util
. +---web
+---war
   +---images
   +---js
   +---META-INF
   +---styles
   +---WEB-INF
      +---classes
      +---jsp
      +---lib

以下是该项目所需的 build.xml 。让我们逐件考虑。

Here is the build.xml required for this project. Let us consider it piece by piece.

<?xml version="1.0"?>
<project name="fax" basedir="." default="build">
   <property name="src.dir" value="src"/>
   <property name="web.dir" value="war"/>
   <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
   <property name="name" value="fax"/>

   <path id="master-classpath">
      <fileset dir="${web.dir}/WEB-INF/lib">
         <include name="*.jar"/>
      </fileset>
      <pathelement path="${build.dir}"/>
   </path>

   <target name="build" description="Compile source tree java files">
      <mkdir dir="${build.dir}"/>
      <javac destdir="${build.dir}" source="1.5" target="1.5">
         <src path="${src.dir}"/>
         <classpath refid="master-classpath"/>
      </javac>
   </target>

   <target name="clean" description="Clean output directories">
      <delete>
         <fileset dir="${build.dir}">
            <include name="**/*.class"/>
         </fileset>
      </delete>
   </target>
</project>

首先,让我们为源文件、Web 和构建文件夹声明一些属性。

First, let us declare some properties for the source, web, and build folders.

<property name="src.dir" value="src"/>
<property name="web.dir" value="war"/>
<property name="build.dir" value="${web.dir}/WEB-INF/classes"/>

在上述示例中,

In the above mentioned example −

  1. src.dir refers to the source folder of the project, where the java source files can be found.

  2. web.dir refers to the web source folder of the project, where you can find the JSPs,web.xml, css, javascript and other web related files

  3. build.dir refers to the output folder of the project compilation.

属性可以引用其他属性。如上述示例所示, build.dir 属性引用 web.dir 属性。

Properties can refer to other properties. As shown in the above example, the build.dir property makes a reference to the web.dir property.

在此示例中, src.dir 指的是项目的源文件夹。

In this example, the src.dir refers to the source folder of the project.

我们项目的默认目标是 compile 目标。但首先,让我们看一下 clean 目标。

The default target of our project is the compile target. But first, let us look at the clean target.

正如名称所示,clean 目标将删除构建文件夹中的文件。

The clean target, as the name suggests, deletes the files in the build folder.

<target name="clean" description="Clean output directories">
   <delete>
      <fileset dir="${build.dir}">
         <include name="**/*.class"/>
      </fileset>
   </delete>
</target>

master-classpath 保存类路径信息。在这种情况下,它包括构建文件夹中的类和 lib 文件夹中的 jar 文件。

The master-classpath holds the classpath information. In this case, it includes the classes in the build folder and the jar files in the lib folder.

<path id="master-classpath">
   <fileset dir="${web.dir}/WEB-INF/lib">
      <include name="*.jar"/>
   </fileset>
   <pathelement path="${build.dir}"/>
</path>

最后,构建目标用于构建文件。

Finally, the build targets to build the files.

首先,如果构建目录不存在,我们将创建它,然后执行 javac 命令(指定 jdk1.5 作为目标编译)。我们向 javac 任务提供源文件夹和类路径,并要求它将类文件放入构建文件夹中。

First of all, we create the build directory, if it does not exist, then, we execute the javac command (specifying jdk1.5 as our target compilation). We supply the source folder and the classpath to the javac task and ask it to drop the class files in the build folder.

<target name="build" description="Compile main source tree java files">
   <mkdir dir="${build.dir}"/>
   <javac destdir="${build.dir}" source="1.5" target="1.5"
      debug="true" deprecation="false" optimize="false" failonerror="true">

      <src path="${src.dir}"/>
      <classpath refid="master-classpath"/>
   </javac>
</target>

在此文件中执行 Ant 会编译 Java 源文件,并将类放入构建文件夹中。

Executing Ant on this file compiles the java source files and places the classes in the build folder.

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

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

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

BUILD SUCCESSFUL
Total time: 6.3 seconds

文件会编译并在 build.dir 文件夹中以 build 文件夹中放置。

The files are compiled and placed in the build.dir folder.