Friday, September 26, 2008

Eclipse Ant Tutorial Building War File Struts Step 12

Now that you have your Struts set up with an Action class, a Action Form, the Struts.xml, a jsp page, your libraries and the web.xml files, you are ready to create your War file and test your configuration on the Tomcat Server. If you haven't gotten this far, see my prior posts on setting up your file structure in eclipse. You now need to create a build.xml file.

Create a build.xml file under the UserBook directory. The build.xml file should be at the same tree level as your src file. If you are following my tutorial, paste the following code into your build.xml file.


<project name="UserBook" default="war">
<!-- ============ Project Settings ====================================-->
<property environment="env" />
<property name="PROJECT_HOME" value="./" />
<property name="BUILD_DIR" value="${PROJECT_HOME}/antBuild" />
<property name="JAVA_BUILD_DIR" value="${BUILD_DIR}/classes" />
<property name="CONTENT_DIR" value="${PROJECT_HOME}/content" />
<property name="JAVA_SRC_DIR" value="${PROJECT_HOME}/src" />
<property name="CONFIG" value="${PROJECT_HOME}/config" />
<property name="LIB_DIR" value="${CONTENT_DIR}/WEB-INF/lib" />
<path id="class.path">
<fileset dir="${LIB_DIR}">
<include name="*.jar"/>
<!-- =========== Clean ====================================================== -->
<target name="clean">
<delete dir="${BUILD_DIR}" />
<delete dir="${CONFIG}/WEB-INF" />

<!-- =========== Init System ================================================= -->
<target name="init" depends="clean">
<format property="now" pattern="MMM d yyyy hh:mm aa" />
<!-- Create build dir -->
<mkdir dir="${BUILD_DIR}" />
<mkdir dir="${JAVA_BUILD_DIR}" />
<mkdir dir="${CONFIG}/WEB-INF" />
<!-- =========== Compile Java Code =========================================== -->
<target name="compile" depends="init">
<javac debug="on" classpathref="class.path" debuglevel="lines,vars,source" optimize="on" destdir="${JAVA_BUILD_DIR}">
<src path="${JAVA_SRC_DIR}" />
<!-- =========== Jar Process ================================================== -->
<target name="jar" depends="compile">
<!-- increase the build number -->
<buildnumber />
<echo>Create Jar</echo>
<jar jarfile="${JAVA_BUILD_DIR}/UserBook.jar">
<fileset dir="${JAVA_BUILD_DIR}">
<include name="com/**" />
<copy file="${JAVA_BUILD_DIR}/UserBook.jar" todir="${LIB_DIR}" />

<!-- =========== War DEV ======================================================= -->
<target name="war" depends="jar">
<war warfile="${BUILD_DIR}/UserBook.war" webxml="${CONTENT_DIR}/WEB-INF/web.xml">
<attribute name="Built-By" value="${}" />
<section name="Struts 1">
<attribute name="Implementation-Title" value="UserBook" />
<attribute name="Implementation-Build" value="${build.number}" />
<attribute name="Implementation-Build-Date" value="${now}" />
<attribute name="Implementation-Vendor" value="" />

<!-- include everything in content directory -->
<fileset dir="${CONTENT_DIR}" />
<lib dir="${JAVA_BUILD_DIR}">
<include name="*.jar" />
<lib dir="${LIB_DIR}">
<include name="*.jar"/>
<classes dir="${BUILD_DIR}/classes">
<exclude name="*.war" />
<copy file="${BUILD_DIR}/UserBook.war" todir="${env.CATALINA_HOME}/webapps" />

At the top of the build.xml file you have the project name which in our case is UserBook.

Looking at the project settings, you will see a list of variables that will be used in your build.xml file. The environment="env" is pulling in the environmental variables that will be used. We need to know the environmental variables to know what the Tomcat path is. The Project_Home is the home of your current project. To use the Project_Home variable, you can put it like this: ${Project_Home}. So the other variables are using the Project_Home variable to declare variables in other areas of your project. We have one that goes to your library directory, your src directory, your content directory, antBuild directory and your classes directory.

The first thing you do when building your project is clean the project by deleting the Ant Build directory.

The next thing you do is to initialize the system by stamping a time date on your war file, and then making sure you have the ant build directory created.

The depends says "clean" which means that this target depends on clean. That means that clean will happen first, and then the init will take place.

The next target you have is compile which compiles your java code and puts the classes in your ant build directory.

Below that you also create a jar file. This jar file can be used later if you need to use these classes in another project. You have a copy that copies the jar file into the lib directory in your ant build directory.  Within the war file section, you want to tell ant what files to include in the war file.

After you create your war file, you will copy the war file to your tomcat webapps directory.

Now you need to start your tomcat server to see if everything is working correctly.

The first time you build your file you need to select the right mouse button on your build.xml file and select "run as" ant build.  After the first time you run the build, you can select the green icon at the top of eclipse to build the project.

After you build your file, you should see printing in your console that will tell you the order and what is taking place while eclipse is building your War file. First, the build file should be cleaning your system, then running init, then compile, then create your jar files, and then it will create your war file. If your build was successful, it will say Build Successful. If there was a problem, then eclipse will give you an error that the code didn't compile correctly and you will need to fix it.

Before starting Tomcat, I usually will delete the directory from the webapps folder. You don't have to delete the War file, but just the directory that says UserBook. After you build your War file, you should be able to look in this directory and see a file called UserBook.war which is the file you just created.

If you installed the Tomcat plugin for eclipse, you should see a cat at the top of the menu in eclipse. When you select the cat, tomcat will start and you should see information in your console. If you see any stack traces of Java code while Tomcat is starting, then there might be a problem with your xml mappings. If you see
INFO: Server startup in 27813 ms then your Tomcat was started successfully.

Now you need to open a browser and goto this URL to see if your project is working correctly.


The localhost is the tomcat on your machine, and the 8080 is the port number for this Tomcat instance. The UserBook is the name of your project and your War file.

If you see "This is a test" then your project is working and you successfully configured Struts 1.

Your index.jsp page forwarded the user to your which is in your struts mapping. Struts will call the action method in your action class called forwardToUser. After the forwardToUser method is finished, it will return a mapping.findForward("success") which will read the struts.xml file and send the user to the user.jsp page.

In my next post I will show you how to set up tiles with struts. Tiles is used to seperate your application into sections to make it easier to manage.

Go To Step 13


Nirodha November 11, 2008 at 11:36 PM  

Thanks for your effort, this tutorial is really nice,
I am getting "Servlet action is unavailable"

Please help me

Greg November 14, 2008 at 10:54 AM  

Sorry I didn't look at your comment earlier. If you are still getting that error, post some of your stack trace, and your struts config file and your web.xml file. Thanks.

Nirodha November 25, 2008 at 9:42 AM  

Hi Greg,
Hope you are doing good.
Once I completed up to this step I got only a blank page, so I added another method called "execute" in the UserAction class and deleted "?action=forwardToUser" in the index.jsp.

After that I got the desired out put. Can you imagine where I got wrong here.

Thanks for your blog, I am enjoying with your writings regarding java/struts/hibernate.

Greg November 26, 2008 at 3:25 PM  

You will use the execute method when extending action class. For example:
public class UserAction extends Action

If you extend Action, then you will use the execute method. In our case, we want to extend DispatchAction. This way, you can name your action methods.

Make sure your struts action mapping contains the parameter="action" like this:
<action path="/UserAction" name="userForm" type="com.action.UserAction" parameter="action" scope="session">

Nirodha December 1, 2008 at 10:40 AM  

Thank you so much greg

Greg December 2, 2008 at 11:13 PM  

sure, anytime.

javarevisited March 7, 2011 at 11:20 PM  

One of the most important feature of eclipse is remote debugging which gives it an edge over other IDE and that was the single biggest reason I switched over from Netbeans to Eclipse. to learn about eclipse remote debugging you can check here.

swathi October 3, 2011 at 6:39 AM  

I want to generate html file dynamically in war file.and the generated file i want to how to create a file dynamically in war file and read using struts2.0,tomcat5.5 and eclipse.

Dhemz October 3, 2011 at 6:40 PM  

I think you will need to give me more information. You don't generate HTML files dynamically. That is what JSP, ASP or another language does. A JSP page will dynamically create HTML and will send a response to the browser. If you follow my tutorial from the beginning, you will learn how to display data in a JSP page. However, my tutorial is using Struts 1 so you will need to adjust for Struts 2. I don't know what you mean by reading a HTML file.