简体   繁体   中英

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

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM