Java – How to create an empty multi module Maven project

javamaven

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?

Best Answer

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.