87

Right now I usually find a pom.xml file on the web that has a pom packaging and copy and paste it to create my parent project. Then I used to run archetype:create inside the parent directory to create sub modules but archetype:create has become deprecated since then.

Any recommendations on how to create new Maven multi-module projects?

0

8 Answers 8

143

The easiest way I've found to do this is to use the pom-root archetype to create the top-level pom and then repeatedly use archetype:generate to create each module individually. This will automatically add the modules to the root pom (aggregator) and set the root pom as the parent pom for each module (edit: apparently some archetypes can have a hard-coded parent, but it works for maven-archetype-quickstart).

Here's the breakdown:

  1. Create the top-level root:

    mvn archetype:generate \
    -DarchetypeGroupId=org.codehaus.mojo.archetypes \
    -DarchetypeArtifactId=pom-root \
    -DarchetypeVersion=RELEASE
    
  2. cd into your newly created root dir.

  3. For each module:

    mvn archetype:generate \
    -DarchetypeGroupId=org.apache.maven.archetypes \
    -DarchetypeArtifactId=maven-archetype-quickstart \
    -DarchetypeVersion=RELEASE
    

Note that -DarchetypeVersion=RELEASE above will automatically use the latest version of the archetype. You may want to add -DgroupId=... to each of those commands to avoid repeating yourself.

4
  • 4
    Using -Dfilter=pom-root will give you one choice without having to select the archetype group and artifact Id, and if you drop the archetypeVersion, it's just a few options to select from. Might be an easier path for those averse to typing. :-) Commented Apr 19, 2014 at 19:37
  • I thought copy/paste was the best for those averse to typing ;) so I've made the commands multiline for easier copy/paste.
    – Chris H.
    Commented Apr 21, 2014 at 21:27
  • 1
    True, but I was just adding for future-proofing. :-D Commented Apr 23, 2014 at 17:18
  • 8
    It's sad that I still come to SO to copy and paste this, rather than being able to simply rely on the mvn utility alone. Commented May 12, 2014 at 8:33
50

Here is a screencast on how you could do it with Intellij Idea. First, start a new project (File -> New Project), choose 'Maven Module':

enter image description here

Type in a name, click next, don't change anything else in the following steps, click finish.

Now in your pom.xml type <packaging> and enable auto-updates:

enter image description here

Type in modules:

enter image description here

Place your cursor at m1 and press Alt+Enter.

enter image description here

Module m1 will be automatically added to your project. Now you can do Alt+Enter for m2 and that's it.

enter image description here

You can also start by adding pom.xml for an existing module of your project: right click on it in the project tree, 'Add Framework Support...', choose 'Maven'. This will create a pom.xml.

2
  • Man, this was an amazing and helpful answer. can I follow you in twitter / stackoverflow answers etc?
    – Rinor
    Commented Aug 27, 2017 at 10:34
  • Ensure that you wrap those <module>m1</module> inside <modules> tag. I am new to pom file and ended up spending good amount of time on why alt + enter doesn't show any mentioned options. Commented May 4, 2018 at 13:50
22

mvn archetype:create has been deprecated in favor of mvn archetype:generate, so just the name changed. There is an archetype for multi-module projects in the official repositories, so running this command yields the (minimalist) result:

[axe@gromp test]$ mvn archetype:generate
..
<num>: remote -> pom-root (Root project archetype for creating multi module projects)
..
Choose a number: 109: <num>
.. 

[axe@gromp test]$ tree 
.
└── modules.test
    └── pom.xml

1 directory, 1 file

[axe@gromp test]$ cat modules.test/pom.xml 
<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>com.example</groupId>
  <artifactId>modules.test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>modules.test</name>
</project>

So, basically you will have to create the folder structure and module descriptors (pom.xml files) yourself. Using a simple shell script or batch file will easily do this, if you require it more than once.

4
  • FYI, the number to choose for pom-root has changed. Currently it is 449.
    – mwoodman
    Commented Apr 19, 2013 at 20:28
  • @mwoodman Thanks for the hint. I edited my answer to make it generic for future changes.
    – Axel Knauf
    Commented May 15, 2013 at 17:52
  • 2
    FYI the number changes depending on the repository you get it from.
    – user177800
    Commented Jul 31, 2013 at 23:37
  • 2
    Using -Dfilter=pom-root eliminates the need to document numbers, and should reduce the results to one. Commented Apr 19, 2014 at 20:10
9

I'm not sure if I understand your question correctly, but for creating a multi module project I normally use a simple pom (at the root level):

<?xml version="1.0" encoding="UTF-8"?>
<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>com.vijaykiran</groupId>
  <artifactId>myproject-parent</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>

  <modules>
    <module>m1</module>
    <module>m2</module>
  </modules>
</project>

This is probably the simplest multi-module parent pom that you can use. The project you want to create might already have an archetype which might help you in creating the structure. Although you can get help from an IDE to write the pom yourself, if there's an archetype available for the type of the project you want to build, it is normally easier to use that instead.

1
  • 6
    What I am saying is that memorizing the format of the POM file is difficult and writing it each time from scratch is a waste of time. As such, everytime I want to create a new project I copy and paste the content of the pom file and save it to my new project's pom.xml. I am looking for a more disciplined and efficient way to create multi module projects. For example an interactive command like archetype:generate that asks me whether I want to create a multi module project and if so what modules it does contain and then it creates the pom.xml files for me.
    – Sleepless Caffeine
    Commented Jun 12, 2011 at 14:30
6

If you are working with the eclipse IDE you should use the m2eclipse plug-in. This is one of the easiest way to create multi-module projects. You can add a module to every maven project by creating a 'Maven Module-Project' within eclipse. When doing this you have the possibility to select a parent project. The plug-in does everything, means it copies the new module to parent module and modifies the pom.xml file.

6

Same answer as Chris H. i just added groupId, artifactId and version options and disabled the interactive mode.

mvn archetype:generate -DarchetypeGroupId=org.codehaus.mojo.archetypes \
    -DarchetypeArtifactId=pom-root \
    -DarchetypeVersion=RELEASE \
    -DinteractiveMode=false \
    -DgroupId=com.mycompany.app \
    -DartifactId=my-app \
    -Dversion=1.0.0-SNAPSHOT  \
6

Consider a parent project bookmarks and 3 sub modules rest, security and model, referring to Spring docs. It doesn't have the dependencies as in the Spring doc, just the basic setup from multi-module point of view.

To create a parent maven project in non-interactive mode/ batch mode

mvn archetype:generate \
-DarchetypeGroupId=org.codehaus.mojo.archetypes \
-DarchetypeArtifactId=pom-root \
-DarchetypeVersion=RELEASE \
-DgroupId=bookmarks \
-DartifactId=bookmarks \
-Dversion=0.0.1-SNAPSHOT \
-DinteractiveMode=false

To create sub modules in non interactive mode/ batch mode.

cd into your newly created root dir. Referring to answer by @Chris.H

-Dpackage is the package structure. Here it is bookmarks. If not specified then it will consider the artifactId as default package

mvn archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=RELEASE \
-DgroupId=model \
-DartifactId=model \
-Dversion=0.0.1-SNAPSHOT \
-Dpackage=bookmarks \
-DinteractiveMode=false 

To create a new module in eclipse goto File->new->other->maven->maven module, this shows up immediately in eclipse workspace package explorer.

Or from cli, cd inside parent folder, here bookmarks and run the following, it will create the project and then you have to import into eclipse as a maven project, or can work from parent, here bookmarks project

mvn archetype:generate \
-DarchetypeGroupId=org.apache.maven.archetypes \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DarchetypeVersion=RELEASE \
-DgroupId=security \
-DartifactId=security \
-Dversion=0.0.1-SNAPSHOT \
-Dpackage=bookmarks \
-DinteractiveMode=false 
1

Simple 4 steps if you want to avoid xml copy paste.

  1. Create a 284(default) archetype project. Open the pom file created change the packaging from jar to pom

  2. Delete the src folder from the project - This is now the Parent project without src, since the packaging is pom. In the above folder create another new project(284 default). Change the packaging to war or ejb or ear. This becomes the sub module.

  3. Execute mvn eclipse:eclipse on each module. Now the project should be ready to be imported as a Eclipse Project.

  4. While importing the project, Eclipse would complain the below Project configuration is not up-to-date with pom.xml. Run Maven->Update Project or use Quick Fix. To avoid the above error right click and choose Quick Fix. This will update the POMS. Another way to avoid this error is by declaring the sub modules in the Parent pom

Reference the below link for more details. http://maven.apache.org/guides/introduction/introduction-to-the-pom.html#Example_1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.