01 / 21

Maven & Gradle
Build Tools Explained

Discover why modern Java projects rely on build tools to manage dependencies, compile code, and automate repetitive tasks: so you can focus on writing great software.

Java

What Is a Build Tool?

A build tool is software that automates the process of converting your source code into a runnable application. Instead of manually compiling files, downloading libraries, and packaging JARs, the build tool does it all with a single command.

Life Without a Build Tool

Imagine building a large Java project by hand. Every task must be performed manually, and every teammate must follow the exact same steps:

1
Download each library from the internet and place JARs in the right folder.
2
Compile every source file with the correct classpath flags on the command line.
3
Run all tests manually and hope nothing was missed.
4
Package the app into a JAR/WAR with the right manifest entries.

Meet Maven & Gradle

Apache Maven
Released in 2004. Uses an XML file called pom.xml to define how a project is built. Follows a strict "convention over configuration" philosophy.
Gradle
Released in 2012. Uses a Groovy/Kotlin script called build.gradle. Combines Maven's conventions with powerful scripting flexibility and faster builds.

The Build Lifecycle

Maven Default Lifecycle (Simplified) Check structure .java → .class Run unit tests Create JAR/WAR Ship it Validate Compile Test Package Deploy Each phase automatically runs all preceding phases. Running "mvn package" triggers validate → compile → test → package.

Dependency Management

The most important job of Maven and Gradle is managing dependencies: the external libraries your project needs. Instead of downloading JARs manually, you declare what you need and the build tool fetches it automatically.

Your Project Spring Boot JUnit 5 Jackson JSON spring-core spring-web jackson-core databind Transitive dependencies are resolved automatically

Transitive Dependencies

When Library A depends on Library B, and Library B depends on Library C, your build tool automatically pulls in all three. This chain is called transitive dependency resolution.

1
You declare only the libraries you directly use in your config file.
2
The build tool reads each library's own dependency metadata.
3
It downloads everything needed (and resolves version conflicts) from a central repository like Maven Central.

Maven: The pom.xml

Maven uses an XML configuration file called pom.xml (Project Object Model). Here is a minimal example declaring a JUnit dependency:

<project>
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.example</groupId>
  <artifactId>my-app</artifactId>
  <version>1.0</version>

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

Gradle: The build.gradle

Gradle uses a Groovy or Kotlin DSL instead of XML. The same JUnit dependency in Gradle looks like this:

plugins {
  id 'java'
}

group = 'com.example'
version = '1.0'

repositories {
  mavenCentral()
}

dependencies {
  testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
}

Maven vs Gradle

Maven Gradle
Config Format XML (pom.xml) Groovy / Kotlin DSL
Build Model Fixed lifecycle phases Task-based DAG
Build Speed Slower (no caching) Faster (incremental + caching)
Flexibility Rigid conventions Highly customizable
Learning Curve Easier (well-documented) Steeper (scripting power)
Used By Spring, many enterprises Android, Kotlin projects

Convention Over Configuration

Both Maven and Gradle follow a principle called convention over configuration: if you follow the standard project layout, you need almost zero configuration to get started.

my-project/ src/ main/java/ ← Your application code main/resources/ ← Config files, etc. test/java/ ← Your test code pom.xml ← Maven config build.gradle ← Gradle config Standard Maven/Gradle Project Structure Follow this layout and the build tool knows where everything is.

Using a Build Tool: Step by Step

1
Create the project structure following the standard layout (or use a generator like mvn archetype:generate or gradle init).
2
Declare dependencies in your config file (pom.xml or build.gradle). Specify the library name and version.
3
Write your code in src/main/java and your tests in src/test/java.
4
Run a build command: mvn package or gradle build. The tool compiles, tests, and packages everything.
5
Find your artifact in the target/ (Maven) or build/ (Gradle) directory, ready to run or deploy.

Example: Adding Gson as a Dependency

Suppose your project needs Google's Gson library to convert Java objects to JSON. Here is how each tool handles it:

Maven (pom.xml)
<dependency>
  <groupId>com.google.code.gson</groupId>
  <artifactId>gson</artifactId>
  <version>2.10.1</version>
</dependency>
Gradle (build.gradle)
dependencies {
  implementation 'com.google.code.gson:gson:2.10.1'
}

Example: Build & Run

Let us walk through a complete build workflow using both tools:

Maven Commands
# Clean previous build
mvn clean

# Compile + test + package
mvn package

# Run the JAR
java -jar target/my-app-1.0.jar
Gradle Commands
# Clean previous build
gradle clean

# Compile + test + package
gradle build

# Run the JAR
java -jar build/libs/my-app-1.0.jar

Extending with Plugins

Both tools support plugins that add capabilities beyond the defaults: code coverage, static analysis, Docker image creation, and more.

Build Tool Core JaCoCo (Coverage) Checkstyle (Lint) Docker Plugin Shadow (Fat JAR)

Common Mistakes to Avoid

✕ Committing JARs to Git
Never store dependency JARs in your repository. Let the build tool download them. Your config file IS the source of truth.
✕ Omitting Version Numbers
Always pin dependency versions explicitly. Without them, builds become unpredictable as libraries auto-update.
✕ Wrong Project Layout
Placing source files outside the conventional directories (src/main/java) causes the build tool to silently skip compilation.

Question 1 of 5

What is the primary purpose of build tools like Maven and Gradle in Java development?

Question 2 of 5

Which configuration file does Maven use to define a project's dependencies and build settings?

Question 3 of 5

What does transitive dependency resolution mean in the context of build tools?

Question 4 of 5

Which of the following is an advantage that Gradle has over Maven?

Question 5 of 5

What does the principle of "convention over configuration" mean for build tools?