Exercise-2
2. Working with Maven: Creating a Maven Project, Understanding the POM File, Dependency Management and Plugins
What is Maven?
Apache Maven is a build automation and project management tool that simplifies software development by managing dependencies, compiling code, running tests, and packaging applications. It follows a convention-over-configuration approach and is widely used in Java-based projects.
Why Use Maven?
- Standardized project structure.
- Automatic dependency management using
pom.xml. - Integration with CI/CD pipelines (Jenkins, GitHub Actions, Azure DevOps).
- Supports various build lifecycle phases (compile, test, package, deploy).
Creating a Maven Project
Step 1: Install Maven (If Not Installed)
Check if Maven is installed using:
If not installed, follow Exercise 1 for installation steps.
Step 2: Create a Maven Project Using the Command Line
Open Command Prompt:
Explanation:
-DgroupId=com.example→ Defines the package structure (similar to a namespace).-DartifactId=myapp→ The project name and the name of the final artifact (JAR file).-DarchetypeArtifactId=maven-archetype-quickstart→ Uses a pre-defined template for Java projects.-DinteractiveMode=false→ Skips manual inputs and creates the project automatically.
Output:
Step 3: Navigate to the Project Folder
Step 4: Verify the Project Structure
Maven creates a standard folder structure like this:
src/main/java→ Contains the main application code.src/test/java→ Contains test cases for the application.pom.xml→ The core configuration file for Maven.
Output:
Understanding the POM File (pom.xml)
The Project Object Model (POM) file is the heart of a Maven project. It defines project metadata, dependencies, plugins, and build configurations.
Example pom.xml File
Key Sections of pom.xml
<groupId>- Unique identifier for the project (e.g.,com.example).<artifactId>- Project name (e.g.,myapp).<version>- Version of the project (e.g.,1.0-SNAPSHOT).<dependencies>- Lists external libraries required for the project.<build>- Specifies plugins for compiling, packaging, and testing.
Managing Dependencies in Maven
Maven automatically downloads required libraries from the Maven Central Repository.
Adding a Dependency
To use Spring Boot, add the following inside the <dependencies> section:
After adding dependencies, run:
This downloads and installs required JAR files.
Output:
.m2/repository.Output:
mvn dependency:tree to list all dependencies.Output:
.jar files using an archive tool or the jar command.Output:
Using Maven Plugins
Maven plugins automate tasks like compiling code, running tests, and packaging the application.
Common Maven Plugins
| Plugin | Purpose |
|---|---|
| maven-compiler-plugin | Compiles Java code |
| maven-surefire-plugin | Runs unit tests |
| maven-jar-plugin | Packages the project as a JAR file |
Practically Using Maven Plugins in a Project
To compile code, run unit tests, and package the application using Maven plugins, follow this step-by-step guide.
pom.xmlThis configures Maven to:
✅ Compile Java code using the maven-compiler-plugin.
✅ Run unit tests using the maven-surefire-plugin.
✅ Package the application into a JAR file using the maven-jar-plugin.App.java)AppTest.java)AppTest.java file inside src/test/java/com/example/ to test the greet method:✅ We use JUnit to test the
greet() method.✅ The
assertEquals() method checks if the output matches "Hello, DevOps!".a. mvn compile (compile the code)
If successful, you will see
b. mvn test (run unit tests)
Expected output (if the test passes)
If the test fails, check the assertion inside AppTest.java.
c. mvn package (package the application)
This will generate the output
The JAR file will be inside the target/ folder.
d. java -jar target/myapp-1.0-SNAPSHOT.jar (run the jar file)
Output

This guide on creating a Maven project is very clear and practical. I especially appreciate the explanation of the DevOps approach to dependency management—it reminds me how much easier CI/CD pipelines become when the pom.xml is structured properly.
ReplyDeleteThis breakdown of Maven commands and plugins is very clear. I like how you explained the role of the POM file—it made it easier for me to debug dependency issues in my own projects. I’m curious how you see tools like Maven evolving alongside trends in AI machine learning and automated build optimization.
ReplyDelete