How to create an empty multi module Maven project?

Solution 1:

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.

Solution 2:

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.

Solution 3:

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.

Solution 4:

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.