Expert · JDBC, Maven, Gradle and JUnit · topic 49 of 60 · 20 min
Gradle in brief
Explanation
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.
// 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() }Common commands
| Command | Does |
|---|---|
| ./gradlew build | Compile, test and assemble |
| ./gradlew test | Run the tests |
| ./gradlew run | Run the application (with the application plugin) |
| ./gradlew clean | Delete the build/ folder |
| ./gradlew dependencies | Show the dependency tree |
| ./gradlew tasks | List 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
| Maven | Gradle | |
|---|---|---|
| Build file | pom.xml (XML, declarative) | build.gradle(.kts) (Groovy/Kotlin script) |
| Speed | Good | Often faster: incremental builds, build cache, daemon |
| Flexibility | Convention-based, less custom logic | Very flexible scripting |
| Typical users | Most Spring Boot backends | Android, Kotlin, large multi-module builds |
| Output folder | target/ | 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.
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
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.
# backend
./mvnw test
# android app
./gradlew testDebugUnitTestPractice (0/3 done)
In Gradle, which configuration adds a library only for tests?
Where does Gradle put build output by default?
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
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Unit testing with JUnitJDBC, Maven, Gradle and JUnit · same module
- SQL injection and safe queriesJDBC, Maven, Gradle and JUnit · same module
- Project structure and GradleKotlin and Android App Development · Android Studio and your first app
- Publishing on Google PlayFlutter and Dart · Publishing to the stores and the web
- Kotlin and Android: the big pictureKotlin and Android App Development · Kotlin basics
- Writing Dockerfiles: caching, multi-stage builds and small imagesDevOps: Git, Docker, Kubernetes and CI/CD · Docker
Questions people ask about this
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.
Where does Maven cache downloaded JARs?Related
Can Kotlin and Java code be used in the same Android project?Related
Answer: Yes, they interoperate
Kotlin was designed to work alongside Java.
Which command runs the tests in a Maven project?Related
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 more topic in “JDBC, Maven, Gradle and JUnit” before its exam: Unit testing with JUnit.
- Module 10 exam: JDBC, Maven, Gradle and JUnit · 12 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Spring Boot basics. 60 topics left in the course before the final exam.