web apps & directory layout

May 7, 2007

When servlet spec first appeared there was a trend to maintain web app sources in the same manner as any other non-web java app (bin-src layout pattern, etc. ). Then build process, usually ant, assembled war file by placing classes, libs, web.xml in appropriate places and zipping all together. Even ant war task seems to encourage this ANTI-PATTERN. I never use that task because…

Web app sources should be maintained in a layout as close to real deployment structure as possible. So, web.xml is kept in WEB-INF, compiled java code in WEB-INF/classes and so on. Why?

  • Main motivation is that it’s so easy to setup the proper web development environment with goodies like: java hot-swap, instantly visible changes to view layer (html, jsp, vm or whatever), etc. Just tell server to look for web app at the source files’ location. It’s usually simple addition to server configuration file or something more fancy like tomcat’s context files.
  • Ant scripts that build wars are smaller and easier to understand. They jar folder as-is. No hidden magic added.
  • It’s just simpler – source layout reflects contents of war. When I look at unpackaged war file on server I know exactly where to look for those files in code-base (and vice versa).

All above advocate productive (java) web development. Productive means that when I change java/html/jsp/vm I don’t have to run any ant scripts and/or restart web server to see my change working. Keeping web app source structure conforming to servlet spec is a first step towards productivity.