In this section, we will discuss how to create a custom plugin. A plugin can be created by implementing the org.gradle.api.Plugin<T>
interface. This interface has one method named apply(T target)
, which must be implemented in the plugin class. Typically, we write a plugin for the Gradle projects. In that situation, T becomes the Project. However, T can be any type of object.
The class that implements the plugin interface can be placed in various locations, such as:
The same build file
The
buildSrc
directoryA standalone project
This is similar to creating a custom task that we discussed in the last chapter. When we define a plugin in the same build file, the scope is limited to the defining project only. This means, this plugin cannot be reused in any other projects. This is not a good idea, if we want to distribute our plugin for other projects. For a multiproject Gradle build, the plugin code can be placed in the buildSrc
folder of the root project or build file of the...