Setting up Java with Maven in VS Code
Here's how to set up Java with Maven in Visual Studio Code:
1. Install Prerequisites
-
Install Java Development Kit (JDK)
- Download and install an LTS version (8, 11, 17, or 21) from Oracle or Adoptium
-
Install Maven
-
Download from Apache Maven website
- Extract the archive to a location on your computer
- Add Maven's
bindirectory to your system PATH - Verify installations in a terminal/command prompt:
java -version mvn -version
2. Install VS Code Extensions
- Open VS Code and go to Extensions (Ctrl+Shift+X)
- Install these extensions:
- "Extension Pack for Java" (includes Java essentials)
- "Maven for Java" (if not included in the pack)
3. Create a Maven Project
Option 1: Using VS Code Command Palette
- Press Ctrl+Shift+P to open the Command Palette
- Type "Maven: Create Maven Project"
- Select "Create from archetype" (recommended for beginners)
- Choose an archetype (e.g., maven-archetype-quickstart)
- Follow prompts to set groupId, artifactId, and version
Option 2: Using Terminal in VS Code
- Open a terminal in VS Code (Ctrl+`)
- Navigate to your desired directory
-
Run:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false -
Open the created folder in VS Code
4. Working with Your Maven Project
- The Maven Explorer view shows your project structure
- Edit
pom.xmlto manage dependencies and build configuration - Use the Maven extension menu for common tasks like clean, compile, and package
- Run Maven goals from Command Palette with "Maven: Execute Commands..."
5. Running Your Application
- Open the Java source file with your main method
- Click the "Run" button that appears above the main method
- Alternatively, use the Command Palette: "Java: Run Java"
Your project is now set up with Java and Maven in VS Code!
With Maven, you can create:
- Desktop applications (using JavaFX, Swing, or AWT)
- Web applications (using frameworks like Spring, Jakarta EE, etc.)
- Mobile applications (with frameworks like JavaFX Mobile or through Apache Cordova)
- Enterprise server applications
- Microservices
- Command-line tools and utilities
- Libraries and frameworks for other developers to use
- Android applications (though Gradle is more common for Android)