Ad

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?

Ad

Answer

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

my-webapp/
my-webapp/WEB-INF/
my-webapp/WEB-INF/web.xml
my-webapp/WEB-INF/lib/
my-webapp/WEB-INF/classes/
my-webapp/WEB-INF/jsp/my.jsp
my-webapp/WEB-INF/tld/my.tld

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="**/*"/>
</copy>

<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}"/>
</war>

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/
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, http://example.com/my-webapp/my.jsp
  • 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.

Ad
source: stackoverflow.com
Ad