Skip to end of metadata
Go to start of metadata

Multi-modules projects

Imagine you are working on a project based upon a traditional 3 layered architecture, in which the layers are named presentation, business and integration. Of course, you want each layer to be independant of all the other layers and so you want to produce one artifact for each one. Following the Maven norm, you will need to have one project for each layer. Hence, your setup should look like this :
So to compile your presentation project, you would first need to compile the business project. But wait, the business project depends upon the integration project so it should be compiled first. Ok, let's recapitulate. So to compile your presentation project, you would need to type the following commands (we assume the projects have been put in a common root directory):

/root_directory$/ cd integration
/root_directory/integration$ mvn compile
/root_directory/integration$ cd ..
/root_directory$/ cd business
/root_directory/business$ mvn compile
/root_directory/business$ cd ..
/root_directory$/ cd presentation
/root_directory/presentation$ mvn compile


Few! 8 lines just to compile! I thought Maven was simple. Of course, Maven offers a solution to this problem in the form of a multi-modules project.

A multi-modules project is a very particular type of project - it doesn't produce any artifact and is composed of several other projects known as modules. When you run a command on the project, it will execute it on all of its children projects. Even better, Maven is able, through its reactor component (don't worry about it for now), to discover the correct execution order and to detect circular dependencies. So let's apply this solution to the last example. Your project should have the following structure at the end of this lesson:

First, create a new directory named after your project, something like myproject (original isn't it?). Now let's write the pom file for your multi-modules project:
As you can see, there is nothing very complex in this pom file. It is a normal parent project's pom file, which was introduced in the lesson [todo : link].  Only, this time there is something new: the modules section. The modules section allows you to declare the child modules of a parent project, hence the name multi-modules project. Modules are regular Maven projects which must be located under a subdirectory so the parent project can retrieve them easily.

Modules location

Icon

Maven doesn't force you to place your modules at a specific location. You can put them wherever you want as long as they are under the parent project's directory and their paths are declared correctly in the modules section (for instance, modules/presentation). However, there is no point in not placing them directly under your parent project unless you really need to because of some external constraints.


Next, create the following subdirectories and the pom.xml files that go with them [todo : explains the directory name restriction]:
 
 
Now let's fill the presentation project's pom file:
 

  • No labels

5 Comments

  1. Guys, have you missed the dependency version on the last image (with pom.xml file listing)?
    I've omitted it in my code, and Maven starts complaining that dependency version must be specified.

    Thanks,
    Jakub Pawlowicz

  2. Hi, this is good until you put it into SVN.  you get this directory structure:

    now if you want to branch it, you have to branch the entire parent-project and all its children - branching sub projects in directoreis under trunk can be a nightmare to use.

    Ideally we would want:

    etc.  now all projects can be branched independently, but there is a still a parent project to compile them all. The question is, can maven support a parent project which doesnt contain the directories of the children inside it?

  3. In addition, if the parent project has a common dependency specified, e.g.:

    Then you have to put this in each child project pom also (at least for the child project to work in eclipse).  There seems to be no inheritance of dependencies between parent and child.

    I created a webapp-parent, in the hope that each child webapp project, as created as a maven module of the parent using the m2eclipse maven-archtype-webapp, would not have to have this dependecy added to work.  Not sure why a webapp archetype would not contain this by default, or why children cant look to their parents for dependencies.

    The pom documenttion http://maven.apache.org/pom.html says:

    then POMs inheriting from this one can set their dependency giving the groupId=junit and artifactId=junit only, then Maven will fill in the version set by the parent.

    However, if you try putting just this in the child pom:

    It wont build, you get:

  4. Hi,

    Seems like this is not working at all as the parent version is missing.

  5. Simon, you should use dependencyManagement within parent pom

    eg within the parent pom : 

    then within the child pom :