The IT Alchemy Lab really doesn't have any set "purpose" to speak of. It is more about the IT technologies and issues I come across in my day-to-day business, meetings and chats (lunches, coffee and drinks) with my IT colleagues and friends.

Blog Archive

Sunday, May 11, 2008

Creating a war file

Let's get right to the basics. A 'WAR' file is a Web ARchive. To those familiar with Linux (and you should all be) it's nothing more than a tar file. By default it's just a package of files and folders and has no compression. (You can use different flags to compress the data however)

Sun's War how-to

A war file consist of x parts.
1) WEB-INF folder: this is the 'container only' folder that houses resources specific to your proejct
1) WEB-INF/web.xml: The brains of the file. This tells your container what the war file will be accessing and how it is to be accessed.
1b) WEB-INF/jboss-web.xml: If you aren't going to be using JBoss you can skip all of these references. This file is used to manage the JBoss container and will have no impact on other containers.
2) WEB-INF/classes: a default classpath for raw .class files
3) WEB-INF/lib: default folder classpath for your .jar (or .zip if your rebelling)
4) an .htm or .jsp page in the root folder. This is where your files are access as the root folder

Let us setup a new Java Project HelloWorldWar and create the following folders.
./HelloWorldWar/ant
./HelloWorldWar/dist
./HelloWorldWar/build
./HelloWorldWar/web
./HelloWorldWar/web/WEB-INF
./HelloWorldWar/web/WEB-INF/lib
./HelloWorldWar/web/WEB-INF/taglib
./HelloWorldWar/lib
./HelloWorldWar/src

Copy into ./HelloWorldWar/lib file your j2ee.jar and junit-4.4.jar files (IF they are not already in your Eclipse classpath)

Refresh your eclipse project and it should pickup your libraries. We wont be using them for this section, but they will come into play for the next one when we write the helloworld aspect of the war.

The web.xml file.
All we want to do initially is tell the container who we are and what file to load on default.

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<display-name>HelloWorldWar</display-name>
<description>Java Heuristics Web Demo showing the deployment steps for a
web-application</description>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>

Now, for the JBoss people we need to create the jboss-web.xml. There will be more on this later, but for now lets just keep it simple.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.4//EN" "http://www.jboss.org/j2ee/dtd/jboss-web_4_0.dtd">
<jboss-web>
<context-root>HelloWorldWar</context-root>
</jboss-web


And finally, a simple index.jsp page.

<%@ page language="java"%>
<html>
<head>
<title>HelloWorldWar</title>
</head>
<body>
<center><h1>Hello world!</h1> <i>I am static content</i></center>
<%
out.println("<center><h1><font color=\"blue\">Hello World!" + new
java.util.Date().toString() + "</font></h1><br><i>and I am dynamic content</i></center>");
%>
</body>
</html>



With these three items created all we have to do it build our ant file (build.xml), run it and deploy it to our container.

Here is our build.xml (ant file)

<?xml version="1.0"?>
<project name="HelloWorldWar" default="war">
<description>Demo Application for building the HelloWorldWar war
file</description>

<property name="ROOT" value="../"/>
<property name="BUILD" value="${ROOT}/build"/>
<property name="DIST" value="${ROOT}/dist"/>
<property name="SRC" value="${ROOT}/src"/>
<property name="WAR.HOME" value="${ROOT}/web/"/>
<property name="JAR.FILE" value="${WAR.HOME}/WEB-INF/lib/helloWorldDemo.jar"/>
<property name="WAR.FILE" value="${DIST}/helloWorldDemo.war"/>
<property name="CP.ROOT" value="${ROOT}/lib"/>

<path id="CLASSPATH">
<fileset dir="${CP.ROOT}" includes="**/*.jar"/>
</path>

<target name="clean" description="delete any residual code">
<mkdir dir="${BUILD}"/>
<mkdir dir="${DIST}"/>
<delete includeemptydirs="true">
<fileset dir="${BUILD}" includes="**/*"/>
</delete>
<delete file="${JAR.FILE}" />
<delete file="${WAR.FILE}" />
</target>

<target name="compile" depends="clean" description="compile the source">
<javac srcdir="${SRC}"
destdir="${BUILD}"
includes="*/**"
target="1.5"
source="1.5"
classpathref="CLASSPATH"
debug="true"
>
<compilerarg value="-Xlint:unchecked"/>
</javac>
<delete dir="${BUILD}/package cache" />
</target>

<target name="jar" depends="compile" description="jar the application">
<jar jarfile="${JAR.FILE}"
basedir="${BUILD}"
includes="**">
<manifest>
<attribute name="Built-By" value="Steve Gee"/>
<attribute name="user-Email" value="java.heuristics@gmail.com"/>
<attribute name="user-homepage" value="http://ioexcept.blogspot.com/"/>
</manifest>
</jar>
</target>

<target name="war" depends="jar" description="build the war file">
<jar jarfile="${WAR.FILE}"
basedir="${WAR.HOME}"
includes="**">
<manifest>
<attribute name="Built-By" value="Steve Gee"/>
<attribute name="user-Email" value="java.heuristics@gmail.com"/>
<attribute name="user-homepage" value="http://ioexcept.blogspot.com/"/>
</manifest>
</jar>
</target>
</project>



Now, execute our ant file and deploy your war.
From there, open your browser and go to 'http://localhost:8088/HelloWorldWar

You should see the following output:


Hello world!

I am static content

Hello World!Fri Jan 11 09:11:39 CST 2008


and I am dynamic content


1 comment:

JamesBlond said...

A great post, concise and to the point.

Kudos on a job well-done.

About Me

My photo
Don't tell people how to do things, tell them what to do and let them surprise you with their results. --General George S. Patton
Open Source Links | Sourceforge | Slashdot | Open Source