Share
Explore

Here's a detailed step-by-step workflow for installing Apache Maven:

image.png
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:
image.png
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).
image.png
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`).
image.png
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.

image.png
image.png
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`:

image.png
image.png
- 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
image.png
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.


info

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.
image.png
Run the following command to create a new Maven project:
mvn archetype:generate -DgroupId=com.example.helloworld -DartifactId=HelloWorldMaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
image.png
Explanation:
-DgroupId: The group ID of the project (e.g., com.example.helloworld).
-DartifactId: The name of the project (e.g., HelloWorldMaven).
-DarchetypeArtifactId: The archetype used to generate a basic Maven project structure.
-DinteractiveMode=false: Prevents Maven from prompting for input, using default values.
Navigate to the newly created project directory:
bash
cd HelloWorldMaven

Step 2: Project Structure

Maven automatically creates a standard directory structure:

HelloWorldMaven/
├── src/
│ ├── main/
│ │ └── java/
│ │ └── com/example/helloworld/
│ │ └── App.java
│ └── test/
│ └── java/
│ └── com/example/helloworld/
│ └── AppTest.java
└── pom.xml

Step 3: Modify the Main Class

Open src/main/java/com/example/helloworld/App.java in a code editor.
Replace the contents with the following code to create a simple "Hello World" program:
mvn archetype:generate -DgroupId=com.example.helloworld -DartifactId=HelloWorldMaven -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Step 4: Understand and Review pom.xml

Open pom.xml in a code editor.
Ensure it has basic settings like project groupId, artifactId, and version:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.helloworld</groupId>
<artifactId>HelloWorldMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<name>Hello World Maven Project</name>
<dependencies>
<!-- Dependencies can be added here if needed -->
</dependencies>
</project>

s

Step 5: Compile the Project

Compile the Java code by running: remember to execute this in the Project directory - same directory where the /src folder is.
mvn compile
This command compiles the source code found in src/main/java.

Step 6: Package the Project

Create a .jar file to package all your file assets into one jar file by running:
mvn package
Maven will compile the code, run tests, and package it into a HelloWorldMaven-1.0-SNAPSHOT.jar file located in the target/ directory.

Step 7: Run the Program

Navigate to the target directory:
cd target

Run the JAR file using the java -jar command:
java -cp HelloWorldMaven-1.0-SNAPSHOT.jar com.example.helloworld.App
Explanation:
-cp: Puts the jar file on the classpath.
com.example.helloworld.App: Specifies the miain class to execute.
Expected Output:
Hello, World!

Step 8: Common Commands Recap

Compile the code:
mvn compile
Package the project:
mvn package
Run the packaged JAR file:
java -cp target/HelloWorldMaven-1.0-SNAPSHOT.jar com.example.helloworld.App

Step 9: Cleaning Up

Clean the project (remove target directory and compiled code):
mvn clean
This command deletes the target/ directory to ensure a clean build next time.

Conclusion

This workflow guides you through creating, compiling, building, and running a simple "Hello World" Java program using Maven.
It covers the core Maven commands and concepts, providing a practical introduction to Maven’s build and management capabilities.
We are now ready to use Maven to build our EJB Project.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.