Phase 5 · Professional DevelopmentModule 31~38 min read

Build Tools & Dependency Management

Automate builds and manage dependencies with Maven and Gradle.

What you'll learn

Real Java projects have dozens of libraries, a compile step, tests, and packaging. Doing that by hand is impossible to reproduce. Build tools — Maven and Gradle — automate all of it and manage your dependencies. Every professional project uses one.

By the end you'll be able to:

  • Explain what build tools do and why they matter
  • Read a Maven pom.xml and run its lifecycle
  • Add dependencies and understand scopes and transitive dependencies
  • Recognise a Gradle build script
  • Understand multi-module and reproducible builds

Why build tools matter

A build tool solves three problems at once: dependency management (declare a library and the tool downloads it and everything it needs), build automation (compile, test, and package with one command), and reproducibility (anyone, anywhere, gets an identical build). No more hunting for JARs or "works on my machine."

Maven & pom.xml

Maven is convention-based: put your code in the standard folders and it just works. Its heart is pom.xml (Project Object Model) — an XML file declaring your project's identity (the coordinates groupId : artifactId : version) and its dependencies:

pom.xml
<project>
  <groupId>com.mastercoding</groupId>
  <artifactId>app</artifactId>
  <version>1.0.0</version>

  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.17.0</version>
    </dependency>

    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>5.10.0</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

The Maven lifecycle

Maven runs through a fixed sequence of phases. Running one phase runs all the phases before it — so mvn package automatically validates, compiles, and tests first:

The build lifecycle
validate→compile→test→package→verify→install→deploy
Terminal
mvn compile     # compile the source code
mvn test        # compile + run the tests
mvn package     # compile + test + build the JAR
mvn install     # + install to your local repository

Dependencies & scopes

You declare a dependency by its coordinates; Maven fetches it (and its own dependencies — transitive dependencies) from a central repository. A dependency's scope controls when it's available — for example, test libraries shouldn't ship in your production JAR:

ScopeAvailable atExample
compileeverywhere (the default)your core libraries
providedcompile & test, not packageda servlet API the server supplies
runtimerun & test, not compilea JDBC driver
testtests onlyJUnit, Mockito

Mind your dependencies

Every dependency is code you didn't write running in your app — a maintenance and security surface (Module 36). Keep them updated, remove unused ones, and prefer well-maintained libraries. Tools can scan for known vulnerabilities in your dependency tree.

Gradle

Gradle is the other major build tool — the default for Android. Instead of XML it uses a concise script (Groovy or Kotlin), and it's faster thanks to a build cache and incremental builds. The same two dependencies look like this:

build.gradle
plugins {
    id 'java'
}

dependencies {
    implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}

Note

Both tools support multi-module projects (one build coordinating several sub-projects) — essential for large codebases. Maven vs Gradle is often just a team preference; Android uses Gradle, many enterprise backends use Maven.

Recap & quick check

Key takeaways

  • Build tools automate compile/test/package and manage dependencies reproducibly.
  • Maven's pom.xml declares coordinates (groupId:artifactId:version) and dependencies.
  • Running a lifecycle phase runs all earlier phases (mvn package also compiles and tests).
  • Dependency scope controls availability (compile, provided, runtime, test); deps pull in transitive deps.
  • Gradle is script-based and fast (Android's default); both support multi-module builds.

Quick check

1. What is the main file that configures a Maven project?

2. What happens when you run 'mvn package'?

3. What is a transitive dependency?

4. Which scope keeps a library out of the production build?

5. Which build tool is the default for Android?

Great — your projects can now build and manage dependencies professionally. Next up: Module 32 — Git & Professional Java Workflow.