Section 1
What is Gradle?
Gradle is an open-source build automation tool used for compiling, testing, packaging, and deploying software projects. It is the default build system for Android applications and is widely used in Java, Kotlin, Spring Boot, Scala, Groovy, and many other JVM-based projects.
Unlike traditional build tools, Gradle combines the flexibility of scripting with the performance of incremental builds, making it suitable for projects ranging from small applications to enterprise-scale systems.
I’ll expand the earlier outline into detailed, blog-ready pointers: beginner first, then intermediate, then advanced, with practical examples and SEO sections included.Thought for a couple of seconds
Below is the detailed pointer-wise blog structure.
Gradle Complete Guide: Basic, Intermediate and Advanced Concepts
1. What is Gradle?
Gradle is a build automation tool.
It helps developers to:
- Compile source code
- Download dependencies
- Run tests
- Package application as JAR/WAR
- Generate reports
- Build Docker images
- Publish artifacts
- Run CI/CD builds
Example:
./gradlew clean build
This single command can clean, compile, test, and package the project.
2. Why Do We Need Gradle?
Without Gradle, developers must manually:
- Download JAR files
- Add them to classpath
- Compile Java files
- Run test cases
- Create JAR/WAR manually
- Manage different versions of libraries
Gradle automates all these steps.
Example:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}
Gradle downloads Spring Boot, Tomcat, Jackson, logging libraries, and required transitive dependencies automatically.
3. Gradle vs Maven
| Point | Gradle | Maven |
|---|---|---|
| Build file | build.gradle | pom.xml |
| Syntax | Groovy/Kotlin | XML |
| Speed | Faster | Slower |
| Customization | High | Limited |
| Android support | Official | Not preferred |
| Multi-module support | Excellent | Good |
| Learning curve | Medium | Easy |
Gradle is more flexible and faster, while Maven is simpler and more convention-based.
Basic Gradle Concepts
4. Gradle Project Structure
A basic Gradle Java project looks like this:
my-app
├── build.gradle
├── settings.gradle
├── gradlew
├── gradlew.bat
├── gradle
│ └── wrapper
└── src
├── main
│ └── java
└── test
└── java
Meaning:
build.gradlecontains build configurationsettings.gradlecontains project name and module detailsgradlewis Gradle Wrapper for Linux/Macgradlew.batis Gradle Wrapper for Windowssrc/main/javacontains application codesrc/test/javacontains test code
5. build.gradle File
Example:
plugins {
id 'java'
}
group = 'com.truecode'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.12.0'
}
test {
useJUnitPlatform()
}
Explanation:
pluginsdefines what type of project it isgroupdefines organization/package identityversiondefines artifact versionrepositoriestells Gradle where to download dependenciesdependenciesdeclares required librariestestconfigures test execution
6. settings.gradle File
Example:
rootProject.name = 'gradle-demo'
For multi-module project:
rootProject.name = 'company-project'
include 'common'
include 'service'
include 'api'
This tells Gradle which modules are part of the project.
7. Gradle Plugins
Plugins add functionality to Gradle.
Common plugins:
plugins {
id 'java'
id 'application'
id 'jacoco'
}
Important plugins:
java— Java project supportapplication— run Java main classwar— create WAR filejacoco— code coveragemaven-publish— publish JARorg.springframework.boot— Spring Boot support
8. Repositories
Repositories are places where Gradle searches for dependencies.
Example:
repositories {
mavenCentral()
}
Common repositories:
mavenCentral()google()mavenLocal()- company Nexus
- company Artifactory
Example with custom repository:
repositories {
mavenCentral()
maven {
url = uri("https://repo.company.com/maven")
}
}
9. Dependencies
Dependencies are external libraries needed by the project.
Example:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.junit.jupiter:junit-jupiter:5.12.0'
}
Common dependency types:
implementationapicompileOnlyruntimeOnlytestImplementationannotationProcessor
10. Common Gradle Commands
./gradlew build
Builds the complete project.
./gradlew clean
Deletes generated build files.
./gradlew test
Runs test cases.
./gradlew jar
Creates JAR file.
./gradlew bootRun
Runs Spring Boot application.
./gradlew dependencies
Shows dependency tree.
./gradlew tasks
Shows all available Gradle tasks.
Intermediate Gradle Concepts
11. Gradle Lifecycle
Gradle build has three phases.
1. Initialization Phase
Gradle reads:
settings.gradle
It identifies:
- Root project
- Sub-projects
- Multi-module structure
2. Configuration Phase
Gradle reads:
build.gradle
It creates a task graph.
Example:
./gradlew build
Gradle decides which tasks are required before build.
3. Execution Phase
Gradle executes selected tasks.
Example flow:
compileJava
processResources
classes
test
jar
assemble
build
12. Gradle Tasks
A task is a unit of work.
Examples:
- compile code
- run tests
- create JAR
- copy files
- generate reports
Custom task:
tasks.register("hello") {
doLast {
println "Hello from Gradle"
}
}
Run:
./gradlew hello
Output:
Hello from Gradle
13. implementation vs api
implementation
Use this when dependency is internal to your module.
implementation 'org.apache.commons:commons-lang3:3.14.0'
Other modules do not directly see this dependency.
api
Use this when dependency is exposed to other modules.
api 'com.fasterxml.jackson.core:jackson-databind:2.17.0'
Usually used in library modules.
Rule:
- Use
implementationby default - Use
apionly when required
14. compileOnly
Used when dependency is needed only during compile time.
Example:
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
Lombok is needed during compilation but not at runtime.
15. runtimeOnly
Used when dependency is required only at runtime.
Example:
runtimeOnly 'org.postgresql:postgresql'
Your code may not directly compile against PostgreSQL driver, but the application needs it while running.
16. Excluding Transitive Dependencies
Sometimes Gradle downloads unnecessary dependencies automatically.
Example:
implementation('org.springframework.boot:spring-boot-starter-web') {
exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
}
Useful when replacing Tomcat with Jetty or Undertow.
17. Viewing Dependency Tree
Command:
./gradlew dependencies
This helps to find:
- Version conflicts
- Duplicate libraries
- Unwanted transitive dependencies
- Security vulnerable libraries
For a specific dependency:
./gradlew dependencyInsight --dependency log4j
18. Gradle Wrapper
Gradle Wrapper allows the project to use a fixed Gradle version.
Files:
gradlew
gradlew.bat
gradle/wrapper/gradle-wrapper.properties
Run with wrapper:
./gradlew build
Advantage:
- No need to install Gradle manually
- Same Gradle version for all developers
- Same Gradle version in Jenkins
- Avoids “works on my machine” issues
19. Spring Boot with Gradle
Example:
plugins {
id 'java'
id 'org.springframework.boot' version '3.5.0'
id 'io.spring.dependency-management' version '1.1.7'
}
group = 'com.truecode'
version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
test {
useJUnitPlatform()
}
Important commands:
./gradlew bootRun
Runs Spring Boot app.
./gradlew bootJar
Creates executable Spring Boot JAR.
./gradlew build
Builds complete app.
20. jar vs bootJar
jar
Creates normal Java JAR.
./gradlew jar
bootJar
Creates executable Spring Boot JAR.
./gradlew bootJar
bootJar contains:
- Your application classes
- Dependencies
- Embedded Tomcat
- Spring Boot launcher
For Spring Boot apps, bootJar is usually used.
21. JaCoCo Code Coverage
JaCoCo checks test coverage.
Example:
plugins {
id 'jacoco'
}
jacocoTestReport {
reports {
xml.required = true
html.required = true
}
}
Run:
./gradlew test jacocoTestReport
Coverage report location:
build/reports/jacoco/test/html/index.html
22. Multi-module Gradle Project
Example structure:
company-app
├── settings.gradle
├── build.gradle
├── common
│ └── build.gradle
├── service
│ └── build.gradle
└── api
└── build.gradle
settings.gradle:
rootProject.name = 'company-app'
include 'common'
include 'service'
include 'api'
Example dependency between modules:
dependencies {
implementation project(':common')
}
Useful for:
- Large enterprise applications
- Microservices shared libraries
- Common DTO modules
- Common utility modules
- API and service separation
Advanced Gradle Concepts
23. Gradle Daemon
Gradle Daemon keeps Gradle running in the background.
Benefit:
- Faster builds
- Avoids JVM startup every time
- Useful in local development
Check daemon:
./gradlew --status
Stop daemon:
./gradlew --stop
24. Incremental Build
Gradle does not rebuild everything every time.
If only one Java file changed, Gradle recompiles only required parts.
Example:
./gradlew build
You may see:
compileJava UP-TO-DATE
test UP-TO-DATE
Meaning Gradle skipped unchanged tasks.
25. Build Cache
Build cache stores previous build outputs.
Enable in gradle.properties:
org.gradle.caching=true
Benefits:
- Faster local builds
- Faster CI builds
- Reuses outputs from previous builds
- Very useful for large projects
26. Parallel Build
Enable in gradle.properties:
org.gradle.parallel=true
Gradle can build independent modules in parallel.
Useful for multi-module projects.
27. Configuration Cache
Enable:
org.gradle.configuration-cache=true
Gradle reuses configuration phase output.
Useful when:
- Project is large
- Many plugins are used
- Configuration phase takes time
28. Version Catalog
Version Catalog manages dependency versions centrally.
File:
gradle/libs.versions.toml
Example:
[versions]
springBoot = "3.5.0"
junit = "5.12.0"
[libraries]
junit-jupiter = { module = “org.junit.jupiter:junit-jupiter”, version.ref = “junit” }
Usage:
dependencies {
testImplementation libs.junit.jupiter
}
Benefits:
- Centralized dependency version
- Cleaner build files
- Easier upgrades
- Good for multi-module projects
29. Custom Gradle Tasks
Example:
tasks.register("printProjectInfo") {
doLast {
println "Project: ${project.name}"
println "Version: ${project.version}"
}
}
Run:
./gradlew printProjectInfo
Useful for:
- Copying files
- Generating reports
- Validating project setup
- Printing build metadata
- Preparing deployment artifacts
30. Custom Gradle Plugin
When the same build logic is repeated in many projects, create a custom plugin.
Example use case:
- Common Java version
- Common repository
- Common test setup
- Common JaCoCo rule
- Common code style rule
Instead of repeating this in every project, create one company plugin.
Example:
plugins {
id 'com.company.java-conventions'
}
Useful in large companies.
31. Publishing Artifacts
Gradle can publish JARs to Maven repository.
Plugin:
plugins {
id 'maven-publish'
}
Example:
publishing {
publications {
mavenJava(MavenPublication) {
from components.java
}
}
}
Publish command:
./gradlew publish
Used to publish to:
- Maven Local
- Nexus
- Artifactory
- Maven Central
32. Gradle in Jenkins CI/CD
Example Jenkinsfile:
pipeline {
agent any
stages {
stage('Build') {
steps {
sh './gradlew clean build'
}
}
stage('Test') {
steps {
sh './gradlew test'
}
}
stage('Code Coverage') {
steps {
sh './gradlew jacocoTestReport'
}
}
}
}
In CI/CD, Gradle is used for:
- Build
- Test
- Code coverage
- Security scan
- Artifact creation
- Docker image build
- Deployment
33. Gradle Best Practices
Use these in real projects:
- Always use Gradle Wrapper
- Avoid using system-installed Gradle
- Use
implementationinstead ofapiby default - Avoid dynamic dependency versions like
1.+ - Use Version Catalog for large projects
- Enable build cache
- Enable parallel build for multi-module projects
- Keep build scripts clean
- Move repeated logic to custom plugin
- Use JaCoCo for code coverage
- Use dependency locking for production projects
- Regularly check dependency vulnerabilities
Common Gradle Errors
34. Could not resolve dependency
Reason:
- Wrong version
- Repository missing
- Network issue
- Private repository authentication issue
Fix:
repositories {
mavenCentral()
}
35. Gradle version mismatch
Reason:
Different developers use different Gradle versions.
Fix:
Use wrapper:
./gradlew build
Not:
gradle build
36. Plugin not found
Reason:
Plugin version missing or wrong.
Fix:
plugins {
id 'org.springframework.boot' version '3.5.0'
}
37. Main class not found
For application plugin:
application {
mainClass = 'com.truecode.MainApplication'
}
For Spring Boot, usually Spring Boot detects main class automatically.
Gradle Interview Questions
Use these at the end of your blog:
- What is Gradle?
- Why is Gradle faster than Maven?
- What is
build.gradle? - What is
settings.gradle? - What is Gradle Wrapper?
- What are Gradle plugins?
- What is a Gradle task?
- Explain Gradle lifecycle.
- Difference between
implementationandapi. - Difference between
compileOnlyandruntimeOnly. - What is transitive dependency?
- How do you exclude dependencies?
- What is dependencyInsight?
- What is Gradle Daemon?
- What is incremental build?
- What is build cache?
- What is configuration cache?
- What is Version Catalog?
- What is multi-module Gradle project?
- Difference between
jarandbootJar.
Conclusion
Gradle is a powerful build automation tool widely used in Java, Kotlin, Android, and Spring Boot projects. Beginners should first understand build.gradle, plugins, repositories, dependencies, and common commands. Intermediate developers should learn dependency management, Gradle Wrapper, tasks, JaCoCo, and multi-module builds. Advanced developers should focus on build cache, configuration cache, custom plugins, publishing artifacts, and CI/CD integration.
For Java and Spring Boot developers, Gradle is an important skill because it improves productivity, build performance, and project maintainability.