站内搜索: 请输入搜索关键词
当前页面: 图书首页 > Servlets and JavaServer Pages: The J2EE Technology Web Tier

Servlets and JavaServer Pages: The J2EE Technology Web Tier

[ directory ]Web Applications Summary

Ant

The Jakarta Ant project is popular with Java developers and for good reason. Ant is a cross-platform build utility that is easy to use and configure. Ant uses an XML configuration file typically called build.xml. If you are unfamiliar with build utilities, do not worry. This book does not rely on such knowledge. Ant is treated purely as a complimentary tool. If you have used Ant before, you should appreciate this, or if you have not used Ant, hopefully you will be glad we introduced the tool to you. At any time you can opt to completely ignore Ant and simply compile .java files by hand and reload Tomcat using the appropriate scripts. At no point will this book tell you to use Ant to do this. Instead, an example will dictate that Tomcat, the Web Application, or both need to be reloaded, or that code needs to be compiled. In these cases it is implied you can use Ant if you so desire.

What Does Ant Do?

Ant performs "tasks", any number of them, and in any order, possibly dependent on previously accomplished tasks. There are many default tasks that Ant defines; Ant also allows for Java developers to code custom tasks. In use, Ant takes a simple build file, which defines tasks to be done, and does them. For this book we will make a build file that turns off Tomcat, compiles code, then turns Tomcat back on. The tasks are all simple but after trying several of the code examples in this book, you might be glad Ant consolidates the redundant steps.

Installing Ant

Install Ant by downloading the latest distribution from http://jakarta.apache.org/ant. This book uses Ant 1.5, but any subsequent release should work fine. Binary distributions of Ant are available in either ZIP or tarball form. Download the format you are most familiar with and save the compressed file in a convenient location. Unpack the distribution and installation of Ant complete. Because the files are Java binaries, there is no need to run an installer or compile a platform-specific distribution.

Using Ant

Ant is designed to be simple to use. The /bin directory of the Ant distribution contains an executable file named ant that runs Ant. You can execute Ant at any time by running $ANT_HOME/bin/ant; replace $ANT_HOME with the appropriate directory of the Ant distribution. By default, Ant looks to the current directory and tries to use a file named build.xml as its build file. Listing 1-5, which is included in the example WAR, is a build file for use with this book.

Listing 1-5. Ant Build File for Use with This Book
<?xml version="1.0" ?>
<project name="jspbook" default="build" basedir=".">
  <target name="build">
    <echo>Starting Build [JSP Book - http://www.jspbook.com]</echo>
    <!-- Turn Tomcat Off -->
    <antcall target="tomcatOff"/>
    <!-- Compile Everything -->
    <antcall target="compile"/>
    <!-- Turn Tomcat On -->
    <antcall target="tomcatOn"/>
    <echo>Build Finished [JSP Book - http://www.jspbook.com]</echo>
  </target>

  <target name="tomcatOff">
    <echo>Turning Off Tomcat [http://www.jspbook.com]</echo>
    <exec executable="bash" os="Windows">
      <arg value="../../bin/shutdown.bat"/>
    </exec>
    <exec executable="bash" os="Linux">
      <arg value="../../bin/shutdown.sh"/>
    </exec>
  </target>

  <target name="tomcatOn">
    <echo>Starting Tomcat [http://www.jspbook.com]</echo
    <exec executable="bash" os="Windows">
      <arg value="../../bin/startup.bat"/>
    </exec>
    <exec executable="bash" os="Linux">
      <arg value="../../bin/startup.sh"/>
    </exec>
  </target>

  <target name="compile">
    <echo>Compiling Book's Examples [http://www.jspbook.com]</echo>
    <javac
      srcdir="WEB-INF/classes"
      extdirs="WEB-INF/lib:../../common/lib"
      classpath="../../common/lib/servlet.jar"
      deprecation="yes"
      verbose="no">
      <include name="com/jspbook/**"/>
    </javac>
  </target>
</project>

Save the preceding code as build.xml in the base directory of the jspbook Web Application. You do not need to care about the entries in the build file; however, they should be intuitive. If you are interested, you can find further documentation for the Ant tasks online at http://jakarta.apache.org/ant/manual/index.html.

Use of the build file is simple. From shell, or command prompt in Windows, switch to the directory of the jspbook Web Application. For instance, if Tomcat was installed in /usr/jakarta-tomcat/[10], then the desired directory would be /usr/jakarta-tomcat/webapps/jspbook. From there, execute Ant. Assuming Ant is in your classpath, you can simply type ant, or a full path can be used such as /usr/jakarta-ant-1.5/bin/ant. Ant automatically looks for build.xml and will execute the specified tasks. Execution should look similar to Figure 1-17.

[10] These are UNIX file systems. In Windows, the starting '/' is replaced by the drive, such as C:\, and subsequent '/' are replaced with '\'. For instance, C:\jakarta-tomcat\webapps\jspbook.

Figure 1-17. Ant Execution of the Book's Build File

graphics/01fig17.jpg


Note the build file turns off Tomcat, compiles all the book's code, and turns Tomcat back on. Should an example request any of these tasks, simply run Ant and the job is finished.

[ directory ]Web Applications Summary