Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Language Features by Version

Maven and Gradle

Reference for Java build tools: Maven lifecycle, pom.xml, dependency scopes, Gradle tasks and build.gradle.kts, plus a side-by-side comparison and tips.

A build tool turns source code into something you can ship: it downloads libraries, compiles, runs tests, packages a JAR and often publishes it. Two tools dominate Java: Maven, which is declarative and convention-based, and Gradle, which is script-based and very flexible. Both resolve dependencies from repositories (Maven Central is the default) using the same coordinates: groupId:artifactId:version.

Neither tool can run in the sandbox on this page, so the configuration files below are read-only and clearly marked. Runnable stand-ins show the two ideas that are easy to understand once you can see them: reading a pom.xml and ordering tasks by their dependencies.

Maven

Maven describes a project in a pom.xml (project object model). The directory layout is fixed by convention: src/main/java, src/main/resources, src/test/java, and output in target/.

XML
<!-- Read-only: pom.xml -->
<project xmlns="http://maven.apache.org/POM/4.0.0">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shop</groupId>
  <artifactId>orders</artifactId>
  <version>1.0.0</version>

  <properties>
    <maven.compiler.release>25</maven.compiler.release>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.22.0</version>
    </dependency>
    <dependency>
      <groupId>org.junit.jupiter</groupId>
      <artifactId>junit-jupiter</artifactId>
      <version>6.1.3</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
Terminal
# Read-only: common commands
mvn clean verify              # clean, compile, test, package, run integration checks
mvn test -Dtest=OrderTest     # a single test class
mvn dependency:tree           # show resolved dependencies and why they are there
mvn -q package -DskipTests
mvn versions:display-dependency-updates

The default lifecycle

A phase runs all earlier phases first. mvn package therefore also compiles and tests.

Phase What happens
validate Check that the project is correct
compile Compile main sources
test Run unit tests (Surefire plugin)
package Build the JAR or WAR
verify Run integration tests and checks (Failsafe plugin)
install Copy the artifact to the local repository ~/.m2
deploy Upload to a remote repository

Dependency scopes

Scope On the compile class path On the test class path At run time
compile (default) yes yes yes
provided yes yes no (the container supplies it)
runtime no yes yes
test no yes no

Gradle

Gradle uses a script in the Kotlin DSL (build.gradle.kts) or Groovy (build.gradle). It models work as tasks that depend on each other and skips tasks whose inputs did not change (incremental builds and a build cache).

Kotlin
// Read-only: build.gradle.kts
plugins {
    java
    application
}

repositories {
    mavenCentral()
}

java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
    }
}

dependencies {
    implementation("com.fasterxml.jackson.core:jackson-databind:2.22.0")
    testImplementation(platform("org.junit:junit-bom:6.1.3"))
    testImplementation("org.junit.jupiter:junit-jupiter")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

application {
    mainClass = "com.shop.Main"
}

tasks.test {
    useJUnitPlatform()
}
Terminal
# Read-only: use the wrapper so everyone builds with the same Gradle version
./gradlew build
./gradlew test --tests "com.shop.OrderTest"
./gradlew dependencies --configuration runtimeClasspath
./gradlew run
./gradlew build --scan

implementation keeps a dependency off the compile class path of your consumers, while api (from the java-library plugin) exposes it. Gradle's toolchains download and select the right JDK, so the build does not depend on which Java happens to be installed.

Try it

Java Reading a pom.xml (what Maven does first)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Ordering tasks like a build tool

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Maven or Gradle?

Topic Maven Gradle
Style Declarative XML; plugins bound to phases Code (Kotlin or Groovy DSL); tasks and a task graph
Learning curve Small, uniform across projects Steeper, more powerful
Speed Good; Maven 4 and the build cache extension help Incremental builds, daemon and build cache are built in
Customization Write or configure a plugin Write a task or a convention plugin inline
Android Not used The standard
Multi-module <modules> in a parent POM settings.gradle.kts with include(...)

Usage notes

  • Use the wrappers (mvnw, gradlew). They are checked into the repository and download the exact tool version, so a build behaves the same on every machine and in CI.
  • Use a BOM or version catalog to keep related versions aligned: Maven's <dependencyManagement> with <scope>import</scope>, Gradle's platform(...) and gradle/libs.versions.toml.
  • Pin plugin versions, because an unpinned plugin can change behavior when the tool upgrades.
  • Snapshots (1.0-SNAPSHOT) are mutable and may change between builds. Release builds should depend on fixed versions only.
  • Transitive dependencies come along automatically. Inspect them with dependency:tree or gradlew dependencies, and exclude or constrain a version when two libraries disagree.
  • Prefer --release over -source/-target in the compiler settings (maven.compiler.release, options.release).
  • Other build tools exist (Ant, sbt, Bazel, Bld), but Maven and Gradle cover almost all Java jobs. IDEs import either format directly.