LuckyLuke
LuckyLuke

Reputation: 49087

How to assemble a multi module project in Maven

I am trying to make a multi module project. I have one module for JAR containing models, controller and service classes, and another module for the WAR. I have managed to build the project and generated the outcome, but I am unsure about the structure of my folders.

Currently I have made one Maven module in the same project.

Folders are:

Parent
Module1
Module2

They are all on the same level, is that correct? Or should Module1 and Module2 be inside the Parent folder? And is it possible (or will it happen automagically) to have the produced target to appear in the parent folder instead of in each module? Or maybe there is no need for that.

I am using IntelliJ 11 IDEA.

http://www.sonatype.com/books/mvnex-book/reference/multimodule-sect-intro.html

Upvotes: 3

Views: 2136

Answers (2)

Eyal Golan
Eyal Golan

Reputation: 804

You can either put all modules, and parent in the same hierarchy. A good approach I found is to have a folder for the parent, which will have a pom. In that pom you will need to define the sub modules:

<modules>
  <module>shared</module>
  <module>common</module>
  <module>dal</module>
  <module>logic</module>
</modules>

If all modules on the same folder hierarchy, then you need do something like:

<module>../shared</module>

In the sub modules, you need to set the parent's version:

<parent>
    <artifactId>Aggregation</artifactId>
    <groupId>com.mycomm</groupId>
    <version>3.0.2-SNAPSHOT</version>
</parent>

To have dependency between each module in the same project:

<dependency>
    <groupId>com.mycomm</groupId>
    <artifactId>shared</artifactId>
    <version>${project.version}</version>
</dependency>

If you change the parent's pom, you'll also need to change all sub modules parent's pom. You could use mvn-release-plugin Example:

mvn release:update-versions -DautoVersionSubmodules=true -DdevelopmentVersion=1.0.0.2-SNAPSHOT

Upvotes: 0

AlexR
AlexR

Reputation: 115338

Typically people put modules under parent (both logically and in directory structure). But it is not a law. It is just a recommendation. Now you have to create 4 pom.xml files: one on top level and one per project (parent, module1 and module2). The top level pom should mention all its 3 sub-projects:

<modules>
    <module>parent</module>     
    <module>module1</module>
    <module>module2</module>        
</modules>

If module1 and 2 depend on parent (that is at the same level) just say:

<dependencies>
    <dependency>
        <groupId>com.yourcompany.yourproject</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
<dependencies>

Upvotes: 4

Related Questions