Here's a detailed step-by-step workflow for installing Apache Maven:
Step-by-Step Workflow for Installing Apache Maven
Step 1: Check Java Installation
Before installing Maven, ensure that Java is installed and properly configured on your system, as Maven requires Java to run.
1. Verify Java Installation:
Open Command Prompt or Terminal and type:
```bash
java -version
```
- You should see output similar to:
openjdk version "17.0.11" 2024-04-16
OpenJDK Runtime Environment (build 17.0.11+9)
OpenJDK 64-Bit Server VM (build 17.0.11+9, mixed mode, sharing)
```
2. Install Java (if not installed):
- Download Java from the [AdoptOpenJDK](https://adoptopenjdk.net/) or [Oracle Java](https://www.oracle.com/java/technologies/javase-downloads.html) website.
- Follow the installation prompts for your operating system.
Step 2: Download Apache Maven
1. Go to the Official Maven Website:
- Visit the [Apache Maven download page](https://maven.apache.org/download.cgi).
2. Select and Download:
- Choose the binary archive (`.zip` for Windows or `.tar.gz` for macOS/Linux).
- Click on the download link and save the file to a preferred directory (e.g., `Downloads`).
Step 3: Extract the Maven Archive
—-. Extract the Archive:
- On Windows: Right-click the `.zip` file and select *Extract All*, or use a tool like 7-Zip to extract it.
- On macOS/Linux: Use the command:
```bash
tar -xzvf apache-maven-x.x.x-bin.tar.gz
```
Replace `x.x.x` with the actual version number (e.g., `3.8.6`).
—- Move the Extracted Folder:
- Move the extracted folder to a preferred location such as `C:\Program Files\Apache\Maven` on Windows or `/usr/local/apache-maven` on macOS/Linux.
Step 4: Set Up Environment Variables
1. Configure `MAVEN_HOME`:
- Windows:
1. Go to *Control Panel* > *System* > *Advanced system settings* > *Environment Variables*.
2. Click *New* under *System variables*.
3. Enter `MAVEN_HOME` as the variable name.
4. Enter the path to your Maven directory (e.g., `C:\Program Files\Apache\Maven\apache-maven-x.x.x`) as the variable value.
- macOS/Linux:
Edit your `~/.bash_profile`, `~/.bashrc`, or `~/.zshrc` file:
```bash
export MAVEN_HOME=/usr/local/apache-maven/apache-maven-x.x.x
export PATH=$MAVEN_HOME/bin:$PATH
```
Save the file and run `source ~/.bash_profile` (or the respective profile file) to apply changes.
2. Add Maven to `PATH`:
- Windows:
1. Under *Environment Variables*, find the `Path` variable in *System variables* and click *Edit*.
2. Click *New* and add `C:\Program Files\Apache\Maven\apache-maven-x.x.x\bin`.
- macOS/Linux:
Ensure the `export PATH=$MAVEN_HOME/bin:$PATH` line is in your profile file as mentioned above.
Step 5: Verify Maven Installation
1. Open Command Prompt or Terminal:
Run the following command:
```bash
mvn -v
2. Check Output:
You should see output similar to:
```
Apache Maven 3.x.x (Replace with actual version)
Maven home: C:\Program Files\Apache\Maven\apache-maven-x.x.x
Java version: 17.0.11, vendor: Eclipse Adoptium, runtime: C:\Program Files\Eclipse Adoptium\jdk-17.x.x
Default locale: en_US, platform encoding: UTF-8
OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"
```
Step 6: Common Troubleshooting Tips
1. Incorrect PATH Configuration:
- Double-check that `MAVEN_HOME\bin` is correctly added to the `PATH` variable.
2. Restart Required:
- On Windows, sometimes a restart or opening a new Command Prompt window is necessary for changes to take effect.
3. Permissions:
- Ensure that the extracted Maven folder has the necessary read and execute permissions on macOS/Linux.
Conclusion
Installing Maven is essential for Java development, especially for managing dependencies and automating build processes. Once set up, students can create Maven projects, manage dependencies, and build Java Enterprise (Jakarta EE) applications seamlessly.
Let’s do a workflow to compile, build and run a simple Hello World workflow with maven
Here's a detailed workflow to compile, build, and run a simple "Hello World" Java project using Maven.
This guide will take you from setting up a basic Maven project to running your Java program.
Step 1: Create a New Maven Project
Open Command Prompt or Terminal and navigate to the directory where you want to create the project.
Run the following command to create a new Maven project: