Maven Multi Modules on Visual Studio Code

Visual Studio Code has many plugin in supporting various programming languages. One of them is Java. One of pupular plugin of VSCode which supports Java is Extension Pack for Java. In this post will show you how to create Java project using maven by utilizing the VSCode plugin.

Creating Java (Maven) Project on VSCode

  1. Makesure we have Java SDK and VSCode installed in our system.
  2. Install VSCode plugin Extension Pack for Java.

  3. To create new project, select Create Java Project. Then select type of project you want. In this post we will use maven based.

  4. Fill out the maven project setting.

  5. VSCode will generate a java project for you.

    While generating the Java project, we will be asked for the version. If we agree with default version 1.0-SNAPSHOT, we just need to press Enter. Then follow the next question in the terminal tab.

  6. Finally the Java project generated and displayed in the File explorer of VSCode.

Setup Maven Multi Modules Projects

To setup the previous Maven project as Maven Multi Modules projects, let’s do the following steps.

  1. Delete the src and target folder and their contents on the provious project. Then Edit the root pom.xml as below.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <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>io.github.neutrofoton.multimodule</groupId>
    <artifactId>multimodule</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    
    </project>
    
  2. Run maven install to ensure the pom.xml is valid.

  3. If no error on step #2, then add a new maven project as a new maven module. To do that, right click on the VSCode Explorer > select Create Maven Project.

  4. Fill out the group and artifact as we did previously.

    We use the sama group id as parent/root pom.xml. But a new name for artifact, in this case we named it core module.

  5. Select the destionation folder of the module iside the root/parent project.

  6. Create another module called app by repeating step #3 to #5.

  7. The pom.xml should be updated as follow.

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <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>io.github.neutrofoton.multimodule</groupId>
    <artifactId>multimodule</artifactId>
    <version>1.0-SNAPSHOT</version>
    
    <packaging>pom</packaging>
    
    <modules>
       <module>core</module>
       <module>app</module>
    </modules>
       
    </project>
    

    And the project structure should be like this

  8. To ensure the project well defined, we can run maven install on the root pom.xml

Tips

To clear the plugin cache of the Java plugin, we can run
(MacOS) : CMD + Shift + P then type Java: Clean Java language Server Workspace