aspose file tools*
The moose likes Ant, Maven and Other Build Tools and the fly likes J2EE Deployment using ANT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » Ant, Maven and Other Build Tools
Bookmark "J2EE Deployment using ANT" Watch "J2EE Deployment using ANT" New topic
Author

J2EE Deployment using ANT

Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Hello All,
I don't have much experience with Ant.
I have a J2EE web app where in I have a few servlets, JSPs and JavaBeans.

Can somebody post a build script as an example that I can use to deploy my web app.

Regards,
Ritu
SCJP
Kristin Stromberg
Ranch Hand

Joined: May 17, 2005
Posts: 91
Hi Ritu - there's a sample Ant script right here on JavaRanch (part of the Cattle Drive). Scroll down to the "Ant" section.
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Hi Kristin!

Thanks for posting.
I think there is a typo in the build.xml (Lne. No. 10) script.

Code Snippet
------------------------------------------------------------------------
<project name="bee" default="build" basedir=".">

<target name="init">
<property file="${user.home}/build.properties"/>
<property name="app.name" value="bee"/>

<property name="src.dir" location="${basedir}/src"/>
<property name="resources.dir" value="${basedir}/resources"/>
<property name="build.dir" location="build"/>
<property name="classes.dir" location="${build.dir}/classes"/>

<property name="dist.dir" location="${build.dir}/dist"/>
</target>

-----------------------------------------------------------------------

What is "build" there? I guess "build" is the default target.
Correct me if I'm wrong.

Also, it will be very nice of you if you could explain me the "war" tag listed below. I hope that I'm not asking for much.

Code Snippet
-------------------------------------------------------------------------
<target name="war" depends="compile">
<war destFile="${dist.dir}/${app.name}.war"
webxml="${resources.dir}/WEB-INF/web.xml">
<classes dir="${classes.dir}"/> <!-- the java classes -->
<zipfileset dir="${resources.dir}"
includes="**/jr.jar"/> <!-- WEB-INF/lib -->
<fileset dir="${src.dir}"
includes="**/*.jsp"/> <!-- web page stuff -->
</war>
</target>
---------------------------------------------------------------------------

Regards,
Ritu
SCJP
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


What is "build" there? I guess "build" is the default target.
Correct me if I'm wrong.

You are wrong. It is a directory called "build". Have you read the Ant documentation? It explains the war task quite well.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Hi,
Why r the simple things getting so complex here....
Is "build" a pre-defined dir?

Regards,
Ritu
SCJP 2
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you are finding it complex - slow down. Build is a directory know to whomever wrote that build file. Ant assumes nothing about the file system.

May I suggest you take a moment to write down the steps involved in deploying your web app (i.e. compile and java source, war up the jsps etc., copy the file to the server etc.) and look at the available Ant tasks which can do the work for you? You might end up with an Ant file which looks something like this:

Of course as you get better with Ant you will discover there are far better ways of doing this. But try walking before you run.
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Hello Paul,
Thanks for your patience!

I am making things crystal clear.

1.My Development Directory is D:\JavaProjects
2.Following is the structure of JavaProjects directory
build.xml[the actual build script]
./src/jsp/hello.jsp
./src/html/index.html
./src/myapp.xml[to be used as the DD web.xml file]
./src/UserRegistrationServlet.java [servlet source code]

3.Following is the code snippet of my Ant build script (build.xml)
<?xml version="1.0"?>
<project name="MyProj" default="war" basedir=".">

<property name="MyProj" value="MyProj" />
<property name="build" value="${basedir}/build" />
<property name="source" value="${basedir}/src" />

<target name="init" >
<tstamp />
<mkdir dir="${build}" />
</target>

<target name="compile" depends="init" >
<javac srcdir="${source}" destdir="${build}" />
</target>

<target name="war" depends="compile">

<war destFile="${MyProj}.war" webxml="${basedir}/src/myapp.xml">
<classes dir="${basedir}/build" />
<fileset dir="${basedir}/src/html" />
<fileset dir="${basedir}/src/jsp" />
</war>

</target>

</project>

4.And it worked, I have successfully created a WAR file. JavaRanch works!

5.But, my next query is that my Tomcat is installed in C:\jakarta-tomcat-5.5.9 directory. I don’t wanna manually move the WAR file to the webapps directory of my Tomcat installation. So, how can I use the copy Task to achieve the same.
I thought of doing this:

….
<property name="tomcat" value="C:/ jakarta-tomcat-5.5.9/webapps" />
…..
<war destFile="${MyProj}.war" webxml="${basedir}/src/myapp.xml">
<classes dir="${basedir}/build" />
<fileset dir="${basedir}/src/html" />
<fileset dir="${basedir}/src/jsp" />

<copy todir="${tomcat}" file="${MyProj}.war" />

</war>
Is there any other way?

6.Secondly, when I manually copied the MyProj.war file to the webapps directory of my Tomcat installation and restarted the container, I was glad to notice that the web.xml (DD) and servlet class file landed at the desired locations (inside WEB-INF, and WEB-INF/classes dirs respectively)
But,
The JSP and HTML page laded in the web app home i-e
C:\jakarta-tomcat-5.5.9\webapps\MyProj\hello.jsp
C:\jakarta-tomcat-5.5.9\webapps\MyProj\index.html

I though that it would land in the respective jsp and html folders. How can I do that?

Pls. help.....

Regards,
Ritu
SCJP
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Anybody there to help :roll:
Ranchers pls. come foreward ...
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
It looks like a dead thread.
Anybody alive out there!
I thought that JavaRanch is the best site to get answers to your Java related queries.

Ranchers pls. help.
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
Patience Ritu, patience. We're all volunteers around here and your post is really long.

I'll take a shot at your #6. You wanted your jsp stuff in a jsp directory and html stuff in an html directory. You'll need to tweak your filesets.

Try something like this:
<fileset dir="${basedir}/src" includes="html/**/*" />
<fileset dir="${basedir}/src" includes="jsp/**/*" />
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Hi Enderlin,
Thanks! Things are working fine now.

Now, I have successfully created a WAR file, and when I manually place that WAR file in my servlet container’s (Tomcat) webapps directory, restart Tomcat, things are located in the right directories as desired.

My next task is to copy the WAR file using the <copy> task. But, that requires hard coding the Tomcat root directory if my Tomcat is not installed in the ${basedir}.

This is what I do:

<property name="tomcat" value="C:/jakarta-tomcat-5.5.9/webapps" />

<war …>

</war>
…..

<copy todir="${tomcat}" file="${MyProj}.war" />


Although this works, but is there any other way I can copy the WAR file to the desired location?

Regards,
Ritu
SCJP
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
The usual way to avoid hard-coding stuff in your build file is to put it in a properties file, which is then included in the build file. Check out the Ant Manual -> Using Ant -> Properties section; it explains how to go about that.
Alternatively, if your Tomcat path is defined in an environment variable, then Ant can pick that up too (explained in the same section of the manual).


Ping & DNS - my free Android networking tools app
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Yes, I have an environment variable called "CATALINA_HOME" which points to my Tomcat installation dir.

How can i get it's value in the Ant build file?
I have gone through the "Using Ant" section of Ant's manual but didn't get that....

Can anybody help...

Regards,
Ritu
SCJP2
Carol Enderlin
drifter
Ranch Hand

Joined: Oct 10, 2000
Posts: 1364
From the Ant manual - Property Task their example uses ANT_HOME environment variable:

<property environment="env"/>
<echo message="ANT_HOME is set to = ${env.ANT_HOME}"/>
[ July 31, 2005: Message edited by: Carol Enderlin ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Actually, I don't copy the war file into Tomcat, I tell Tomcat to look for the war file in my project.

To do this, you set up an application Context - which I almost always have to do anyway, just for external control of database and other JNDI definitions. Although you can do this in server.xml, this is discouraged in favor of placing the Context definition in its own file (in Tomcat 5, that's TOMCAT_HOME/conf/Catalina/localhost/).

When you set up the context's docBase, you can point it to a WAR in your Ant project OR to an exploded WAR in your Ant project. An exploded WAR is good for quick debugging if you remember to back-propagate whatever changes you do there to the project sources from which it was build.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ritu Verma
Greenhorn

Joined: Jul 26, 2005
Posts: 10
Thanks All!
I would like to thank all the Ranchers who posted here....
It was indeed a gr8 help

I copied the WAR file to my Tomcat Installation directory by using a properties file.


<property file="conf/config.properties"/>
......
......
<copy todir="${CAT_HOME}" file="${dist}/${MyProj}.war" />


It works!

Thanks again!

Regards,
Ritu
SCJP 2
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: J2EE Deployment using ANT