// Copyright 2017 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

[[custom_plugins]]
= Developing Custom Gradle Plugins

A Gradle plugin packages up reusable pieces of build logic, which can be used across many different projects and builds.
Gradle allows you to implement your own plugins, so you can reuse your build logic, and share it with others.

You can implement a Gradle plugin in any language you like, provided the implementation ends up compiled as JVM bytecode.
In our examples, we are going to use Java as the implementation language for standalone plugin project and Groovy or Kotlin in the buildscript plugin examples.
In general, a plugin implemented using Java or Kotlin, which are statically typed, will perform better than the same plugin implemented using Groovy.

[[sec:packaging_a_plugin]]
== Packaging a plugin

There are several places where you can put the source for the plugin.

Build script::
You can include the source for the plugin directly in the build script.
This has the benefit that the plugin is automatically compiled and included in the classpath of the build script without you having to do anything.
However, the plugin is not visible outside the build script, and so you cannot reuse the plugin outside the build script it is defined in.

`buildSrc` project::
You can put the source for the plugin in the `__rootProjectDir__/buildSrc/src/main/java` directory
(or `__rootProjectDir__/buildSrc/src/main/groovy` or `__rootProjectDir__/buildSrc/src/main/kotlin` depending on which language you prefer).
Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script.
The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.
+
See <<organizing_gradle_projects.adoc#organizing_gradle_projects,Organizing Gradle Projects>> for more details about the `buildSrc` project.

Standalone project::
You can create a separate project for your plugin. This project produces and publishes a JAR which you can then use in multiple builds and share with others.
Generally, this JAR might include some plugins, or bundle several related task classes into a single library. Or some combination of the two.


In our examples, we will start with the plugin in the build script, to keep things simple. Then we will look at creating a standalone project.

[[sec:writing_a_simple_plugin]]
== Writing a simple plugin

To create a Gradle plugin, you need to write a class that implements the link:{javadocPath}/org/gradle/api/Plugin.html[Plugin] interface.
When the plugin is applied to a project, Gradle creates an instance of the plugin class and calls the instance's link:{javadocPath}/org/gradle/api/Plugin.html#apply-T-[Plugin.apply()] method.
The project object is passed as a parameter, which the plugin can use to configure the project however it needs to.
The following sample contains a greeting plugin, which adds a `hello` task to the project.

.A custom plugin
====
include::sample[dir="snippets/customPlugins/customPlugin/groovy",files="build.gradle[]"]
include::sample[dir="snippets/customPlugins/customPlugin/kotlin",files="build.gradle.kts[]"]
====

.Output of **`gradle -q hello`**
----
> gradle -q hello
include::{snippetsPath}/customPlugins/customPlugin/tests/customPlugin.out[]
----

One thing to note is that a new instance of a plugin is created for each project it is applied to. Also note that the link:{javadocPath}/org/gradle/api/Plugin.html[Plugin] class is a generic type.
This example has it receiving the link:{groovyDslPath}/org.gradle.api.Project.html[Project] type as a type parameter.
A plugin can instead receive a parameter of type link:{groovyDslPath}/org.gradle.api.initialization.Settings.html[Settings], in which case the plugin can be applied in a settings script,
or a parameter of type link:{groovyDslPath}/org.gradle.api.invocation.Gradle.html[Gradle], in which case the plugin can be applied in an initialization script.

[[sec:getting_input_from_the_build]]
== Making the plugin configurable

Most plugins offer some configuration options for build scripts and other plugins to use to customize how the plugin works. Plugins do this using _extension objects_.
The Gradle link:{groovyDslPath}/org.gradle.api.Project.html[Project] has an associated link:{javadocPath}/org/gradle/api/plugins/ExtensionContainer.html[ExtensionContainer] object
that contains all the settings and properties for the plugins that have been applied to the project.
You can provide configuration for your plugin by adding an extension object to this container.
An extension object is simply an object with Java Bean properties that represent the configuration.

Let's add a simple extension object to the project. Here we add a `greeting` extension object to the project, which allows you to configure the greeting.

.A custom plugin extension
====
include::sample[dir="snippets/customPlugins/customPluginWithConvention/groovy",files="build.gradle[]"]
include::sample[dir="snippets/customPlugins/customPluginWithConvention/kotlin",files="build.gradle.kts[]"]
====

.Output of **`gradle -q hello`**
----
> gradle -q hello
include::{snippetsPath}/customPlugins/customPluginWithConvention/tests/customPluginWithConvention.out[]
----

In this example, `GreetingPluginExtension` is an object with a property called `message`. The extension object is added to the project with the name `greeting`.
This object then becomes available as a project property with the same name as the extension object.

Oftentimes, you have several related properties you need to specify on a single plugin. Gradle adds a configuration block for each extension object, so you can group settings together.
The following example shows you how this works.

.A custom plugin with configuration block
====
include::sample[dir="snippets/customPlugins/customPluginWithAdvancedConvention/groovy",files="build.gradle[]"]
include::sample[dir="snippets/customPlugins/customPluginWithAdvancedConvention/kotlin",files="build.gradle.kts[]"]
====

.Output of **`gradle -q hello`**
----
> gradle -q hello
include::{snippetsPath}/customPlugins/customPluginWithAdvancedConvention/tests/customPluginWithAdvancedConvention.out[]
----

[.multi-language-text.lang-groovy]
In this example, several settings can be grouped together within the `greeting` closure. The name of the closure block in the build script (`greeting`) needs to match the extension object name.
Then, when the closure is executed, the fields on the extension object will be mapped to the variables within the closure based on the standard Groovy closure delegate feature.

[.multi-language-text.lang-kotlin]
In this example, several settings can be grouped together within the `configure<GreetingPluginExtension>` block.
The type used on the `configure` function in the build script (`GreetingPluginExtension`) needs to match the extension type.
Then, when the block is executed, the receiver of the block is the extension.

In this way, using an extension object _extends_ the Gradle DSL to add a project property and DSL block for the plugin.
And because an extension object is simply a regular object, you can provide your own DSL nested inside the plugin block by adding properties and methods to the extension object.

=== Developing project extensions

You can find out more about implementing project extensions in <<custom_gradle_types.adoc#custom_gradle_types,Developing Custom Gradle Types>>.

[[sec:working_with_files_in_custom_tasks_and_plugins]]
== Working with files in custom tasks and plugins

When developing custom tasks and plugins, it's a good idea to be very flexible when accepting input configuration for file locations.
To do this, you can leverage the link:{groovyDslPath}/org.gradle.api.Project.html#org.gradle.api.Project:file(java.lang.Object)[Project.file(java.lang.Object)] method to resolve values to files as late as possible.

.Evaluating file properties lazily
====
include::sample[dir="snippets/tasks/customTaskWithFileProperty/groovy",files="build.gradle[tags=all]"]
include::sample[dir="snippets/tasks/customTaskWithFileProperty/kotlin",files="build.gradle.kts[tags=all]"]
====

.Output of **`gradle -q sayGreeting`**
----
> gradle -q sayGreeting
include::{snippetsPath}/tasks/customTaskWithFileProperty/tests/lazyFileProperties.out[]
----

In this example, we configure the `greet` task `destination` property as a closure/provider, which is evaluated with
the link:{groovyDslPath}/org.gradle.api.Project.html#org.gradle.api.Project:file(java.lang.Object)[Project.file(java.lang.Object)] method to turn the return value of the closure/provider
into a `File` object at the last minute. You will notice that in the example above we specify the `greetingFile` property value after we have configured to use it for the task.
This kind of lazy evaluation is a key benefit of accepting any value when setting a file property, then resolving that value when reading the property.

[[sec:mapping_extension_properties_to_task_properties]]
== Mapping extension properties to task properties

Capturing user input from the build script through an extension and mapping it to input/output properties of a custom task is a useful pattern.
The build script author interacts only with the DSL defined by the extension. The imperative logic is hidden in the plugin implementation.

Gradle provides some types that you can use in task implementations and extensions to help you with this. Refer to <<lazy_configuration.adoc#lazy_configuration,Lazy Configuration>> for more information.

[[sec:custom_plugins_standalone_project]]
== A standalone project

Now we will move our plugin to a standalone project so that we can publish it and share it with others. This project is simply a Java project that produces a JAR containing the plugin classes.
The easiest and the recommended way to package and publish a plugin is to use the <<java_gradle_plugin.adoc#java_gradle_plugin,Java Gradle Plugin Development Plugin>>.
This plugin will automatically apply the <<java_plugin.adoc#java_plugin,Java Plugin>>, add the `gradleApi()` dependency to the api configuration,
generate the required plugin descriptors in the resulting JAR file and configure the <<plugins.adoc#sec:plugin_markers,Plugin Marker Artifact>> to be used when publishing.
Here is a simple build script for the project.

.A build for a custom plugin
====
include::sample[dir="snippets/plugins/customPlugin/groovy/java-gradle-plugin",files="build.gradle[tags=use-and-configure-plugin]"]
include::sample[dir="snippets/plugins/customPlugin/kotlin/java-gradle-plugin",files="build.gradle.kts[tags=use-and-configure-plugin]"]
====

[[sec:creating_a_plugin_id]]
=== Creating a plugin id

Plugin ids are fully qualified in a manner similar to Java packages (i.e. a reverse domain name). This helps to avoid collisions and provides a way to group plugins with similar ownership.

Your plugin id should be a combination of components that reflect namespace (a reasonable pointer to you or your organization) and the name of the plugin it provides.
For example if you had a Github account named "foo" and your plugin was named "bar", a suitable plugin id might be `com.github.foo.bar`.
Similarly, if the plugin was developed at the baz organization, the plugin id might be `org.baz.bar`.

Plugin ids should conform to the following:

* May contain any alphanumeric character, '.', and '-'.
* Must contain at least one '.' character separating the namespace from the name of the plugin.
* Conventionally use a lowercase reverse domain name convention for the namespace.
* Conventionally use only lowercase characters in the name.
* `org.gradle` and `com.gradleware` namespaces may not be used.
* Cannot start or end with a '.' character.
* Cannot contain consecutive '.' characters (i.e. '..').

Although there are conventional similarities between plugin ids and package names, package names are generally more detailed than is necessary for a plugin id.
For instance, it might seem reasonable to add "gradle" as a component of your plugin id, but since plugin ids are only used for Gradle plugins, this would be superfluous.
Generally, a namespace that identifies ownership and a name are all that are needed for a good plugin id.

[[sec:publishing_your_plugin]]
=== Publishing your plugin

If you are publishing your plugin internally for use within your organization, you can publish it like any other code artifact.
See the <<publishing_ivy.adoc#publishing_ivy,Ivy>> and <<publishing_maven.adoc#publishing_maven,Maven>> chapters on publishing artifacts.

If you are interested in publishing your plugin to be used by the wider Gradle community, you can publish it to the http://plugins.gradle.org[Gradle Plugin Portal].
This site provides the ability to search for and gather information about plugins contributed by the Gradle community.
Please refer to the corresponding https://guides.gradle.org/publishing-plugins-to-gradle-plugin-portal/[guide] on how to make your plugin available on this site.

[[sec:using_your_plugin_in_another_project]]
=== Using your plugin in another project

To use a plugin in a build script, you need to configure the repository in `pluginManagement {}` block of the project's settings file.
The following example shows how you might do this when the plugin has been published to a local repository:

.Using a custom plugin in another project
====
include::sample[dir="snippets/plugins/customPlugin/groovy/consumer",files="settings.gradle[tags=use-plugin];build.gradle[tags=use-plugin]"]
include::sample[dir="snippets/plugins/customPlugin/kotlin/consumer",files="settings.gradle.kts[tags=use-plugin];build.gradle.kts[tags=use-plugin]"]
====

==== Note for plugins published without `java-gradle-plugin`
If your plugin was published without using the <<java_gradle_plugin.adoc#java_gradle_plugin,Java Gradle Plugin Development Plugin>>, the publication will be lacking <<plugins.adoc#sec:plugin_markers,Plugin Marker Artifact>>, which is needed for <<plugins.adoc#sec:plugins_block,plugins DSL>> to locate the plugin.
In this case, the recommended way to resolve the plugin in another project is to add a `resolutionStrategy` section to the `pluginManagement {}` block of the project's settings file as shown below.

.Resolution strategy for plugins without Plugin Marker Artifact
====
include::sample[dir="snippets/plugins/customPlugin/groovy/consumer",files="settings.gradle[tags=use-legacy-plugin]"]
include::sample[dir="snippets/plugins/customPlugin/kotlin/consumer",files="settings.gradle.kts[tags=use-legacy-plugin]"]
====

[[sec:precompiled_plugins]]
== Precompiled script plugins
In addition to plugins written as standalone projects, Gradle also allows you to provide build logic written in either Groovy or Kotlin DSLs as precompiled script plugins. You write these as `\*.gradle` files in `src/main/groovy`
directory or `*.gradle.kts` files in `src/main/kotlin` directory.

Precompiled script plugins are compiled into class files and packaged into a jar. For all intents and purposes, they are binary plugins and can be applied by plugin ID, tested and published as binary plugins. In fact, the plugin metadata for
them is generated using the <<java_gradle_plugin#java_gradle_plugin,Gradle Plugin Development Plugin>>.

[NOTE]
====
Kotlin DSL precompiled script plugins built with Gradle 6.0 cannot be used with earlier versions of Gradle.
This limitation will be lifted in a future version of Gradle.

Groovy DSL precompiled script plugins are available starting with Gradle 6.4.
====

[.multi-language-text.lang-groovy]
To apply a precompiled script plugin, you need to know its ID which is derived from the plugin script's
filename (minus the `.gradle` extension).

[.multi-language-text.lang-kotlin]
To apply a precompiled script plugin, you need to know its ID which is derived from the plugin script's
filename (minus the `.gradle.kts` extension) and its (optional) package declaration.

[.multi-language-text.lang-groovy]
For example, the script `src/main/groovy/java-library-convention.gradle` would have a plugin ID of `java-library-convention`.
Likewise, `src/main/groovy/my.java-library-convention.gradle` would result in a plugin ID of `my.java-library-convention`.

[.multi-language-text.lang-kotlin]
For example, the script `src/main/kotlin/java-library-convention.gradle.kts` would have a plugin ID of
`java-library-convention` (assuming it has no package declaration).
Likewise, `src/main/kotlin/my/java-library-convention.gradle.kts` would result in a plugin ID of
`my.java-library-convention` as long as it has a package declaration of `my`.

To demonstrate how you can implement and use a precompiled script plugin, let's walk through an example based on a `buildSrc` project.

[.multi-language-text.lang-groovy]
First, you need a `buildSrc/build.gradle` file that applies the `groovy-gradle-plugin` plugin:

[.multi-language-text.lang-kotlin]
First, you need a `buildSrc/build.gradle.kts` file that applies the `kotlin-dsl` plugin:

.Enabling precompiled script plugins
====
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/groovy",files="buildSrc/build.gradle[tags=apply]"]
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/kotlin",files="buildSrc/build.gradle.kts[tags=apply]"]
====

[.multi-language-text.lang-groovy]
We recommend that you also create a `buildSrc/settings.gradle` file, which you may leave empty.

[.multi-language-text.lang-kotlin]
We recommend that you also create a `buildSrc/settings.gradle.kts` file, which you may leave empty.

[.multi-language-text.lang-groovy]
Next, create a new `java-library-convention.gradle` file in the `buildSrc/src/main/groovy` directory and set its contents to the following:

[.multi-language-text.lang-kotlin]
Next, create a new `java-library-convention.gradle.kts` file in the `buildSrc/src/main/kotlin` directory and set its contents to the following:

.Creating a simple script plugin
====
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/groovy",files="buildSrc/src/main/groovy/java-library-convention.gradle[]"]
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/kotlin",files="buildSrc/src/main/kotlin/java-library-convention.gradle.kts[]"]
====

This script plugin simply applies the Java Library and Checkstyle Plugins and configures them.
Note that this will actually apply the plugins to the main project, i.e. the one that applies the precompiled script plugin.

Finally, apply the script plugin to the root project as follows:

.Applying the precompiled script plugin to the main project
====
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/groovy",files="build.gradle[]"]
include::sample[dir="snippets/plugins/precompiledScriptPlugins-inBuildSrc/kotlin",files="build.gradle.kts[]"]
====

=== Applying external plugins in precompiled script plugins
In order to apply an external plugin in a precompiled script plugin,
it has to be added to the plugin project's implementation classpath in the plugin's build file.
====
include::sample[dir="snippets/plugins/precompiledScriptPlugins-externalPlugins/groovy",files="buildSrc/build.gradle[]"]
include::sample[dir="snippets/plugins/precompiledScriptPlugins-externalPlugins/kotlin",files="buildSrc/build.gradle.kts[]"]
====

It can then be applied in the precompiled script plugin.
====
include::sample[dir="snippets/plugins/precompiledScriptPlugins-externalPlugins/groovy",files="buildSrc/src/main/groovy/my-plugin.gradle[]"]
include::sample[dir="snippets/plugins/precompiledScriptPlugins-externalPlugins/kotlin",files="buildSrc/src/main/kotlin/my-plugin.gradle.kts[]"]
====

The plugin version in this case is defined in the dependency declaration.

This limitation will be removed in future versions of Gradle.

[[sec:writing_tests_for_your_plugin]]
== Writing tests for your plugin

You can use the link:{javadocPath}/org/gradle/testfixtures/ProjectBuilder.html[ProjectBuilder] class to create link:{groovyDslPath}/org.gradle.api.Project.html[Project] instances to use when you test your plugin implementation.

=== Example: Testing a custom plugin

[source.multi-language-sample,java]
.src/test/java/org/gradle/GreetingPluginTest.java
----
include::{snippetsPath}/plugins/customPlugin/groovy/plugin/src/test/java/org/gradle/GreetingPluginTest.java[tag=test-plugin]
----

== More details

Plugins often also provide custom task types. Please see <<custom_tasks.adoc#custom_tasks,Developing Custom Gradle Task Types>> for more details.

Gradle provides a number of features that are helpful when developing Gradle types, including plugins. Please see <<custom_gradle_types.adoc#custom_gradle_types,Developing Custom Gradle Types>> for more details.

CAUTION: When developing Gradle Plugins, it is important to be cautious when logging information to the build log.
Logging sensitive information (e.g. credentials, tokens, certain environment variables) is <<logging.adoc#sec:debug_security,considered a security vulnerability>>.
Build logs for public Continuous Integration services are world-viewable and can expose this sensitive information.

== Behind the scenes

So how does Gradle find the link:{javadocPath}/org/gradle/api/Plugin.html[Plugin] implementation?
The answer is - you need to provide a properties file in the JAR's `META-INF/gradle-plugins` directory that matches the id of your plugin, which is handled by <<java_gradle_plugin.adoc#java_gradle_plugin,Java Gradle Plugin Development Plugin>>.

=== Example: Wiring for a custom plugin

[source,properties]
.src/main/resources/META-INF/gradle-plugins/org.samples.greeting.properties
----
include::{snippetsPath}/plugins/customPlugin/groovy/plugin/src/main/resources/META-INF/gradle-plugins/org.samples.greeting.properties[]
----

Notice that the properties filename matches the plugin id and is placed in the resources folder, and that the `implementation-class` property identifies the link:{javadocPath}/org/gradle/api/Plugin.html[Plugin] implementation class.
