John Doe
John Doe

Reputation: 9764

Run a multi-module Maven project

This is a basic question, I'm just not really familiar with maven multi-module structures. Say, I have a web application. And I want to connect some modules to it (some services). Do I need to make a web app just one of the modules, dependent from some others, and then run it? At first I thought I could run the whole project, but this option turns out to be inactive in my IDE (I'm using NetBeans now), which made me think I should run something like a main module (a web app in this case). Is it so? Thanks in advance.

Upvotes: 2

Views: 7259

Answers (1)

khmarbaise
khmarbaise

Reputation: 97389

If you have a multi-module project you need a structure like the following which will also be represented by the folder structure as well.

+-- root (pom.xml)
     +--- module-1
     +--- module-2
     +--- module-war 

Whereas the root module contains a thing like this:

<project ..>

  <groupId>com.test.project</groupId>
  <artifactId>parent</artifactId>
  <version>1.0-SNAPSHOT</version>

  <modules>
    <module>module-1</module>
    <module>module-2</module>
    <module>module-war</module>
  </modules>

</project>

In the module-1 your pom should look like this:

<project ..>
  <parent>
    <groupId>com.test.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>module-1</artifactId>

  dependencies for the module

</project>

in module-2 it look more or less the same..and in the war module it looks like:

<project ..>
  <parent>
    <groupId>com.test.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <packaging>war</packaging>
  <artifactId>module-war</artifactId>

  dependencies for the module

</project>

If you have dependencies between the modules for example module-1 depends on module-2 it looks like the following in module-1:

<project ..>
  <parent>
    <groupId>com.test.project</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>module-1</artifactId>

  <dependencies>
    <dependency>
      <groupId>${project.groupId}</groupId>
      <artifactId>module-2</artifactId>
      <version>{project.version}</version>
    </dependency>

    ..other dependencies..

  </dependencies>

</project>

To build and package your project you will go to parent folder and do simply a

mvn clean package

Upvotes: 6

Related Questions