YouTube Video
Prerequisites
Before creating a Maven project, ensure the following:
Java Development Kit (JDK)
- Install the latest JDK (e.g., JDK 11 or higher).
- Verify installation by running:
java -version
Apache Maven
- Download and install Maven from https://maven.apache.org.
- Verify installation by running:
mvn -version
Visual Studio Code
- Install VS Code from https://code.visualstudio.com.
Java and Maven Extensions
- Open VS Code and install the following extensions from the Extensions Marketplace:
- Java Extension Pack
- Maven for Java
- Open VS Code and install the following extensions from the Extensions Marketplace:
Step 1: Create a Maven Project
Option 1: Using Maven Archetype in VS Code
Open the Command Palette
PressCtrl+Shift+P
to open the Command Palette.Generate Maven Project
Type and select Maven: Create Maven Project.Select Archetype
- Choose an archetype to define your project structure.
- For a basic Java application, select
maven-archetype-quickstart
.
Configure Project
- Enter the Group ID (e.g.,
com.example
). - Enter the Artifact ID (e.g.,
my-maven-project
).
- Enter the Group ID (e.g.,
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
- Open a terminal in VS Code (`Ctrl+```) and navigate to your desired folder.
- Run the following Maven command:
mvn archetype:generate -DgroupId=com.example -DartifactId=my-maven-project -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
- 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
- In VS Code, click File > Open Folder.
- Navigate to and select the folder containing your Maven project.
- 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
Open the
App.java
file located insrc/main/java/com/example/
.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!"); } }
Save the file (
Ctrl+S
).
Step 5: Run the Maven Project
Option 1: Using the Run Button
- Open the
App.java
file. - Click the Run button (green triangle) at the top-right corner of the editor.
- The output will appear in the Terminal or Debug Console.
Option 2: Using Maven Commands
- Open the terminal in VS Code.
- Navigate to the project root folder (where the
pom.xml
file is located). - Compile the project:
mvn compile
- Run the application:
mvn exec:java -Dexec.mainClass="com.example.App"
- 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:
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>
Save the file, and Maven will automatically download and include the dependency.
Step 7: Debug Your Maven Project (Optional)
- Open the Run and Debug view by clicking the bug icon in the left sidebar or pressing
Ctrl+Shift+D
. - Click Create a launch.json file and select Java as the environment.
- Set breakpoints by clicking to the left of the line numbers in the code editor.
- 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 thepom.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
Post a Comment
Leave Comment