Expand Authors Top

If you have a few years of experience in the Java ecosystem and you’d like to share that with the community, have a look at our Contribution Guidelines.

Expanded Audience – Frontegg – Security (partner)
announcement - icon User management is very complex, when implemented properly. No surprise here.

Not having to roll all of that out manually, but instead integrating a mature, fully-fledged solution - yeah, that makes a lot of sense.
That's basically what Frontegg is - User Management for your application. It's focused on making your app scalable, secure and enjoyable for your users.
From signup to authentication, it supports simple scenarios all the way to complex and custom application logic.

Have a look:

>> Elegant User Management, Tailor-made for B2B SaaS

November Discount Launch 2022 – Top
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

November Discount Launch 2022 – TEMP TOP (NPI)
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

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 {
        mavenCentral()
    }
    dependencies {
        classpath group: 'org.yaml', name: 'snakeyaml', version: '1.19'
    }
}

plugins {
    //plugins
}

def prop = new Yaml().loadAll(new File("$projectDir/src/main/resources/application.yml")
  .newInputStream()).first()
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.

November Discount Launch 2022 – Bottom
We’re finally running a Black Friday launch. All Courses are 30% off until end-of-day today:

>> GET ACCESS NOW

Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments