What are the best practices of creating war files (using eclipse) to run on tomcat? tutorials, links, examples are highly appreciated.
You can use Ant to set up, compile, WAR, and deploy your solution.
<target name="default" depends="setup,compile,buildwar,deploy"></target>
You can then execute one click in Eclipse to run that Ant target. Here are examples of each of the steps:
We'll assume that you have your code organized like:
${basedir}/src
: Java files, properties, XML config files${basedir}/web
: Your JSP files${basedir}/web/lib
: Any JARs required at runtime${basedir}/web/META-INF
: Your manifest${basedir}/web/WEB-INF
: Your web.xml filesDefine a setup
task that creates the distribution directory and copies any artifacts that need to be WARred directly:
<target name="setup">
<mkdir dir="dist" />
<echo>Copying web into dist</echo>
<copydir dest="dist/web" src="web" />
<copydir dest="dist/web/WEB-INF/lib" src="${basedir}/../web/WEB-INF/lib" />
</target>
Build your Java files into classes and copy over any non-Java artifacts that reside under src
but need to be available at runtime (e.g. properties, XML files, etc.):
<target name="compile">
<delete dir="${dist.dir}/web/WEB-INF/classes" />
<mkdir dir="${dist.dir}/web/WEB-INF/classes" />
<javac destdir="${dist.dir}/web/WEB-INF/classes" srcdir="src">
<classpath>
<fileset dir="${basedir}/../web/WEB-INF/lib">
<include name="*" />
</fileset>
</classpath>
</javac>
<copy todir="${dist.dir}/web/WEB-INF/classes">
<fileset dir="src">
<include name="**/*.properties" />
<include name="**/*.xml" />
</fileset>
</copy>
</target>
Create the WAR itself:
<target name="buildwar">
<war basedir="${basedir}/dist/web" destfile="My.war"
webxml="${basedir}/dist/web/WEB-INF/web.xml">
<exclude name="WEB-INF/**" />
<webinf dir="${basedir}/dist/web/WEB-INF/">
<include name="**/*.jar" />
</webinf>
</war>
</target>
Finally, you can set up a task to deploy the WAR directly into your Tomcat deploy location:
<target name="deploy">
<copy file="My.war" todir="${tomcat.deploydir}" />
</target>
Once all this is set up, simply launching the default
target from Eclipse will compile, WAR, and deploy your solution.
The advantage of this approach is that it will work outside Eclipse as well as within Eclipse and can be used to easily share your deployment strategy (e.g. via source control) with other developers who are also working on your project.