Chapter 4. Plugin Management

In the last chapter, we discussed Gradle task, which is the atomic unit of execution in Gradle. In most cases, a task provides only a single unit of work in modules. We can choose to bundle tasks together and execute them in a certain order to provide the complete functionality. This grouping of tasks along with properties and configuration is called a plugin. A plugin is the logical grouping of tasks, which may have a life cycle. You can configure plugins to alter the behavior based on the requirements. You can extend it to provide additional features. At a broader level, Gradle provides two types of plugins; script plugin and binary plugin. Gradle treats a build script as a script plugin and you can use other build scripts in a project by importing build scripts into the current project.

Binary plugins are plugins, that we create using programming languages such as Java or Groovy. Gradle provides in-built binary plugins for different build functionalities. There are different approaches to creating a binary plugin in Gradle, which we will discuss in the Custom Plugin section. First, we will explore the script plugin.

The script plugin

A script plugin is nothing but a Gradle file, which we import into other build files. It is the same as modularizing your code across different classes. When a build file size exceeds to a certain limit or diverse functionalities are clubbed to a single file, it might be a better option to divide the cohesive tasks into different build files. Then, you can import these files to the main build file to use the new functionalities.

To import the build file you can use the following code:

apply from: <Path of otherfile.gradle>

Here, the path could be a local file or a location relative to the project directory or a valid URL. However, if you mention the URL, the downside is that the file will be downloaded each time. Once the build file is imported, you can use the tasks defined in the build file without any additional configuration.

If you are adding multiple build files in the main build file, make sure you do not have tasks with the same name in the imported build files. During import, if Gradle finds two tasks with the same name, it will throw the following exception:

* What went wrong:
A problem occurred evaluating script.
> Cannot add task ':<TASK_NAME>' as a task with that name already exists.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

Consider the following directory structure:

/Chapter4/scriptplugin.gradle

task scriptPluginTask1 << {
  println "this is scplugin1"
}

/Chapter4/build.gradle

apply from: 'scriptplugin.gradle'

task mainTask << {
  println "This is main task"
}

Execute the following command:

$ gradle mainTask scriptPluginTask1
:mainTask
This is main task
:scriptPluginTask1
this is scplugin1

BUILD SUCCESSFUL

Here, we have defined the scriptPluginTask1 in the scriptplugin.gradle file and have imported this build file in the main script build.gradle. Thus, importing the scriptplugin.gradle file into build.gradle will make scriptPluginTask1 available in the main build file and you can call it directly without mentioning any build filename.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.181.47