Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Java
Stage 5: Specialization

Part 15 / Topic 3 of 7

Maven: coordinates, layout, and the pom.xml

Maven is a build tool driven entirely by one XML file, pom.xml (Project Object Model), plus a strict, non-negotiable directory layout. Give it the file and the layout, and mvn <phase> does the rest — fetching dependencies, compiling, testing, packaging — the same way on any machine with Maven installed. Think of a shipping label with a fixed set of fields in a fixed place: once everyone agrees "name goes here, address goes there," no courier needs to ask where to look.

GAV coordinates

Every Maven artifact — your own project, or a dependency you pull in — is identified by three coordinates, together called GAV:

Coordinate Example Means
groupId com.acme who publishes it (reverse-domain style, like a Java package)
artifactId bill-splitter the specific project/library's name
version 1.0.0 which release

The standard directory layout

Maven calls this convention over configuration: follow the expected layout, and the pom.xml barely needs to say where anything is.

bill-splitter/
├── pom.xml
├── src/
│   ├── main/java/com/acme/billsplit/   <- your application code
│   └── test/java/com/acme/billsplit/   <- your tests (Part 10's JUnit 5, now with a home)
└── target/                             <- everything Maven builds; never hand-edit, never commit

target/ is exactly as disposable as Part 13's profiler output — mvn clean deletes it, and the next build recreates it from scratch.

pom.xml anatomy

<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.acme</groupId>
  <artifactId>bill-splitter</artifactId>
  <version>1.0.0</version>
  <packaging>jar</packaging>

  <properties>
    <maven.compiler.release>21</maven.compiler.release>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.14.5</version>
      <scope>test</scope>              <!-- only needed to compile/run tests, not shipped -->
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <!-- maven-compiler-plugin, maven-surefire-plugin (runs tests),
           maven-jar-plugin (writes Main-Class into the manifest for you) -->
    </plugins>
  </build>
</project>

<scope>test</scope> is the Maven answer to a question this course has run into before (Part 10, Part 11): a test-only dependency (JUnit) should never end up bundled into a shipped jar alongside production code. Maven tracks that distinction for every dependency, not just the one you remember to be careful about.

The build lifecycle

A Maven phase is a named step; running one phase runs every phase before it too, in this fixed order:

Phase Does
validate checks the project is structured correctly
compile compiles src/main/java to target/classes
test compiles and runs src/test/java against target/classes
package bundles target/classes into a jar (or war, for web projects — not used in this course)
install copies the jar into your local repository (~/.m2/repository), for other local projects to depend on
deploy uploads the jar to a shared/remote repository, for other people's projects to depend on

mvn package runs validate → compile → test → package, in that order — which is exactly why a failing test fails mvn package too, before a jar is even built. mvn test stops one phase earlier, useful when you only want the test result and don't need a fresh jar.

Full project: examples/maven-project/ (pom.xml, src/main/java/com/acme/billsplit/, src/test/java/com/acme/billsplit/).

Done here? The practice set for Part 15 — including the solutions — is on the part page.

Search the guide