lab2

 Experiment

2:

Working

with

Also integrates seamlessly with

CI/CD tools and is often chosen for

its faster build times and flexibility

in pipeline scripting.

Maven: Creating a Maven Project,

Understanding the POM File, Dependency Management and Plugins

1. Introduction to Maven

Maven is a powerful build automation and project management tool primarily used for Java

projects. It simplifies the build process by:

• Enforcing a standard project structure (convention over configuration).

• Managing dependencies automatically by downloading them from remote repositories

(e.g., Maven Central).

• Defining a clear build lifecycle (compile, test, package, install, deploy).

• Allowing the integration of various plugins to extend functionality (e.g., testing,

reporting).

2. Creating a Maven Project

Step-by-Step Process

Step 1: Open Your Terminal

Make sure you have Maven installed (refer to Experiment 1). Open your terminal on your

Ubuntu system.

Step 2: Use Maven Archetype to Generate a New Project

Maven comes with a set of archetypes that provide you with a standard project template. Use

the following command to create a new Maven project:

mvn archetype:generate -DgroupId=com.example -DartifactId=MyMavenApp -

DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

• groupId: Uniquely identifies your project’s group (like a package name).

• artifactId: The name of your project (the resulting artifact).

• maven-archetype-quickstart: A simple archetype that sets up a basic Java project with

a sample unit test.

• -DinteractiveMode=false: Runs the command in non-interactive mode, using the

provided parameters.

Step 3: Navigate to Your Project Directory

Once the command completes successfully, change your directory to the newly created project:

cd MyMavenApp

3. Maven Project Layout and Components

After generating the project, you will notice the following standard Maven directory structure:

MyMavenApp/

├── pom.xml

└── src

├── main

│ └── java

└── com

└── test

└── example

└── App.java

└── java

└── com

└── example

└── AppTest.java

Explanation of Key Components

• pom.xml:

The Project Object Model (POM) file is the core of any Maven project. It contains

configuration details such as project coordinates (groupId, artifactId, version),

dependencies, plugins, and build settings.

• src/main/java:

This directory holds the source code of your application. In our example, the package

structure com.example is created, and you have an App.java file.

• src/test/java:

This directory is for your test cases. The default example includes a basic test class,

AppTest.java.

4. Understanding the POM File (pom.xml)

The pom.xml file is written in XML and is essential to how Maven operates. It includes several

key sections:

Basic Structure of a POM 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>

<!-- Project Coordinates -->

<groupId>com.example</groupId>

<artifactId>MyMavenApp</artifactId>

<version>1.0-SNAPSHOT</version>

<!-- Properties: Customize Java version or plugin versions -->

<properties>

<maven.compiler.source>11</maven.compiler.source>

<maven.compiler.target>11</maven.compiler.target>

</properties>

<!-- Dependencies -->

<dependencies>

<!-- Example: JUnit dependency for testing -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version>

<scope>test</scope>

</dependency>

</dependencies>

<!-- Build: Configuring plugins and build settings -->

<build>

<plugins>

<!-- Example: Maven Compiler Plugin to compile Java code -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>1.6</source>

<target>1.6</target>

</configuration>

</plugin>

<!-- Example: Maven Surefire Plugin to run tests -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.2</version>

</plugin>

</plugins>

</build>

</project>

Key Elements Explained

• Project Coordinates:

o <groupId>: Acts like a namespace, usually following the reverse domain name

convention.

o <artifactId>: The name of the project.

o <version>: The current version of the project.

• Properties:

o Used to define values that can be referenced elsewhere in the POM (e.g., Java

source and target versions).

• Dependencies:

o Dependency Management: Maven downloads and manages external libraries.

In the example, JUnit is added for testing.

o Scope: Determines when a dependency is used (e.g., compile, test, runtime).

• Build and Plugins:

o Maven Compiler Plugin: Ensures that your Java code is compiled with the

specified Java version.

o Maven Surefire Plugin: Executes unit tests during the build process.

5. Dependency Management with Maven

How Maven Manages Dependencies

• Automatic Download: When you specify a dependency in the <dependencies> section,

Maven automatically downloads the library from a remote repository (usually Maven

Central).

• Transitive Dependencies: Maven also resolves dependencies required by your

dependencies.

• Version Control: You can specify precise versions for each dependency, ensuring

consistency across builds.

Example Dependency

To add a dependency for JUnit, include the following snippet in your <dependencies> section:

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version>

<scope>test</scope>

</dependency>

• groupId, artifactId, version: These three elements uniquely identify the dependency.

• scope: The test scope ensures that this dependency is only available during the test

phase and not included in the final artifact.

6. Maven Plugins: Extending Maven Functionality

Plugins are key to Maven’s flexibility, adding tasks to your build process.

Example 1: Maven Compiler Plugin

This plugin is used to compile your Java source code.

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>11</source>

<target>11</target>

</configuration>

</plugin>

• Configuration: Specifies that the project should be compiled using Java 11.

Example 2: Maven Surefire Plugin

This plugin runs your unit tests during the test phase.

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-surefire-plugin</artifactId>

<version>2.22.2</version>

</plugin>

• Purpose: Automatically detects and runs tests in the src/test/java directory.

7. Building and Testing Your Maven Project

Once your project is set up and your pom.xml is defined, you can use Maven commands to

build and test your application.

Common Maven Commands

• Compile the Project: mvn compile

• Run Unit Tests: mvn test

• Package the Application:

mvn package

This command compiles, tests, and packages your code into a JAR file located in the

target directory. Screenshot Tip: Capture the listing of the target directory showing the

JAR file.

• Clean the Project:

mvn clean

This removes any files generated by previous builds.

Comments

Popular posts from this blog

lab1

lab4