Create WAR Files with Sun's Free DeployTool
by Richard Shaler
Web applications contain many components of numerous types that reside in numerous locations within a hierarchical structure of a Web application server. But, as you learned in my last article, archiving Web applications into a Web ARchive file can make deployment as easy as copying a single file to your Web application server. In this article, I show you how to create WAR files with a free downloadable tool from Sun Microsystems called DeployTool.
You can deploy Java Web applications without WAR files. Java integrated development environments normally contain some type of publishing or export feature that will deploy an application to a server. However, WAR files can make things easier when you need to deploy to more than one server. For example, you would normally have a test server and a production server. The same WAR file could be used for both servers.
If you decide to use WAR files, there are several ways to create them. You can use most any compression tool, such as WinZip, or take a more sophisticated approach such as using Apache Software Foundation's Ant build tool. Here, I use Sun's DeployTool, a tool that's not too simplistic and not too complex.
How to Obtain DeployTool
DeployTool is part of Sun's Java 2 Enterprise Edition. You obtain it by downloading the J2EE Software Development Kit. The J2EE SDK has a number of powerful features, including an Enterprise JavaBeans-capable Web application server and even a database. Here, only DeployTool will be used, and only its most basic features.
To obtain DeployTool, go to the J2EE downloads page. Select the most recent version of J2EE SDK (at the time of this writing, it was 1.3.1). If you're interested in learning about J2EE, the J2EE download page also contains a very useful J2EE tutorial (in HTML or PDF format) that you should consider downloading.
Once the J2EE SDK is downloaded, unpackage it and install it by executing the file. By default, the J2EE SDK will be placed in a directory on your c: drive called j2sdkee<version_number>. You can find documentation for the J2EE SDK at c:\j2sdkee<version_number>\doc\index.html.
Once installed, set two environment variables: Java_Home and J2EE_Home. Set Java_Home to your Java Standard Edition (J2SE) SDK installation root directory (c:\jdk1.3.1_02). It's likely your Java_Home is already set. Set J2EE_Home to the J2EE SDK installation root directory (c:\j2sdkee1.3.1).
If you want to use any other J2EE SDK feature, such as the J2EE engine (not required to create a WAR file with DeployTool), run the setenv.bat file, found in the <j2ee_installation_root>/bin/ directory.
Sample WAR File Creation
Start the DeployTool by running the deploytool.bat file from the <j2ee_installation_root>/bin/ directory. A command prompt window will appear with an informational message"Could not connect to local host"but don't be concerned with it. (The message appears because J2EE SDK's J2EE engine isn't running.) In addition to the command prompt window, the DeployTool application should appear.
DeployTool is actually Enterprise ARchive (EAR) file centric and will always build an EAR file, even though, here, we're only concerned with creating a WAR file. With DeployTool, you always start at the application (EAR file) level. In other words, you must always create a new application or open an existing one (an EAR file) before you can create a WAR file.
Create a new application by selecting New from the File menu, and then select Application:
A dialog box will appear, prompting you for the application file name (an .ear extension will automatically be appended to your file name) and the application display name. I used MyTest for the application file name and My Test Application for the application display name, as illustrated here:
After you enter the application file, the main DeployTool application screen will reappear. If not already selected, select your application (in this case, My Test Application):
Now you’re ready to create what DeployTool calls a Web Component. Here, I don't actually create a Web component, but instead simply add contents (my application resources) to a WAR file. Select New from the File menu and select Web Component:
The Web Component Wizard will present the Introduction dialog (informational only). Review the information if you'd like to, and click the Next button. The WAR file dialog box will appear.
Change the WAR display name to whatever you want to call your WAR file (normally the name of your application). I used MyTest. (A .war extension will automatically be appended to this name when you save the application.)
Next, select the application resources (contents) to be included in the WAR file by clicking the Edit button. The Edit Contents dialog box will appear. At the top of the dialog box, enter the directory from which you would like to select your components. In this case, I start at the Apache Tomcat 4.0 directory because I know my previously published application (custlastname) resides within its webapp folder.
Normally, an undeployed application would not reside in the application server's deployment directories. But to keep things simple, I'm creating a WAR file from a previously deployed application.
Select the resources (files) you want to include in your WAR file, and click the Add button. This will add the selected files to your WAR file. You can select individual components or select the directory in which they reside. Selecting a directory will cause the directory and all of its contents to be copied to the WAR file. Be careful when selecting entire directories, because it could impact the directory structure of your application. Here, I selected each file individually while holding down the Ctrl key. I was then able to add the entire group with a single click of the Add button.
Once you've selected all of your application's contents, click the OK button and you'll be taken back to the initial Web Component Wizard dialog box. However, now the contents section of the dialog box will contain the resources you added to your WAR file.
Click Next and the Choose Component Type dialog box will appear. As mentioned earlier, I'm not really creating any components, only adding contents to a WAR file. I indicate that by selecting the No Component radio button:
After you select No Component, click the Next button and an empty dialog box will appear. From this dialog box, click the Finish button. You will be taken back to the initial DeployTool application display.
Save the WAR File by selecting the WAR file (in this case MyTest) in the left frame (not the application) and selecting Save As from the File menu.
I saved my MyTest WAR file to my Tomcat's webapp directory. I then restarted Tomcat and ended up with a deployed application called MyTest.
DeployTool will create some special files (for example, sun-j2ee-ri.project) required by J2EE SDK's J2EE engine, but you can simply ignore them.
Give DeployTool a Try
WAR files make Java Web application deployment simple. You should consider using them, especially when you're targeting more than one server. Sun's DeployTool makes WAR file creation simple. DeployTool provides a straight-forward, stand-alone method for creating WAR files that doesn't cost you a dime. I found a few quirks with the tool, but it seems to do the job.
Contact the Editors
Last Updated: 6/6/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.