Gradle build.gradle for Spring Boot
Fast recall
Quick revision
Core answer
implementation and testImplementation. Exact plugin versions and dependency-management style depend on the Boot and Gradle versions; the separate io.spring.dependency-management plugin is common but not mandatory.Basic Gradle familiarity — can you read a Gradle file if you encounter it in a project?
Not knowing that 'implementation' in Gradle corresponds to 'compile' scope in Maven
Explain the difference between 'implementation' and 'api' Gradle configurations
Interview-ready guide
Interview answer
A basic Gradle build file for Spring Boot starts with the plugins block.
A common setup applies the Java and Spring Boot plugins, and may also apply dependency management: the java plugin to compile Java code, the spring-boot plugin to package your app as an executable JAR, and the dependency-management plugin to inherit Spring Boot's dependency versions. Next, you set the Java version in the java block using sourceCompatibility. For Spring Boot 3, that's usually Java 17 or later.
Then you have the repositories block, which almost always just says mavenCentral(). That tells Gradle where to download dependencies from. The dependencies block is where you list your libraries.
You use 'implementation' for dependencies you need at compile-time and runtime, like spring-boot-starter-web or spring-boot-starter-data-jpa. You use 'testImplementation' for test-only stuff like JUnit or Mockito. There's also 'runtimeOnly' for things you only need at runtime, like database drivers.
One thing to note: 'implementation' in Gradle is like 'compile' scope in Maven. Older Gradle code used 'compile', but that's deprecated now.
A common mistake is using 'implementation' for test dependencies instead of 'testImplementation', which means your test libraries are exposed on the wrong dependency configuration and can affect classpaths and publication. If you're building a library that other modules depend on, you'd also learn about 'api' versus 'implementation'. The 'api' configuration exposes transitive dependencies to consumers, while 'implementation' hides them.
That helps with build performance in large projects.
Gradle build.gradle for Spring Boot
Direct Answer
A basic Spring Boot Gradle build applies a Java plugin and the Spring Boot plugin, declares repositories, and lists dependencies such as implementation and testImplementation. Exact plugin versions and dependency-management style depend on the Boot and Gradle versions; the separate io.spring.dependency-management plugin is common but not mandatory.
Code Example
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.0'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17' // Java version for source code
}
repositories {
mavenCentral() // Download dependencies from Maven Central
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'com.h2database:h2' // H2 only needed at runtime
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform() // Enable JUnit 5 for tests
}
Common Mistakes & Pitfalls
- Using 'compile' instead of 'implementation': Older Gradle tutorials use the deprecated 'compile' configuration. In modern Gradle, use 'implementation' for dependencies that are both compile-time and runtime.
- Forgetting 'testImplementation': Freshers often add test libraries (like JUnit, Mockito) with 'implementation', making them available in production code, which bloats the final JAR. Use 'testImplementation' for test-only dependencies.
- Not understanding 'api' vs 'implementation': In multi-module projects, 'implementation' hides transitive dependencies from consumers, while 'api' exposes them. Most single-module projects only need 'implementation', but if you're writing a library module, understanding this distinction matters.
Check your understanding
Answer these without looking back at the article.
- What is settings.gradle?
- How do you add a custom repository in Gradle?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.