BuildScript of Gradle

What is the role of buildScript section in build.gradle?

Search for: What is the role of buildScript section in build.gradle?

Here is some standard dribble

So it is available with a ScriptHandler which is the delegate of the buildScript block

And also at a global level under the Project scope

The plugins {} block only allows a strict subset of the full build script programming language. Only the API of this type can be used, and values must be literal (e.g. constant strings, not variables). Moreover, the plugins {} block must be the first code of a build script. There is one exception to this, in that the buildscript {} block (used for declaring script dependencies) must precede it.


buildScript {
}

//It must be the first block
//A buildScript can precede it
plugins {
  id 'java'
}

Here is where the plugins is explained

This difference is hiding in plain sight (so to say)

If you are using a java plug in, that java plug in may need a number of jar files to execute its tasks. For example it might use Apache Commons API or a logging API to log from its code. This dependency has nothing to do with the logging libraries you may use in your java source code. For example you may use Log4J in your application code.

The Java plugin might use Apache commons or some such logging API.

So the jar dependencies are different between the plugins implementation and your application source code

Not only that, depending on your applications, there may be 20 apps and each app may depend on an entirely different set of libraries. One might be a crypto app and one might be a mobile app. etc.

So fundamentally the application jar dependencies (or classpaht) is different from the jar dependencies of your Gradle plugins.

BuildScript code is executed first

This sets up the class dependencies needed by the plugins

So this is done with repositories() and dependencies() block of the BuildScript

Once the plugins are in place and ready to compile your source code, your source codes dependencies are resolved by the repositories() and dependencies() that are outside the buildscript block.

Say if you create a custom task or a task that reaches out with in the build file (.build) itself and uploads a document through a REST API that uses Apache library.

Now, this is not your source code. it is just a build task that you wrote yourself inside the build file. So how do you get the Apache jar file???

Aha, that is why we call the such dependencies as those needed by the Gradle build execution itself.

these jar files then need to be indicated in the buildScript {} block!!