Gradle Plugin Concepts: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
Line 9: Line 9:
=Overview=
=Overview=


A Gradle plugin is packaged code that uses the Gradle API to provide additional functionality and extend Gradle core. A Gradle plugin applies some configuration to a target object, usually a [[Gradle_Project|project]]. The plugin may introduce new concepts, [[Gradle_Task#Overview|tasks]], new domain objects, conventions, project layouts and patterns for a specific problem domain. Plugins may even extend the core objects. Introducing their own conventions, plugins are "opinionated", in that they encourage the user to do things in a certain way. However, well written plugins must provide means to change the default conventions and make it work for non-standard projects.
A Gradle plugin is packaged code that uses the Gradle API to provide additional functionality and extend Gradle core. A Gradle plugin applies some configuration to a target object, usually a [[Gradle_Project|project]]. The plugin may introduce new concepts, capabilities, [[Gradle_Task#Overview|tasks]], new domain objects, conventions, project layouts and patterns for a specific problem domain. Plugins may even extend the core objects. Introducing their own conventions, plugins are "opinionated", in that they encourage the user to do things in a certain way. However, well written plugins must provide means to change the default conventions and make it work for non-standard projects.


=Plugin API=
=Plugin API=

Latest revision as of 06:44, 28 March 2021

External

Internal

TODO

Gradle Plugins TODEPLETE

Overview

A Gradle plugin is packaged code that uses the Gradle API to provide additional functionality and extend Gradle core. A Gradle plugin applies some configuration to a target object, usually a project. The plugin may introduce new concepts, capabilities, tasks, new domain objects, conventions, project layouts and patterns for a specific problem domain. Plugins may even extend the core objects. Introducing their own conventions, plugins are "opinionated", in that they encourage the user to do things in a certain way. However, well written plugins must provide means to change the default conventions and make it work for non-standard projects.

Plugin API

org.gradle.api.Plugin<T>

Script Plugin

https://docs.gradle.org/current/userguide/plugins.html#sec:script_plugins

A script plugin is simply a file containing Gradle DSL code and possibly in-line Java and Groovy code. The syntax is no different than build.gradle syntax. It can be thought as just another modular build script to be imported into a build script.

apply from: 'example.gradle'

Conventionally, the file has the ".gradle" extension, but this is not mandatory. The file can be loaded from the local filesystem, or from a remote location. Filesystem location is relative to the project directory. Remote locations are specified with HTTP URLs.

Script plugins can declare complex Java or Groovy code, including enhanced task definitions. If such code relies on external libraries, the dependency on those libraries can be declared as such:

buildscript {
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.dependency:my-dep:1.0.0'
  }
}

Binary Plugin

A binary plugin is a class implementing org.gradle.api.Plugin and ancillary classes.

Using a Plugin

Plugins can be applied in two ways, via the apply method or via the plugins script block.

apply() Method

apply() is a method of the PluginAware interface that is inherited by Project, Settings and Gradle. apply DSL keyword is available in build.gradle.

apply plugin: 'java'

plugins Script Block

The 'plugins' script block is exposed by org.gradle.plugin.use.PluginDependenciesSpec, which is the specialized DSL to declare plugins to use in a script.

plugins {
  id 'java'
}

Core Gradle plugin can be declared using their ID.

Plugin Extension

A plugin is a typical use case for an extension.

Core Plugins

https://docs.gradle.org/current/userguide/plugin_reference.html

A core plugin ships with the Gradle runtime. It is also referred to as a "standard" plugin, and it uses the org.gradle namespace. The core plugin exposes a series of predefined task types. Gradle ships with the following core plugins:

Base

Base Plugin

Java

Java Plugin

Java Library

Java Library Plugin

Java Platform

Java Platform Plugin

Groovy

Groovy Plugin

Plugin Libraries

External Plugins

Writing Custom Plugins

Custom Binary Plugins

Plugin Operations

Listing Tasks introduced by a Certain Plugin

There is no know command line support for this operation. It can be coded as such:

def tasksBefore = [], tasksAfter = []    
project.tasks.each { tasksBefore.add(it.name) }
// apply plugin
apply(plugin: 'java') // apply plugin
project.tasks.each { tasksAfter.add(it.name) }
tasksAfter.removeAll(tasksBefore);
println 'tasks introduced by plugin' + tasksAfter;

Also see:

Gradle Task | Tasks and Plugins