Maven Project Structure with Multiple Modules

I am somewhat new to the Maven project structure. I am creating a project this is going have two jars. Neither jar is dependent on each other, however, they will use some of the same libraries and there are two helper classes I created (one for logging) that would be used in both.

I was following this guide as far as project structure goes: http://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

My current project only has one module. I am using the IntelliJ IDE to create my build.xml (using ANT) to create my Jars. The IntelliJ build.xml, however, did not work off the bat and I had to do some manual editing to have it build both jars. I believe this would be solved if each jar was in it’s own module. Also, according to the following, it seems they should be anyway http://www.jetbrains.com/idea/webhelp/module.html

This is where I am a little confused. If I do create a second module for my second jar, how do I deal with classes that are shared by both modules? Whenever I go to create a new module , IntelliJ gives the new module it’s own src/ path.

As I said, I am new to the Maven project structure. I am also fairly new to ant and building jars using a build.xml. If I am completely on the wrong path please let me know so I can fix this problem early.

Thanks ahead of time.

Answer

Create a multi-module project (x) which contains a pom.xml with packaging = pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  <artifactId>x</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>
  <modules>
    <module>x1</module>
    <module>x2</module>
  </modules>
</project>

and 2 regular (jar) projects (x1 and x2). This is how the project structure should look like

x
  x1
    ...
    pom.xml
  x2
    ...
    pom.xml
pom.xml

The main project pom should contain dependencies common to both modules. Nested projects poms should have a reference to the parent.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>test</groupId>
    <artifactId>x</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>x1</artifactId>
</project>

See more here http://maven.apache.org/guides/mini/guide-multiple-modules.html

Leave a Reply

Your email address will not be published. Required fields are marked *