BuildScript of Gradle

satya - 3/18/2019, 6:46:11 PM

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

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

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

satya - 3/18/2019, 6:49:51 PM

Here is some standard dribble

Here is some standard dribble

satya - 3/18/2019, 6:55:59 PM

repositories exist both under the buildscript and also under project

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

satya - 3/18/2019, 7:04:56 PM

Relationship between plugins and the buildscript block

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.

satya - 3/18/2019, 7:07:07 PM

So if present it must be ...


buildScript {
}

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

satya - 3/18/2019, 7:08:50 PM

Here is where the plugins is explained

Here is where the plugins is explained

satya - 3/18/2019, 7:14:03 PM

So what is the difference between jar files needed by your project and those needed by Gradle itself?

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.

satya - 3/18/2019, 7:16:20 PM

Once we recognize that, now we can see the role of buildscript

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.

satya - 3/18/2019, 7:19:57 PM

There is one more subtle point, the jar files needed by the Gradle build itself

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!!