First of all, I know how to build a Java application. But I have always been puzzled about where to put my classes. There are proponents for organizing the packages in a strictly domain oriented fashion, others separate by tier.
I myself have always had problems with
- naming,
- placing
So,
- Where do you put your domain specific constants (and what is the best name for such a class)?
- Where do you put classes for stuff which is both infrastructural and domain specific (for instance I have a FileStorageStrategy class, which stores the files either in the database, or alternatively in database)?
- Where to put Exceptions?
- Are there any standards to which I can refer?
I’ve really come to like Maven’s Standard Directory Layout.
One of the key ideas for me is to have two source roots – one for production code and one for test code like so:
(here, both src/main/java and src/test/java are source roots).
Advantages:
One rule of thumb about class placement and packages:
Generally speaking, well structured projects will be free of circular dependencies. Learn when they are bad (and when they are not), and consider a tool like JDepend or SonarJ that will help you eliminate them.