Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


1. Overview

In this tutorial, we’ll learn about the build script blocks (scripts in build.gradle file) in Gradle and understand the purpose of the buildScript block in detail.

2. Introduction

2.1. What Is Gradle?

It’s a build automation tool that does tasks such as compiling, packaging, testing, deployment, publishing, dependency resolution, etc. Without this, we’ll have to do these tasks manually, which is quite complex and time-consuming. In today’s software development, it’s difficult to work without the build tools like this.

2.2. Common Build Script Blocks of Gradle

In this section, we’ll briefly learn about the most common build script blocks. allProjects, subProjects, plugins, dependencies, repositories, publishing, and buildScript are the most common build script blocks. The given list introduces the concept of these blocks:

  • allProjects block configures the root project and each of the sub-projects.
  • subProjects block, unlike allProjects, only configures the sub-projects.
  • plugins extend the capability of Gradle by bringing a useful set of features. For example, the java plugin adds tasks like assemble, build, clean, jar, documentation, etc., and much more.
  • dependencies by its name imply that it’s a place to declare all jars required by our project.
  • repositories block contains the location from where Gradle will download jars declared in the dependencies block. One can declare multiple locations that execute in the declaration order.
  • publishing block is declared when we develop a library and wish to publish it. This block contains details like the coordinates of the library jar and the repositories block containing the locations to publish.

Now, Let’s consider a use case where we want to use a library inside our build script. In this case, we can’t use the dependencies block as it contains jars required on the project classpath.

Since we want to use the library in the build script itself, therefore it’s required to add this library on the script classpath. And this is what buildScript is for. The next section discusses the buildScript block deeply with this use case.

3. Purpose of BuildScript Block

Considering the use case defined above, suppose in a Spring Boot app, we want to read the defined properties of the application.yml file in our build script. To achieve this, we can use a library called snakeyaml that parses YAML files and read properties easily.

As we discussed in the above section that we need this library on the script classpath. And the solution is adding it as a dependency in the buildScript block.

The script shows how to read property temp.files.path of the application.yml file. buildScript block contains the dependency of the snakeyaml library and repositories location to download it:

import org.yaml.snakeyaml.Yaml

buildscript {
    repositories {
    dependencies {
        classpath group: 'org.yaml', name: 'snakeyaml', version: '1.19'

plugins {

def prop = new Yaml().loadAll(new File("$projectDir/src/main/resources/application.yml")
var path = prop.temp.files.path

The path variable contains that value of temp.files.path. 

More on buildScript block:

  • It can contain any type of dependencies except project-type dependencies.
  • For multi-project builds, the dependencies declared are available to the build scripts of all its sub-projects.
  • To add binary plugins that are available as external jars to a project, we should add them to the build script classpath and then apply the plugin.

4. Conclusion

In this tutorial, we’ve learned about the use of Gradle, the purpose of the most common blocks of the build script, and dived deeply into the buildScript block with a use case.

Course – LS – All
announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:


res – REST with Spring (eBook) (everywhere)