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:

mvn -version

If not installed, follow Exercise 1 for installation steps.

Step 2: Create a Maven Project Using the Command Line

Open Command Prompt:

mvn archetype:generate -DgroupId=com.example -DartifactId=myapp -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

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

cd myapp

Step 4: Verify the Project Structure

Maven creates a standard folder structure like this:

myapp
│── src │ ├── main │ │ └── java │ │ └── com.example │ │ └── App.java │ ├── test │ │ └── java │ │ └── com.example │ │ └── AppTest.java │── pom.xml │── README.md │── .gitignore
  • 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:


Note: Used VS Code Text Editor. Make 'myapp' as your project folder

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

<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</groupId> <artifactId>myapp</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- Example Dependency: JUnit for Testing --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!-- Maven Compiler Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>

Key Sections of pom.xml

  1. <groupId> - Unique identifier for the project (e.g., com.example).
  2. <artifactId> - Project name (e.g., myapp).
  3. <version> - Version of the project (e.g., 1.0-SNAPSHOT).
  4. <dependencies> - Lists external libraries required for the project.
  5. <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:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.7.0</version> </dependency>

After adding dependencies, run:

mvn clean install

This downloads and installs required JAR files.

Output:

  • All downloaded dependencies are stored in .m2/repository.
  • Output:


  • Use mvn dependency:tree to list all dependencies.
  • Output:


  • You can open .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

    PluginPurpose
    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.

    Step1: Create Maven Project (use the above procedure)

    Step2: Configuring Maven Plugins in pom.xml

    <dependencies>
          <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>5.9.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-engine</artifactId>
                <version>5.9.0</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.7.0</version>
            </dependency>
    </dependencies>

    <build> <plugins> <!-- Compiler Plugin --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <!-- Surefire Plugin for Unit Testing --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M7</version> </plugin> <!-- JAR Plugin with Manifest Configuration --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>3.2.0</version> <configuration> <archive> <manifest> <mainClass>com.example.App</mainClass> </manifest> </archive> </configuration> </plugin> </plugins> </build>


    This 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.

    Step3: Writing a Sample Java Program (App.java)

    package com.example; public class App { public static String greet() { return "Hello, DevOps!"; } public static void main(String[] args) { System.out.println(greet()); } }

    Step4: Writing a Unit Test (AppTest.java)
    Modify the AppTest.java file inside src/test/java/com/example/ to test the greet method:

    package com.example; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class AppTest { @Test public void testGreet() { assertEquals("Hello, DevOps!", App.greet()); } }

    Here:
    ✅ We use JUnit to test the greet() method.
    ✅ The assertEquals() method checks if the output matches "Hello, DevOps!".

    Step5: Running Maven Commands

    a. mvn compile (compile the code)

    If successful, you will see

    [INFO] Compiling 1 source file to target/classes

    b. mvn test (run unit tests)

    Expected output (if the test passes)

    [INFO] Running com.example.AppTest [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.01 sec [INFO] BUILD SUCCESS

    If the test fails, check the assertion inside AppTest.java.

    c. mvn package (package the application)

    This will generate the output

    [INFO] Building jar: /path-to-project/myapp/target/myapp-1.0-SNAPSHOT.jar

    The JAR file will be inside the target/ folder.

    d. java -jar target/myapp-1.0-SNAPSHOT.jar (run the jar file)

    Output

    Hello, DevOps!

    -:END:-


    Comments