Maven: A Complete Guide to Understanding and Using the Build Tool

  1. What is Maven?

Maven is a powerful project management tool designed to handle project build processes, dependency management, and documentation generation. It follows the principle of "convention over configuration," providing developers with a standardized project structure and automated build lifecycle management.

  1. Why Use Maven?

When developing applications, developers frequently need to integrate external libraries and tools. Traditionally, this requires manually downloading JAR files from various sources and configuring them in the project classpath, which is time-consuming and error-prone.

Maven solves this problem through a centralized remote repository containing thousands of commonly used libraries. By defining dependencies in a pom.xml file, Maven automatically downloads the reuqired artifacts to your local repository and manages transitive dependencies. This approach streamlines the development workflow and ensures consistent dependency resolution across team members.

  1. How to Use Maven?

3.1 Standard Project Structure

Maven enforces a predefined directory layout that promotes consistency across projects. The src/main/java directory contains production source code, src/main/resources holds configuration files, src/test/java contains unit tests, and target directory stores compiled output.

3.2 Build Lifecycle Commands

Execute these commands from the directory containing the pom.xml file:

mvn clean // Removes compiled artifacts mvn compile // Compiles source files mvn test // Runs unit tests mvn package // Creates distributable archives mvn install // Installs package to local repository mvn clean install // Combines clean and install operations


</div>Note: Each command in the chain automatically triggers preceding lifecycle phases. The packaging element in pom.xml (pom/jar/war) determines the artifact type produced.

### 3.3 Dependency Scope and Management

The scope element controls dependency availability during different build phases:

<div>```

<scope> Values:
    compile    - Available in all classpaths, propagates to dependents (default)
    test       - Available only during test compilation and execution
    runtime    - Required at execution time but not for compilation (JDBC drivers)
    provided   - Expected to be provided by container (Servlet APIs)
    system     - Use with locally managed JARs (requires explicit path)

3.4 Maven Inheritance

Maven supports two inheritance strategies:

Direct Inheritance (Single Parent):

Parent POM configuration:

This approach allows child modules to selectively override version specifications:

Parent POM: <dependencyManagement> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> 5.3.20 </dependencyManagement>


</div><div>```

Child POM:
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.20</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>6.0.0.Final</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  1. Running Multi-Module Projects

Three approaches exist for executing applications in a multi-module setup:

Parent Project Execution: mvn tomcat:run // Run from parent directory

Child Project Execution: mvn tomcat:run // Requires other modules installed first mvn install // Parent installs all child modules

External Server Deployment: Deploy WAR artifacts to standalone servlet container


</div>5. Configuration Examples
-------------------------

**Parent POM Template:**

<div>```

<?xml version="1.0" encoding="UTF-8"?>
<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.mycompany.apps</groupId>
    <artifactId>application-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>

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

    <modules>
        <module>core-services</module>
        <module>web-ui</module>
    </modules>

    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>17</maven.compiler.source>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
                <version>31.1-jre</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

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

<artifactId>web-ui</artifactId>

<parent>
    <groupId>com.mycompany.apps</groupId>
    <artifactId>application-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
</parent>

<dependencies>
    <dependency>
        <groupId>com.mycompany.apps</groupId>
        <artifactId>core-services</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

Tags: apache-maven dependency-management build-automation pom-xml maven-multi-module

Posted on Sun, 31 May 2026 22:43:18 +0000 by Z3RatuL