UNPKG

epubjs

Version:

Render ePub documents in the browser, across many devices

93 lines (87 loc) 12.4 kB
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Building WAR Files with Ant</title><link rel="stylesheet" href="core.css" type="text/css"/><meta name="generator" content="DocBook XSL Stylesheets V1.74.0"/></head><body><div class="sect1" title="Building WAR Files with Ant"><div class="titlepage"><div><div><h1 class="title"><a id="learnjava3-CHP-15-SECT-5"/>Building WAR Files with Ant</h1></div></div></div><p><a id="idx10900" class="indexterm"/>Thus far in this book, we have not become too preoccupied with special tools to help you construct Java applications. Partly, this is because it’s outside the scope of this text, and partly it reflects a small bias of the authors against getting too entangled with particular development environments. There is, however, one universal tool that should be in the arsenal of every Java developer: the Jakarta Project’s Ant. Ant is a project builder for Java, a pure Java application that fills the role that <em class="filename">make</em> does for C applications. Ant has many advantages over <em class="filename">make</em> when building Java code, not the least of which is that it comes with a wealth of special “targets” (declarative commands) to perform common Java-related operations such as building WAR files. Ant is fast, portable, and easy to install and use. Make it your friend.</p><p>We won’t cover the usage of Ant in detail here. You can learn more and download it from its <a class="ulink" href="http://jakarta.apache.org/ant/">home page</a>. To get you started, we give you a sample build file here. The Ant build file supplied with the examples for this chapter will compile the source and build the completed WAR file for you. You can find it with the example source.</p><div class="sect2" title="A Development-Oriented Directory Layout"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-15-SECT-5.1"/>A Development-Oriented Directory Layout</h2></div></div></div><p><a id="idx10844" class="indexterm"/> <a id="idx10878" class="indexterm"/>At the beginning of this chapter, we described the layout of a WAR, including the standard files and directories that must appear inside the archive. While this file organization is necessary for deployment inside the archive, it may not be the best way to organize your project during development. Maintaining <span class="emphasis"><em>web.xml</em></span> and libraries inside a directory named <span class="emphasis"><em>WEB-INF</em></span> under all of your content may be convenient for running the <span class="emphasis"><em>jar</em></span> command, but it doesn’t line up well with how those areas are created or maintained from a development perspective. Fortunately, with a simple Ant build file, we can create our WAR from an arbitrary project layout.</p><p>Let’s choose a directory structure that is a little more oriented toward project development. For example:</p><a id="I_15_tt960"/><pre class="programlisting"> <code class="n">myapplication</code> <code class="o">|</code> <code class="o">|--</code> <code class="n">src</code> <code class="o">|--</code> <code class="n">lib</code> <code class="o">|--</code> <code class="n">docs</code> <code class="o">|--</code> <code class="n">web</code><code class="o">.</code><code class="na">xml</code></pre><p>We place our source code tree under <a id="I_indexterm15_id783616" class="indexterm"/><span class="emphasis"><em>src</em></span>, our required library JAR files under <a id="I_indexterm15_id783625" class="indexterm"/><span class="emphasis"><em>lib</em></span>, and our content under <a id="I_indexterm15_id783634" class="indexterm"/><span class="emphasis"><em>docs</em></span>. We leave <span class="emphasis"><em>web.xml</em></span> at the top where it’s easy to tweak parameters, etc.</p><p>Here is a simple Ant <a id="I_indexterm15_id783649" class="indexterm"/><span class="emphasis"><em>build.xml</em></span> file for constructing a WAR from the new directory structure:</p><a id="I_15_tt961"/><pre class="programlisting"> <code class="o">&lt;</code><code class="n">project</code> <code class="n">name</code><code class="o">=</code><code class="s">"myapplication"</code> <code class="k">default</code><code class="o">=</code><code class="s">"compile"</code> <code class="n">basedir</code><code class="o">=</code><code class="s">"."</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"war-file"</code> <code class="n">value</code><code class="o">=</code><code class="s">"${ant.project.name}.war"</code><code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"src-dir"</code> <code class="n">value</code><code class="o">=</code><code class="s">"src"</code> <code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"build-dir"</code> <code class="n">value</code><code class="o">=</code><code class="s">"classes"</code> <code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"docs-dir"</code> <code class="n">value</code><code class="o">=</code><code class="s">"docs"</code> <code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"webxml-file"</code> <code class="n">value</code><code class="o">=</code><code class="s">"web.xml"</code> <code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">property</code> <code class="n">name</code><code class="o">=</code><code class="s">"lib-dir"</code> <code class="n">value</code><code class="o">=</code><code class="s">"lib"</code> <code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">target</code> <code class="n">name</code><code class="o">=</code><code class="s">"compile"</code> <code class="n">depends</code><code class="o">=</code><code class="s">""</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">mkdir</code> <code class="n">dir</code><code class="o">=</code><code class="s">"${build-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">javac</code> <code class="n">srcdir</code><code class="o">=</code><code class="s">"${src-dir}"</code> <code class="n">destdir</code><code class="o">=</code><code class="s">"${build-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;/</code><code class="n">target</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">target</code> <code class="n">name</code><code class="o">=</code><code class="s">"war"</code> <code class="n">depends</code><code class="o">=</code><code class="s">"compile"</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">war</code> <code class="n">warfile</code><code class="o">=</code><code class="s">"${war-file}"</code> <code class="n">webxml</code><code class="o">=</code><code class="s">"${webxml-file}"</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">classes</code> <code class="n">dir</code><code class="o">=</code><code class="s">"${build-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">fileset</code> <code class="n">dir</code><code class="o">=</code><code class="s">"${docs-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">lib</code> <code class="n">dir</code><code class="o">=</code><code class="s">"${lib-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;/</code><code class="n">war</code><code class="o">&gt;</code> <code class="o">&lt;/</code><code class="n">target</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">target</code> <code class="n">name</code><code class="o">=</code><code class="s">"clean"</code><code class="o">&gt;</code> <code class="o">&lt;</code><code class="n">delete</code> <code class="n">dir</code><code class="o">=</code><code class="s">"${build-dir}"</code><code class="o">/&gt;</code> <code class="o">&lt;</code><code class="n">delete</code> <code class="n">file</code><code class="o">=</code><code class="s">"${war-file}"</code><code class="o">/&gt;</code> <code class="o">&lt;/</code><code class="n">target</code><code class="o">&gt;</code> <code class="o">&lt;/</code><code class="n">project</code><code class="o">&gt;</code></pre><p>A <span class="emphasis"><em>build.xml</em></span> file such as this comes with the source code for the examples from this chapter. You can use it to compile your code (the default target) simply by running <code class="literal">ant</code>, or you can compile and build the WAR by specifying the <code class="literal">war</code> target like this:</p><a id="I_15_tt962"/><pre class="programlisting"> <code class="o">%</code> <strong class="userinput"><code><code class="n">ant</code> <code class="n">war</code></code></strong></pre><p>Our <span class="emphasis"><em>build.xml</em></span> file tells Ant to find all the Java files under the <span class="emphasis"><em>src</em></span> tree that need building and compile them into a “build” directory named <a id="I_indexterm15_id783720" class="indexterm"/><span class="emphasis"><em>classes</em></span>. Running <span class="emphasis"><em>ant war</em></span> creates the file <span class="emphasis"><em>myapplication.war</em></span>, placing all of the docs and the <span class="emphasis"><em>web.xml</em></span> file in the correct locations. You can clean up everything and remove the generated <span class="emphasis"><em>classes</em></span> directory and WAR by typing <strong class="userinput"><code>ant</code></strong><strong class="userinput"><code>clean</code></strong> on the command line.</p><p>There is nothing really project-specific in this sample build file except the project name attribute in the first line, which you replace with your application’s name. And we reference that name only to specify the name of the WAR to generate. You can customize the names of any of the files or directories for your own layout by changing the Ant <a id="I_indexterm15_id783764" class="indexterm"/><code class="literal">&lt;property&gt;</code> declarations. The <span class="emphasis"><em>learningjava.war</em></span> file example for this chapter comes with a version of this Ant <span class="emphasis"><em>build.xml</em></span> file.<a id="I_indexterm15_id783785" class="indexterm"/><a id="I_indexterm15_id783792" class="indexterm"/></p></div><div class="sect2" title="Deploying and Redeploying WARs with Ant"><div class="titlepage"><div><div><h2 class="title"><a id="learnjava3-CHP-15-SECT-5.2"/>Deploying and Redeploying WARs with Ant</h2></div></div></div><p><a id="I_indexterm15_id783807" class="indexterm"/> <a id="idx10877" class="indexterm"/>With Tomcat, you can download a client-side “deployer” package, which provides Ant targets for deploying, redeploying, starting, stopping, and undeploying a web app on a running Tomcat server. The deployer package utilizes the Tomcat manager. Similar Ant tasks exist for other servers, such as WebLogic. Making these tasks part of your Ant build script can save a great deal of time and effort. The deployer package can be found along with the main <a class="ulink" href="http://jakarta.apache.org/tomcat/">Tomcat download</a>.<a id="I_indexterm15_id783843" class="indexterm"/><a id="I_indexterm15_id783850" class="indexterm"/></p></div></div></body></html>