Template for Java projects

January 24th, 2003  |  Published in java  |  8 Comments

Every time I start a new java project, no matter what size, the first thing I do is go hunting through my java directories looking for one to use as a template. Over time I’ve gathered some pretty useful ant targets and settled on a fairly rational directory structure. Today I got round to building a skeleton set of directories and files that I can reuse in the future. Here’s a tarball of the results.

There are some features of the template that are specifically for deploying web apps into Tomcat. They can be easily ignored if you’re not doing that. The directory structure goes like this:

  • etc – put any misc files to go in WEB-INF in here and they’ll be copied over when the project is deployed. web.xml lives in here.
  • lib – put the .jar files your code depends on in here and they will be automatically put into the classpath for compilation and deployed to the right place in the webapp.
  • src – your java source.
  • tests – the java source of your junit tests for the code in src.
  • web – the web document tree for deployment.

Here are the targets in the ant buildfile:

  • compile – compiles the java from src into build.
  • test – compiles the source and the tests and runs every test in tests using junit.
  • clean – nukes the build and testbuild directories.
  • with.jikes – add this before any target that compiles to set jikes as the build compiler.
  • with.clover – add this before any target that compiles to instrument the .class files with clover.
  • clover.report – runs the clover reporter. Best used after running tests, as in ant clean with.clover test clover.report.
  • clover.report.html – runs the clover html reporter.
  • deploy – compiles and deploys the project into a tomcat webapp directory, copying the classes, config files from etc, web tree from web and the jars from lib.
  • ctags – runs ctags -R over the src and tests directories. This is run automatically by the compile targets.
  • javadoc – compile javadoc into javadoc.
  • run – run some code from your source with the classpath all set up correctly for your libs and source.
  • pause – pauses and waits for carriage-return before running the next target. I use this to make ant more responsive; I find that it can take several seconds for ant to start up and parse its build file, and I like to run the compiler a lot while I’m coding so that I can check my tests as often as possible. In the shell, I use this line: while true; do ant with.jikes pause test; done. This means that every time you hit enter, the compile-and-test run is done then ant makes itself ready for the next run.


  1. Lost Boy says:

    January 24th, 2003 at 9:07 pm (#)


    This is spooky. Matt Biddulph has just posted a template for Java projects which is basically a tar ball of

  2. Aslak Helles says:

    February 6th, 2003 at 1:44 pm (#)

    You should try Maven. It defines a structure and have reusable ant tasks packaged as Maven plugins.


    It’s awesome!

  3. Mats Henricson says:

    February 6th, 2003 at 7:04 pm (#)


    This is great, but I have one objection: tests should not be in its own directory. I think they should be in the src directory, together with the code they are testing.

    BTW, I now have working targets for javancss, pmd, checkstyle, cvschangelog, jcsc, jmetra. It is like a drug… can’t… stop… adding… cool… targets… :-)


  4. Matt Biddulph says:

    February 6th, 2003 at 8:19 pm (#)

    When I build a project with this template, I put the tests in their own directory but with an identical hierarchy to the source. Each test also goes in the same java package as the source it’s testing, to help with testing protected methods. So com.hackdiary.foo.Bar lives in src/com/hackdiary/foo/Bar.java and its tests go in tests/com/hackdiary/foo/BarTest.java.

    The reason for keeping a separate test directory structure (apart from neatness) is to allow for separate compilation, packaging and deployment when I don’t want to include the tests.

  5. Jayson says:

    May 12th, 2003 at 3:28 pm (#)

    Good idea.
    Have you tried maven? or for that matter turbine?

  6. Ben Meadowcroft says:

    September 2nd, 2003 at 12:55 pm (#)

    Looks like quite a good list, when developing Tomcat stuff I like to have a dist target that packages the build in a WAR file. This is really handy for deploying the same app to multiple servers (like I’m doing at the moment) especially with the file upload interface on the manager console.

  7. Ankit says:

    February 16th, 2004 at 4:45 pm (#)

    it was nice to see a java site which provides good help for the students.

  8. peabin says:

    April 19th, 2004 at 10:44 am (#)

    free java projects