The Gradle build system- Tutorial
5 stars based on
60 reviews
A Gradle plugin packages bitcoin gradle plugin 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 bitcoin gradle plugin, provided the implementation ends up compiled as bytecode.
In our examples, we are going to use Groovy as the implementation language. Groovy, Java or Kotlin are all good choices as the language to use to implement a plugin, as the Gradle API has been designed to work well with these languages. In general, a plugin implemented using Java or Kotlin, which are statically typed, will perform better than the same plugin implemented using Groovy.
You can include the source for the plugin directly in the build script. This has the benefit that the plugin is bitcoin gradle plugin 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.
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 bitcoin gradle plugin the build, and so you cannot reuse the plugin outside the build it is defined in. See Organizing Build Logic for more details about the buildSrc 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 bitcoin gradle plugin the plugin in the build script, to keep things simple. Then we will look at creating a standalone project. To create a Gradle plugin, you need to write a class that implements the Plugin interface.
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.
One thing to note is that a new instance of a bitcoin gradle plugin is created for each project it is applied to. Also note that the Plugin class is a generic type. This example has it receiving the Project type as a type parameter. A plugin can instead receive a parameter of type Settingsin which case the plugin can be applied in a settings script, or a parameter of type Gradlein which case the plugin can be applied in bitcoin gradle plugin initialization script.
Most plugins need to obtain some configuration from the build script. One method for doing this is to use extension objects. The Gradle Project has an associated 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 a Java Bean compliant class. Groovy is a good language choice to implement an extension object because plain old Groovy objects contain all the getter and setter methods that a Java Bean requires.
Java and Kotlin are other good choices. Here we add a greeting extension object to the project, which allows you to configure the greeting. In this example, GreetingPluginExtension is a plain old Groovy object with a property bitcoin gradle plugin message. The extension object is added to the plugin list 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 closure block for each extension object, so you can group settings together. The following example shows you how this works. 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.
To do this, you can leverage the Project. Object method to resolve values to files as late as possible. In this example, we configure the greet task destination property as a closure, which is evaluated with the Project. Object method to turn the return value of the closure into a File object at the last minute. You will notice that in the example above we specify the bitcoin gradle plugin 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. The end user only interacts with the exposed DSL defined by the extension.
The imperative logic is hidden in the plugin implementation. To avoid evaluation order issues, the actual value of a mapped property bitcoin gradle plugin to be resolved during the execution phase. Refer to Lazy Configuration for more information. The following demonstrates the usage of the type for mapping an extension property to a task property:. Now we will move our plugin to a standalone project, so we can publish it and share it with others. This project is simply a Groovy project that produces a JAR containing the plugin classes.
Here is a simple build script for the project. So how does Gradle find bitcoin gradle plugin Plugin implementation? Notice that the properties filename matches the plugin id and is placed in the resources folder, and that the implementation-class property identifies the Plugin implementation class. Plugin ids are fully bitcoin gradle plugin in a manner similar to Java packages i. 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 bitcoin gradle plugin a reasonable pointer bitcoin gradle plugin you or your organization and the name of the plugin it provides. For example if you had a Github account named "foo" and your bitcoin gradle plugin was named "bar", a suitable plugin id might be com.
Similarly, bitcoin gradle plugin the plugin was developed at the baz organization, the plugin id might be org. Although there are conventional similarities between plugin ids and package names, package names are generally bitcoin gradle plugin 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 bitcoin gradle plugin, this would be superfluous.
Generally, a namespace that identifies ownership and a name are all bitcoin gradle plugin are needed for a good plugin id. If you are publishing your plugin internally for use within your organization, you can publish it like any other code artifact. See the ivy and 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 Gradle plugin portal.
This site provides the ability to search for and gather information about plugins contributed by bitcoin gradle plugin Gradle bitcoin gradle plugin. See the instructions here bitcoin gradle plugin how to make your plugin available on this site. The following example shows how you might do this when the JAR containing the plugin has been published to a local repository:.
You can use the ProjectBuilder class to create Project instances to use when you test your plugin implementation. You can use the incubating Java Gradle Plugin development plugin to eliminate some of the boilerplate declarations in your build script and provide some basic validations of plugin metadata. This plugin will automatically bitcoin gradle plugin the Java pluginadd the gradleApi dependency to the compile configuration, and perform plugin metadata validations as part of the jar task execution.
As we saw above, you can use an extension object to provide configuration for your plugin. An extension object is simply a regular object, and so you can provide DSL elements nested inside this block by adding properties and methods to the extension object. When Gradle creates a task or extension object, Gradle decorates the implementation class to mix in DSL bitcoin gradle plugin. To create a nested DSL element you can use the ObjectFactory type to create objects that are similarly decorated.
The constructor uses this to create a nested object and makes this object available to the DSL through the greeter property. Gradle provides some utility classes for maintaining collections of objects, intended to work well with the Gradle DSL.
Class methods create instances of NamedDomainObjectContainerthat have many useful methods for managing and configuring the objects. In order to use a type with any of the project. See the above link for project. Table of Contents Packaging a plugin Writing a simple plugin Making the plugin configurable Working with files in custom tasks and plugins Mapping extension properties to task properties A standalone project Providing a configuration DSL for the plugin. Build script You can include the source for the plugin directly in bitcoin gradle plugin build script.
Standalone project You can bitcoin gradle plugin a separate project for your plugin. Writing a simple plugin. A custom plugin build. Making the plugin configurable. A custom plugin extension build. A custom plugin with configuration closure build.
Working with files in custom tasks and plugins. Evaluating file properties lazily build. Mapping extension properties to task properties. Mapping extension properties to task properties build. A build for a custom plugin build. Creating a plugin id.