DEV Community

Cover image for Mastering Maven: Beyond Build Management
MINI JAIN
MINI JAIN

Posted on

Mastering Maven: Beyond Build Management

Maven is widely known as a powerful build automation tool, but it’s much more than that. It’s a comprehensive project management tool that simplifies the entire build process, dependency management, and documentation generation. In this post, we will explore various aspects of Maven and understand its capabilities in detail.

Key Features of Maven
1.Build Generation
2.Dependency Management
3.Documentation

When you execute commands like mvn build or mvn deploy, Maven looks into the pom.xml file, which contains all the configurations, and acts accordingly. Let’s dive deeper into the pom.xml structure and its significance.

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

    <!-- Basic project information -->
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- Properties -->
    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <spring.version>5.2.8.RELEASE</spring.version>
    </properties>

    <!-- Dependencies -->
    <dependencies>
        <!-- Spring Core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring Context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- JUnit for testing -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- Build configuration -->
    <build>
        <plugins>
            <!-- Compiler plugin -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>${maven.compiler.source}</source>
                    <target>${maven.compiler.target}</target>
                </configuration>
            </plugin>
            <!-- Surefire plugin for running tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
            </plugin>
        </plugins>
    </build>

    <!-- Repositories -->
    <repositories>
        <repository>
            <id>central</id>
            <url>https://repo.maven.apache.org/maven2</url>
        </repository>
    </repositories>

    <!-- Distribution management for deployment -->
    <distributionManagement>
        <repository>
            <id>releases</id>
            <url>http://repo.mycompany.com/releases</url>
        </repository>
        <snapshotRepository>
            <id>snapshots</id>
            <url>http://repo.mycompany.com/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

</project>

Enter fullscreen mode Exit fullscreen mode

Lets decode th POM file:

**The pom.xml's file follows a specific XML schema (defined in xsi:schemaLocation:)that ensures it adheres to a correct structure, which Maven verifies.The schema here refers to defininfg the pom,For example When I am adding dependencies, I should have groupid, artifactId,version. I cannot add something xyz as key.These type of rules defined are taken from xsi:schemaLocation

Key Elements:

  • Parent POM:
    Each POM file in Spring Boot has a parent POM. If no parent is defined, the super POM becomes the parent. Every Pom is a subset of Super pom.

  • GroupId, ArtifactId, and Version:
    These elements uniquely identify a project in Maven Central.Lets say If I have deployed my project in maven central,these 3 properties will uniquely identify my project.

<groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
Enter fullscreen mode Exit fullscreen mode
  • Properties: Define key-value pairs that can be referenced throughout the pom.xml.
<properties>
    <java.version>1.8</java.version>
</properties>

Enter fullscreen mode Exit fullscreen mode

I can use ${java.version} as variable throughout in the pom.

  • Repositories: Specify where to download dependencies, typically from Maven Central.The url value defines where to get the dependencies and start downloading.
<repositories>
    <repository>
        <id>central</id>
        <url>https://repo.maven.apache.org/maven2</url>
    </repository>
</repositories>
Enter fullscreen mode Exit fullscreen mode

Dependencies: List the project's dependencies.

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>2.3.4.RELEASE</version>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode
  • Build Configuration: Defines the build process and phases. For this to we need to understand the build lifecycle of maven.

Maven Build Life cycle

Maven follows a specific build lifecycle that consists of several phases:

  1. Validate: Validates the project structure.When we want to add certain standards and conventions before building and packaging,We can define a plugin for validations. The command for triger validate is: mvn validate The Maven Checkstyle Plugin helps you check if your Java source code adheres to a specified coding standard.
<project>
    <!-- other configurations -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-checkstyle-plugin</artifactId>
                <version>3.1.1</version>
                <executions>
                    <execution>
                        <id>checkstyle-validation</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                       {% embed  <phase>validate</phase> %}
                    </execution>
                </executions>
                <configuration>
                    <configLocation>config/checkstyle/checkstyle.xml</configLocation>
                    <encoding>UTF-8</encoding>
                    <consoleOutput>true</consoleOutput>
                    <failsOnError>true</failsOnViolation>true</failsOnWarning>false</failsOnSuppressedViolations>false</failsOnNoViolations>false
                    <linkXRef>false</linkXRef>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
Enter fullscreen mode Exit fullscreen mode

Here I am adding checklist validation in validate phase.It will validate all the goals written in checklist validate.

  1. Compile:
    Converts Java code to bytecode (.class files) and places them in the target/classes folder. The command will be mvn compile.(It internally do javac.)

  2. Test:
    Runs test cases located in the test/ directory.The command is mvn test.

  3. Package:
    Generates JAR/WAR files from the compiled bytecode and stores them in the target/ folder.The command is mvn package.We cxan even share the jar whcih can be used as dependency to other project or we can deploy

  4. Verify:
    Checks the integrity of the package.The command is mvn verify.Here we can perform integration test and other checks.
    Ex: Maven PMD plugin.PMD is a source code analyser.It checks for unused variables, unused imports, duplicate code, empty catch blocks etc and reports a warning.

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-pmd-plugin</artifactId>
                <version>3.15.0</version>
                <executions>
                    <execution>
                        <id>pmd-validation</id>
                        <goals>
                            <goal>check</goal>
                        </goals>
                        <phase>verify</phase>
                    </execution>
                </executions>
                <configuration>
                    <printFailingErrors>true</printFailingErrors>
                    <failOnViolation>true</failOnViolation>
                </configuration>
            </plugin>
Enter fullscreen mode Exit fullscreen mode
  1. Install:
    Installs the package in the local repository.The command is mvn install.

  2. Deploy:

    Uploads the package to a remote repository.

Customizing the Build Process:
Maven provides flexibility to add specific goals in each phase through the element. You can create your own plugin or can use inbuilt plugins to extend Maven’s functionality. We have added PMD in verify phase and checkstyle validation in validate phase.

*Install with maven: *
It installs the jar file we created in compile phase after completing test,package and verify.
here it creates a m2 folder in which all the dependencies are kept. We can change this folder location from settings.xml under
<localrepository></localrepository>

Deploying with Maven
Deployment configuration is specified inside the element.

<distributionManagement>
    <repository>
        <id>internal.repo</id>
        <url>http://repo.mycompany.com/maven2</url>
    </repository>
</distributionManagement>

Enter fullscreen mode Exit fullscreen mode

Tip: we can provide credentials in settings.xml inside .m2/repository folder.

Staying updated with Maven and mastering its nuances can significantly enhance project management and build automation skills. Remember, Maven is more than just a build tool—it's project's command center. Dive deep, explore new plugins, and keep experimenting. The more we engage with Maven, the more powerful our development process becomes. Keep pushing the boundaries and let Maven handle the rest—after all, it's like having a Swiss Army knife for your project management needs!

Thank you so much for reading,will appreciate your valuable feedback.
Also tell me in the comments if you encounter any cool Plugins.
Dont forget to like,share and susbcribe.

Top comments (0)