Skip to content

Expert · JDBC, Maven, Gradle and JUnit · topic 49 of 60 · 20 min

Gradle in brief

Explanation

Gradle is the other major Java build tool. It uses a build script (build.gradle in Groovy or build.gradle.kts in Kotlin) instead of XML, is fast thanks to incremental builds and caching, and is the standard for Android. Dependencies use configurations like implementation and testImplementation; tasks run with ./gradlew build or ./gradlew test.

Why learn Gradle too?

You will meet both tools at work. Many backend teams use Maven; Android projects, many Kotlin projects and some large multi-module builds use Gradle. The ideas are the same (dependencies from Maven Central, a standard layout, test and build steps), so learning the second tool is quick.

kotlin
// build.gradle.kts (Kotlin DSL)
plugins {
    java
    application
}

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

repositories { mavenCentral() }

dependencies {
    implementation("org.postgresql:postgresql:42.7.4")             // example versions
    testImplementation("org.junit.jupiter:junit-jupiter:5.11.3")
    testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

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

tasks.test { useJUnitPlatform() }
The same project as the Maven example, as a Gradle Kotlin script.

Common commands

CommandDoes
./gradlew buildCompile, test and assemble
./gradlew testRun the tests
./gradlew runRun the application (with the application plugin)
./gradlew cleanDelete the build/ folder
./gradlew dependenciesShow the dependency tree
./gradlew tasksList available tasks

Dependency configurations

  • implementation: needed to compile and run your code (like Maven compile scope).
  • testImplementation: only for tests (like Maven test scope).
  • runtimeOnly: needed only when running (for example a JDBC driver loaded at run time).
  • compileOnly: needed only to compile (for example annotation-only libraries).

Maven versus Gradle

MavenGradle
Build filepom.xml (XML, declarative)build.gradle(.kts) (Groovy/Kotlin script)
SpeedGoodOften faster: incremental builds, build cache, daemon
FlexibilityConvention-based, less custom logicVery flexible scripting
Typical usersMost Spring Boot backendsAndroid, Kotlin, large multi-module builds
Output foldertarget/build/

Multi-module builds and the build cache

Larger systems split code into modules (for example api, service, persistence) declared in settings.gradle.kts with include("api", "service"). Gradle builds only the modules that changed and can reuse outputs from earlier builds (the build cache), which is why big Android and backend projects often prefer it. The Gradle daemon, a background process kept warm between builds, also saves start-up time on every command.

Careful: Build scripts are code: copying random snippets from old blog posts often uses deprecated configurations such as compile and testCompile (removed in Gradle 7). Use implementation and testImplementation, and keep the Gradle Wrapper (gradlew) in the repository so everyone uses the same Gradle version.
Think of it like this: If Maven is a printed form you fill in (fixed boxes, predictable), Gradle is a recipe you can write in your own words. Both produce the same dish; the form is easier to read at a glance, the recipe is easier to customise.
Tip: Spring Initializr (start.spring.io) can generate a project with either Maven or Gradle. Generate the same small project both ways and compare the files side by side.

Need help?

Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.

Try:

Watch the lesson

Gradle in brief · 1/6

Gradle

In this lesson you will meet Gradle, the other main Java build tool.

Prefer a video by a teacher? Find free videos on “gradle tutorial for java developers build.gradle.kts dependencies” on YouTube ↗

In real life: An Android app and a backend in one company

A food-delivery start-up has an Android app built with Gradle (as all Android projects are) and a Spring Boot backend built with Maven. Developers who move between the two teams only need to learn where each tool keeps dependencies and which command runs the tests.

bash
# backend
./mvnw test
# android app
./gradlew testDebugUnitTest

Practice (0/3 done)

Task 1 · Question

In Gradle, which configuration adds a library only for tests?

Task 2 · Question

Where does Gradle put build output by default?

Task 3 · Practice

Generate the same tiny Spring Boot project twice on start.spring.io: once with Maven and once with Gradle (Kotlin DSL). Open pom.xml and build.gradle.kts side by side and write down how each declares (a) the Java version, (b) the web starter dependency, (c) the test dependency. Run the tests with both wrappers.

Quick quiz

Question 1

1.Which build tool do Android projects use?

Question 2

2.What does ./gradlew mean?

Question 3

3.Which replaced the old compile configuration?

Question 4

4.Which file is a Gradle build script in Kotlin?

Read more (free)

Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.

Which build tool do Android projects use?

Answer: Gradle

Android builds are Gradle-based.

What does ./gradlew mean?

Answer: The Gradle Wrapper, which uses the project's pinned Gradle version

The wrapper downloads the right Gradle version.

Which replaced the old compile configuration?

Answer: implementation (and api)

compile was removed in Gradle 7.

Which file is a Gradle build script in Kotlin?

Answer: build.gradle.kts

.kts = Kotlin script.

Which file declares permissions like INTERNET?Related

Answer: AndroidManifest.xml

Permissions are declared in the manifest.

From Project structure and Gradle

Where does Maven cache downloaded JARs?Related

Answer: ~/.m2/repository

The local repository.

From Maven: building and dependencies

Can Kotlin and Java code be used in the same Android project?Related

Answer: Yes, they interoperate

Kotlin was designed to work alongside Java.

From Kotlin and Android: the big picture

Which command runs the tests in a Maven project?Related

Answer: mvn test

The test phase runs the Surefire plugin.

From Unit testing with JUnit

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

Next steps

  1. 1 more topic in “JDBC, Maven, Gradle and JUnit” before its exam: Unit testing with JUnit.
  2. Module 10 exam: JDBC, Maven, Gradle and JUnit · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Spring Boot basics. 60 topics left in the course before the final exam.

← Previous: Maven: building and dependencies