SyntaxStudy
Sign Up
Java Maven Basics and pom.xml
Java Beginner 1 min read

Maven Basics and pom.xml

Apache Maven is the most widely used build tool in the Java ecosystem. It follows the convention over configuration principle: by following a standard directory layout (src/main/java for source code, src/test/java for tests, src/main/resources for resources), Maven knows how to build your project without extensive configuration. The build lifecycle consists of phases — validate, compile, test, package, verify, install, deploy — that run in sequence. Executing mvn package compiles the source, runs the tests, and produces a JAR file. The pom.xml (Project Object Model) is the heart of a Maven project. It declares the project coordinates (groupId, artifactId, version), properties, dependencies, and plugins. Dependencies are fetched from the Maven Central repository and stored in the local repository (~/.m2/repository). Each dependency specifies a scope: compile (available everywhere, the default), test (only during testing), provided (available at compile time but provided by the runtime), and runtime (only needed at runtime). The dependency management section in a parent POM allows you to centralise version declarations so child modules do not need to repeat them. Maven enforces deterministic dependency resolution through a dependency tree: when two transitive dependencies conflict on version, Maven uses the nearest-wins strategy. The mvn dependency:tree command shows the full resolved dependency tree, which is invaluable for diagnosing version conflicts.
Example
<?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>

    <!-- Project coordinates -->
    <groupId>com.example</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- Shared properties -->
    <properties>
        <java.version>17</java.version>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>5.10.0</junit.version>
    </properties>

    <dependencies>
        <!-- Compile-scope dependency -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>32.1.2-jre</version>
        </dependency>

        <!-- Test-scope dependency -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
            </plugin>
        </plugins>
    </build>
</project>