How to create an empty multi-module Maven project?

Now I usually find a pom.xml file on the Internet that has a pom package and copies and pastes it to create my parent project. Then I used archetype:create inside the parent directory to create helper modules, but since then archetype:create become deprecated.

Any recommendations on creating new multi-module Maven projects?

+60
java maven
Jun 12 '11 at 11:08
source share
8 answers

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

Here's the breakdown:

  • Create a top level root:

     mvn archetype:generate \ -DarchetypeGroupId=org.codehaus.mojo.archetypes \ -DarchetypeArtifactId=pom-root \ -DarchetypeVersion=RELEASE 
  • cd to your newly created root directory.

  • 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 can add -DgroupId=... to each of these commands so as not to repeat yourself.

+97
Jul 31 '13 at 21:30
source share

Here is a screencast on how you could do this with Intellij Idea. First start a new project (File → New Project), select "Maven Module":

enter image description here

Enter a name, click "Next", do not change anything in the next steps, click "Finish".

Now in your pom.xml type <packaging> and enable automatic updating:

enter image description here

Enter the modules:

enter image description here

Place the cursor on m1 and press Alt + Enter .

enter image description here

The m1 module will be automatically added to your project. Now you can do Alt + Enter for m2 and it.

enter image description here

You can also start by adding pom.xml for the existing module of your project: right-click on it in the project tree, "Add support for frames ...", select "Maven". This will create pom.xml .

+33
Nov 23 '13 at 15:03
source share

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

 [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 have to create your own folder structure and module descriptors (pom.xml files). Using a simple shell script or batch file will easily do this if you need more than once.

+19
Jun 13 2018-11-11T00:
source share

I'm not sure if I understand your question correctly, but to create a project with several modules I usually 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 easiest parent pom module you can use. The project you want to create may already have an archetype that can help you create the structure. Although you can get help from the IDE to write pom itself, if there is an archetype for the type of project you want to build, it's usually easier to use this.

+9
Jun 12 '11 at 12:45
source share

If you are working with the Eclipse IDE, you should use the m2eclipse plugin . This is one of the easiest ways to create multi-module projects. You can add a module to each maven project by creating a "Maven Module-Project" in eclipse. In this case, you have the opportunity to choose a parent project. The plugin does everything, which means that it copies the new module to the parent module and modifies the pom.xml file.

+6
Jul 10 '11 at 18:11
source share

Same answer as Chris H. I just added groupId, artifactId and version options and turned off 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 \ 
+3
Oct 31 '15 at 3:42
source share

Simple 4 steps if you want to avoid copying xml.

  • Create an archetype project 284 (default). Open the created pom file, change the packaging from jar to pom

  • Delete the src folder from the project. Now this is a parent project without src since the packaging is pom. In the specified folder, create another new project (284 by default). Change the packaging to war or ejb or ear. This becomes an add-on module.

  • Run mvn eclipse: eclipse on each module. The project should now be ready for import as an Eclipse project.

  • When importing a project, Eclipse will complain below Pom.xml The project configuration is not updated. Launch Maven-> Update Project or use Quick Fix. To avoid the above error, right-click and select Quick Fix. This will update the POMS. Another way to avoid this error is to declare submodules in Parent pom

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

+1
Jun 25 '13 at 7:50
source share

Consider the parent bookmarks of the project and the rest of the 3 modules, security and model, referring to Spring docs . It has no dependencies, as in the Spring document, only basic setup from a multi-module point of view.

To create parent maven project in non-interactive / 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 

Create auxiliary modules in non-interactive mode / batch mode.

cd to your newly created root directory. Referring to the answer @ Chris.H

-Dpackage is the package structure. Here are the bookmarks. If not specified, it will consider the artifactId file as the 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, it immediately appears in the package explorer of the eclipse workspace.

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

 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
Jan 15 '17 at 13:24
source share



All Articles