What Is The Basic Structure Of The Dist Directory Which Is Made Into A .war File For Tomcat?

- 1 answer

What is the ideal way to create the directory structure when preparing a dist directory to be made into a .war file?



In more complicated projects, I've been known to have a webapp directory in source like this:


Then, using Ant, I make a temporary staging directory and use it to build the war:

<mkdir dir="${staging.dir}"/>
<copy todir="${staging.dir}">
  <fileset dir="${src.dir}/my-webapp" includes="**/*"/>

<war destfile="my-webapp.war" webxml="${staging.dir}/WEB-INF/web.xml">
  <fileset dir="${staging.dir}" includes="**/*"/>
  <classes dir="${build.dir}"/>
  <lib dir="${lib.dir}"/>

However, if your project is simpler (no JSPs, resources, etc.), you might be able to just get away without a defined webapp directory in your source. You could just have your web.xml somewhere appropriate in your source tree, point the <war> task to it, and specify your class/lib directories accordingly.

With regard to your clarifications in the comments, Tomcat (and most containers) expect a structure similar to the following:

my-webapp/WEB-INF/lib/     (for jar files)
my-webapp/WEB-INF/classes/ (for class files)
my-webapp/WEB-INF/web.xml  (deployment descriptor)

The Tomcat documentation section on Deployment has a pretty good explanation of this stuff, too.

Notable: anything you put under WEB-INF will not be accessible directly (e.g. via a URL). Example:

  • If you put JSP files in my-webapp/jsp, you will be able to access them directly at, for example,
  • If you put JSP files in my-webapp/WEB-INF/jsp, you will not be able to access them at the URL in the previous bullet. You would need to provide a way to get to them, for example, by forwarding to them from within a Servlet.

I prefer my JSP files to be in WEB-INF, since I treat them as views in an MVC structure, and have my controllers (Servlets) dictate how they're accessed and rendered.