Creating a Maven Java project in Visual Studio Code

Step 1: Install Required Software

  1. Java Development Kit (JDK): Download and install the latest JDK (e.g., JDK 11 or higher).

  2. Apache Maven: Download and install Maven from the Maven website.

  3. Visual Studio Code: Download and install VS Code from the VS Code website.

Step 2: Install Java and Maven Extensions

  1. Open VS Code.

  2. Go to the Extensions Marketplace by clicking on the Extensions icon in the Activity Bar on the side of the window.

  3. Search for "Java Extension Pack" and install it.

  4. Search for "Maven for Java" and install it.

Step 3: Create a Maven Project

  1. Using the Command Palette:

    • Press Ctrl+Shift+P to open the Command Palette.

    • Type "Create Maven Project" and select it.

    • Choose an archetype (e.g., maven-archetype-quickstart for a basic Java application).

    • Enter the Group ID, Artifact ID, and other details as prompted.

    • Select a location to save your project.

  2. Using the Maven Explorer:

    • Open the Maven Explorer in VS Code.

    • Click the + Create Maven Project button.

    • Follow the same steps as above to configure your project.

Step 4: Open the Project in VS Code

  1. Click on File > Open Folder.

  2. Navigate to and select the folder containing your Maven project.

  3. VS Code will detect the pom.xml file and load the project as a Maven project.

Step 5: Write Your First Java Program

  1. Open the App.java file located in src/main/java/com/example/.

  2. Modify the code as needed. For example:

    java

    package com.example;
    
    public class App {
        public static void main(String[] args) {
            System.out.println("Hello, Maven with VS Code!");
        }
    }
  3. Save the file (Ctrl+S).

Step 6: Run the Maven Project

  1. Open the App.java file.

  2. Click the Run button (green triangle) at the top-right corner of the editor.

  3. Alternatively, you can run Maven goals by right-clicking the project in the Explorer and selecting the desired goal.

Last updated