DevOps Tool Series: Maven

Khemnath chauhan
3 min readJan 1, 2024

--

Maven is a powerful project management and build automation tool, primarily used for Java projects, though it can be adapted for other languages and platforms. It simplifies the process of building, managing, and distributing software projects.

KEY CONCEPTS:

Project Object Model (POM):
Maven uses a POM, an XML file that contains project configuration information and the dependencies needed for the project. This file describes how the software should be built, the dependencies, plugins, and other configurations.

Dependency Management:
Maven handles dependencies by managing libraries and external components, ensuring that the required dependencies are downloaded and used during the build process.

Plugins:
Maven relies on plugins to carry out various tasks during the build lifecycle. Plugins can perform tasks such as compiling code, running tests, packaging artifacts, and more.

Build Lifecycle:
Maven defines a set of build phases (e.g., compile, test, package) organized into lifecycles. Each phase executes specific tasks, and the build progresses through these phases sequentially.

USE CASES:

Building the Project:

  • Maven automates the compilation, testing, and packaging of the project. Developers can use a single command to build the entire project.

Dependency Management:

  • Maven simplifies the inclusion of external libraries (dependencies) in the project. Dependencies are specified in the POM, and Maven takes care of downloading and managing them.

Testing:

  • Maven supports the execution of tests during the build process. It ensures that tests are run and reports any failures.

Packaging:

  • Maven can package the project into various formats, such as JAR, WAR, or even create distribution packages.

COMMON MAVEN COMMANDS:

  1. mvn clean: Cleans the target directory, removing the build artifacts.
  2. mvn compile: Compiles the source code of the project.
  3. mvn test: Runs the tests for the project
  4. mvn package: Packages the compiled code into a distributable format ( e.g JAR, WAR )
  5. mvn install: Installs the packages artifact into the local repository for use in other projects.
  6. mvn clean install:

MAVEN INSTALLATION:

Below is the command to install maven in Ubuntu server:

sudo apt update -y
sudo apt install maven -y

# Check the installation.
mvn -version

Check the Installation:

Sample pom.xml and usage:

A pom.xml file is a configuration file used in Apache Maven projects. It defines the project's configuration and dependencies. Below is a simple example of a pom.xml file along with explanations of some of its key elements.

<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">

<!-- Project coordinates -->
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>my-app</artifactId>
<version>1.0.0</version>

<!-- Project metadata -->
<name>My App</name>
<description>My Maven-based Java application</description>

<!-- Project properties -->
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<!-- Project dependencies -->
<dependencies>
<!-- Example dependency: JUnit for testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>

<!-- Add other dependencies here -->
</dependencies>

<!-- Build configuration -->
<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>

<!-- Maven JAR Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</build>
</project>

--

--

Khemnath chauhan
Khemnath chauhan

No responses yet