Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ant compiles the project


May 25, 2021 Apache Ant


Table of contents


Ant compiles the project

Now that we've learned Ant's data types, it's time to apply what we've learned in practice. I n this section, we will build a project. The purpose of this section is to create an Ant build file that compiles java source files and stores them under the WEB-INF\classes folder.

Consider the structure of the next build project:

  • The data script is stored in the db folder.
  • The java source file is stored in the src folder.
  • Images (images), js (JavaScript script), style (css cascade style sheets) are stored in the war folder.
  • JSPs files are stored in the jsp folder.
  • Third-party jar files are stored in the lib folder.
  • The java class file is stored in the WEB-INF\classes folder.

Once you've finished the rest of this tutorial, you'll know that this project is a Hello World fax app.

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

The contents of the build file for the .xml are given below. Let's analyze it one statement after another.

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

First, let's declare some property information about the source, web, and build files.

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

In the example above:

  • src.dir represents the source file directory for this project, which is where the java files are stored.
  • Web.dir represents the project's web file directory, which is where JSPs files, web .xml files, css, javascript, and other web-related files are stored.
  • Build.dir represents the output file for the project.

Properties can also refer to other properties. In the example above, the build.dir property refers to the web.dir property.

In the example above, src.dir is where the project source files are stored.

The default goal of our project is the compilation target. But first let's look at the clean target.

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

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

Control class paths (master-classpaths) hold information about class paths. In this case, it contains all the class files in the build folder and jar folder.

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

Finally, build the target to build these files. F irst, we create a build directory, and if it doesn't exist, we execute the javac command (specifically jdk 1.5 as our target compilation environment). We provide source folders and class paths for javac tasks, and store class files in the build folder by performing javac tasks.

<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 is executed on this file, the java source file is compiled, and the compiled class file is stored where the folder is built.

After you run the Ant file, you can see the following output:

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

BUILD SUCCESSFUL
Total time: 6.3 seconds

When the file is compiled, it is stored in the build.dir folder.