Chapter 8. Developing for the Web

Developing for the Web is bread and butter for Ant developers. There is a wide spectrum of tasks at your disposal: Chapter 4 introduced packaging and deploying applicationsincluding Web applicationswith the move, copy, ftp, telnet, sshexec, and mail tasks, but Ant offers more. This chapter covers the tasks specifically designed for packaging Web applications, such as war, cab, ear, and jspc, and for deploying them, such as get, serverdeploy, and scp. I'll cover the custom Ant tasks targeted to specific servers such as deploy, reload, and undeploy. And there's more to come: Chapter 9 covers the many optional Enterprise JavaBeans (EJB) tasks Ant supports.

Creating WAR Archives

The war task is an extension of the jar task, and it compresses Web applications into .war files, with special handling for files that should end up in the WEB-INF/lib, WEB-INF/classes or WEB-INF directories on the server. For example, say you have this directory layout after you build your project:

war
|____output
|        login.class
|        logout.class
|        
|____source
     |    login.xml
     |    
     |____html
              welcome.xml

The build file in Example 8-1 will create the .war file you need to deploy this application, placing the .class files in the WEB-INF/classes directory, renaming login.xml web.xml and placing it in WEB-INF, and so on.

Example 8-1. Creating a war file (ch08/war/build.xml)

<?xml version="1.0" encoding="UTF-8" ?> <project default="main" basedir="."> <property name="bin" value="output" /> <property ...

Get Ant: The Definitive Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.