How to Create a Maven Project in Visual Studio Code (VS Code) IDE

Maven is a powerful build automation tool used primarily for Java projects. It simplifies project management, dependency handling, and build processes. Combining Maven with Visual Studio Code (VS Code) provides a lightweight, efficient environment for Java development. This guide will walk you through the step-by-step process of creating a Maven project in VS Code.

YouTube Video

Prerequisites

Before creating a Maven project, ensure the following:

  1. Java Development Kit (JDK)

    • Install the latest JDK (e.g., JDK 11 or higher).
    • Verify installation by running:
      java -version
      
  2. Apache Maven

  3. Visual Studio Code

  4. Java and Maven Extensions

    • Open VS Code and install the following extensions from the Extensions Marketplace:
      • Java Extension Pack
      • Maven for Java

Step 1: Create a Maven Project

Option 1: Using Maven Archetype in VS Code

  1. Open the Command Palette
    Press Ctrl+Shift+P to open the Command Palette.

  2. Generate Maven Project
    Type and select Maven: Create Maven Project.

  3. Select Archetype

    • Choose an archetype to define your project structure.
    • For a basic Java application, select maven-archetype-quickstart.
  4. Configure Project

    • Enter the Group ID (e.g., com.example).
    • Enter the Artifact ID (e.g., my-maven-project).
  5. Choose a Location
    Select a folder where you want to save the project. Maven will create the project structure in this location.

Option 2: Using Maven Command in Terminal

  1. Open a terminal in VS Code (`Ctrl+```) and navigate to your desired folder.
  2. Run the following Maven command:
    mvn archetype:generate -DgroupId=com.example -DartifactId=my-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    
  3. Maven will generate a new project with the following structure:
    my-maven-project/
    ├── src/
    │   ├── main/
    │   │   └── java/
    │   │       └── com/example/App.java
    │   ├── test/
    │       └── java/
    │           └── com/example/AppTest.java
    ├── pom.xml

Step 2: Open the Maven Project in VS Code

  1. In VS Code, click 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 3: Explore the Project Structure

  • src/main/java: Contains your application source code.
  • src/test/java: Contains test cases for your application.
  • pom.xml: Maven’s configuration file for managing dependencies, plugins, and project metadata.

Step 4: Write Your First Java Program

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

  2. Modify the code as follows:

    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 5: Run the Maven Project

Option 1: Using the Run Button

  1. Open the App.java file.
  2. Click the Run button (green triangle) at the top-right corner of the editor.
  3. The output will appear in the Terminal or Debug Console.

Option 2: Using Maven Commands

  1. Open the terminal in VS Code.
  2. Navigate to the project root folder (where the pom.xml file is located).
  3. Compile the project:
    mvn compile
    
  4. Run the application:
    mvn exec:java -Dexec.mainClass="com.example.App"
    
  5. The output should display:
    Hello, Maven with VS Code!

Step 6: Add Dependencies to the Project

To add a dependency, modify the pom.xml file:

  1. Open pom.xml and add the dependency under the <dependencies> section. For example, to add the Gson library:

    <dependencies>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.9</version>
        </dependency>
    </dependencies>
    
  2. Save the file, and Maven will automatically download and include the dependency.

Step 7: Debug Your Maven Project (Optional)

  1. Open the Run and Debug view by clicking the bug icon in the left sidebar or pressing Ctrl+Shift+D.
  2. Click Create a launch.json file and select Java as the environment.
  3. Set breakpoints by clicking to the left of the line numbers in the code editor.
  4. Click Start Debugging to run the application in debug mode.

Troubleshooting

  • Maven Not Recognized: Ensure Maven is installed and added to your system PATH.
  • Dependency Issues: Check for typos in the <dependencies> section of the pom.xml file.
  • Slow IntelliSense: Restart VS Code or disable unnecessary extensions.

Conclusion

You’ve successfully created and run a Maven project in Visual Studio Code. VS Code, combined with Maven, provides a powerful and efficient environment for Java development. With features like dependency management, testing support, and debugging tools, Maven projects are perfect for both small and large-scale applications. Happy coding!

Comments