Take the 2-minute tour ×

We are building a small application using different architectural layers such as domain, interface, infrastructure and application. This follows the Onion DDD model. Now I am wondering if there is any benefit in splitting the application into a multimodule maven project. As far as I can see now it seems to make things more difficult than needed. The entire application will be deployed as a single WAR file into a Tomcat container.

share|improve this question
What exactly is more difficult than having a single module? –  khmarbaise Jul 13 '11 at 8:32
The difficulty to use a consist Spring applicationContext.xml through all modules. I guess that i need to create an applicationContext.xml for usage within Spring for all seperate modules, or am i missing something? –  Marco Jul 13 '11 at 13:21
You shouldn't need multiple application contexts. You can have several separate modules, and only one applicationContext.xml to wire up dependencies from those modules. –  Lyle Sep 21 '12 at 22:23
add comment

4 Answers

up vote 4 down vote accepted

Splitting your application makes sense for the following:

  • When a certain part of the project needs to have new functionality or bug fixes, you can simply focus on that module and run just the tests for it. Compiling a fraction of all the code and running just the related tests speeds up your work.

  • You can re-use the code from the modules across different projects. Let's assume your project contains some well-written generic-enough code for mail sending. If you later have another project that need mail sending functionality, you can simply re-use your existing module or build upon it (in another module by adding it as a dependency).

  • Easier maintainability on the long run. Maybe now it seems like a small project. In a few months things might look different and then you'll need to do more refactoring to split things into logical units (modules).

  • Conceptual clarity (as added by Adriaan Koster).

Concerning the WAR: You can have an assembly module which puts things together and produces a final WAR file from all the related modules.

Initially, this may seem as more work, but in the long-run, modularized projects are easier to work with and to maintain. Most sane developers would prefer this approach.

share|improve this answer
Another benefit: it enforces conceptual clarity of your architecture. –  Adriaan Koster Jul 13 '11 at 10:25
add comment

Using multiple modules forces you to have a hierarchy of dependencies. You have one module which is standalone and doesn't depend on any other of your modules. You have another which only depends on that. It might appear harder than allowing anything to depend on anything else but this approach results in a mess of dependencies which is hard to fix later.

If you are trying to follow a layered model I suggest you place each layer in a different module. This will ensure you are not tempted to break the model.

share|improve this answer
add comment

Spliting your project into multiple maven projects is useful if you want to reuse your classes in another project or if your projects are deployed in different configurations.

Maybe think of a webservice - if you are hosting the server, you could build a project for your domain classes (models) and your endpoint interfaces that could be used by server and client. The server would be another project that is build to a WAR.

To develop further clients the first project could be used, too.

Use a parent project for dependency management on common projects (like logging) and different profiles and build configurations.

share|improve this answer
add comment

As far as I know, Maven do little help for WAR dependencies. As you are talking about single WAR, this should never be a problem.

You can separate java classes into several "jar" submodules, but if you split the WAR project into several smaller WARs, using some kind of "overlapped" packaging things get complicated.

Just information, one of our projects, it contains too many web pages, so we decided to split it into several WAR submodules, however, the session is not shared between different WARs deployed, and we are not going to use Kerberos stuff. At last, we modified a lot sources of Glassfish, Jetty, MyFaces, etc. To make them resolve web.xml stuff inside JARs. And converted the whole project to Facelets 2.0 (to avoid the dependency of JDK tools.jar and custom resource handler), the only reason is to change the WAR submodules to JAR submodules, and move all webapp/pages into class resources. So the conclusion, Maven does great job for JAR dependencies, but no WAR or single WAR.

EDIT You can put applicationContext.xml in one of the base submodule, and import it by classpath:com/example/applicationContext.xml. Also Spring 3.0 do have annotation supports, you can make spring auto scan them instead of declaring them all in the xml.

share|improve this answer
add comment

Your Answer


