views:

280

answers:

6

In a software-project written in java you often have resources, that are part of the project and should be included on the classpath. For instance some templates or images, that should be accessible through the classpath (getResource). These files should be included into a produced JAR-file.

It's clear that these resources should be added to the revision-control-system. But in which directory you put these files? Parallel to the java-source-files or in another directory that also reproduces the needed package-structure?

+1  A: 

Personally, I'd prefer having them together with the source code if they are strongly related to specific parts of the code, and if there are not too many of them.

But if it makes sense for those files to have their own organization structure, or if there are hundreds of files that would make it hard to find the source code amongst them, I'd keep them separate.

Michael Borgwardt
+4  A: 

Maven puts them into src/main/resources/ (Java code would go to src/main/java/). The main reason is that Maven can compile code in various languages and it makes sense to keep them separate so one compile isn't confused by files for another.

In the case of resources, Maven will replace variables in them before they are added to a Jar, so they are "compiled" sources as well.

Aaron Digulla
+1  A: 

I prefer your latter solution: a separate directory that mimicks the source code’s package structure. This way they won’t clobber your source code but will be right next to the compiled class files in the packaged JAR file.

Bombe
+1  A: 

It depends on your preferences. Having them mixed with the source code files makes refactorings like renaming of packages easier (as the ressources are moved toghether with the sources), but with more than a handful of icons, images and localization files, you get a clutter of files very easily.

With todays tools it also doesn't really matter. Whether you use Eclipse, Netbeans or another tool, they all allow you to have binary packages with a different layout than source code. So in the end you can do it like you want.

Personally I try to avoid mixing source with resources, because I typically change resources very seldom, but source code very frequently.

Bananeweizen
A: 

You can put these things in the classpath if your build tool doesn't get confused. Eclipse won't try to compile your jpegs and neither will Ant, so there isn't much to worry about. The nice thing is that if you have lots of related things you can keep them together, organized by functionality as opposed to by file type.

We do this where I work; we have email messages which require a bit of code and a handful of static resources. All of these things are kept together in one folder in the java source path; we can add and remove an email from the system easily, without forgetting things or making mistakes like src/resource paths mismatched or orphan files in one tree not present in another tree.

Mr. Shiny and New
+1  A: 

I usually go like this:

project/src
project/resources
project/classes
project/lib
project/dist

Both src and resources have the package structure and the build file take classes and resources as input for the jar which is placed in dist.

When running internally the classpath is something like: lib;classes;resources;

If the app has an installation the installer creates and places the resources directory intact in the instalation.

OscarRyz