How to create a Multi Module Maven Project in VSCode

5.6k views Asked by At

I am planning to built multi module Maven Java Project as part of creating a mono repo. InteliJ IDE has an easy way to build Maven Modules inside an existing Maven Project. https://www.codejava.net/ides/intellij/create-multi-module-maven-project-intellij

(e.g. Right-click on the root project, and select New > Module:)

Is there a similar feature in VSCode or a plugin to create module in an existing Maven Project?

I have already installed the "Extension Pack for Java" plugin https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-java-pack as well as "Maven for Java" plugin https://marketplace.visualstudio.com/items?itemName=vscjava.vscode-maven

Interestingly VScode recognizes modules in a multi-module maven if you open one with it. it just doesn't have a way of adding new module afaik.

3

There are 3 answers

6
MingJie-MSFT On

Of course yes. You can creat the Multi-Module Maven project according to the first four steps in the article.

And right-click your root project and choose "install".

enter image description here

3
Cyrus On

After you've created a parent with <packaging>pom<packaging> tag, right-click on plus-sign on "Maven" tab to create a new module, and choose parent's working directory when prompted (In my case "demo"). (Btw, it adds some extra new lines into your parent's pom.xml, so I undo their reformatting, and add <modules><module>Your Name<module><modules> tags manually. Simply copy them before undoing something.)

Maven Tab

To see a new module under "Java Projects", and be able to run, or debug it (debugging in vscode), type "Java: Import Java Projects into Workspace" into "Command Palette" (Cntrl+Shift+P)

In parent's pom.xml should appear modules tag:

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>1.0</version>
  <packaging>pom</packaging>
    
  <modules>
    <module>demo-first</module>
  </modules>

In child's pom.xml should be generated these tags:

  <parent>
    <artifactId>demo</artifactId>
    <groupId>com.example</groupId>
    <version>1.0</version>
  </parent>

  <artifactId>demo-first</artifactId>
  <version>1.0</version>
0
CyberSensei On

I know this is a late response but I encountered this problem in my project aswell and it may help someone. My fastest solution is to prepare the parent module with

<packaging>pom</packaging>

Then from your command prompt, in your parent module folder you manually create a new maven project

mvn archetype:generate -DgroupId=org.yourcompany -DartifactId=YourChildModule -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

This will be your new child module, hope it helps.