almost Making use of a third-party Gradle plugin as a composite plugin
will lid the newest and most present instruction almost the world. edit slowly consequently you comprehend effectively and appropriately. will deposit your information effectively and reliably
This submit reveals you wrap a third-party Gradle plugin in your personal plugin, so you’ll be able to work together with it programmatically. We’ll use Gradle composite builds to permit us to construct unexpectedly, lowering the construct, take a look at, and launch cycle.
Have you ever ever used a Gradle plugin and needed to do some tweaking to it, however don’t need the trouble of forking it or deploying one other model to rely upon? Carrying Gradle Composite Builds you’ll be able to create a Gradle plugin that your construct could rely upon. Then, inside that plugin, have it apply the third occasion plugin you wish to use/modify.
Word: A 3rd-party plugin means one developed individually from its repository. It doesn’t essentially imply one written by different individuals. Examples of such plugins might be ktlint, android gradle plugin, affected module detectorsomething you may have written your self or on your group/firm that’s in a gradle root.
What is a composite construction?
A composite construct is solely a construct that features different builds. In some ways, a composite construct is just like a Gradle multi-project construct, besides that as an alternative of together with solely
tasks
fullbuilds
are included.Composite builds permit you to:
– mix builds which are sometimes developed independently, for instance when testing a bug repair in a library your app makes use of– break a big construct of a number of tasks into smaller, remoted components that may be labored on independently or collectively as wanted
For this instance, we’re going to use the Affected Module Detector (AMD) Gradle plugin from mailbox. We’ll create our personal Gradle plugin, embody it as a composite construct, after which have that plugin apply the AMD plugin.
The end result will likely be that our multi-module mission will rely upon the AMD plugin and we can run AMD duties, whereas on the similar time with the ability to increase the habits of the AMD plugin nonetheless we would like as a result of it’s wrapped in our personal plugin.
All of the code for this submit is on the market at the repository here. One thing we’re not going to cowl is apply the AMD plugin in a typical Gradle manner, nonetheless that is lined within the repository and out there. in this branch. If you wish to go on to the composite resolution then it’s out there in this branch.
The very first thing to do whenever you wish to create a composite construct is to create the Gradle folder and construction. Our mission is a brand new primary Android mission from the Android Studio IDE clean template. It has an app module and we’ve got additionally added an android library module (mylibrary
) to make it multimodule. Giving it a folder construction like so:
/
construct.gradle
settings.gradle
app/
src/
construct.gradle
mylibrary/
src/
construct.gradle
We’re going to make our utility depending on a plugin that we created as a composite plugin. To create a composite plugin, begin with a folder that incorporates construct.gradle, settings.gradle, and the opposite common Gradle information. (You need to use the gradle init
command to create them, or copy them from a earlier mission). Along with the information within the Gradle root folder we simply mentioned, we added a subproject referred to as ‘plugin’ and this additionally has a construct.gradle file. Supplying you with an up to date folder construction like so:
/
amd-plugin/
gradle/
construct.gradle
gradlew
settings.gradle
plugin/
construct.gradle
src/
app/
src/
construct.gradle
mylibrary/
src/
construct.gradle
construct.gradle
settings.gradle
Reminder, you’ll be able to see the settings of this folder construction, here on GitHub.
filling /amd-plugin/settings.gradle
It appears like this:
pluginManagement // Declare the place we wish to discover plugin dependencies repositories gradlePluginPortal() google() mavenCentral() dependencyResolutionManagement // Declare the place we wish to discover code dependencies repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories google() mavenCentral() rootProject.identify = "blundell-amd-plugin" // Identify this mission embody(":plugin") // Guarantee our module (mission) is used
filling /amd-plugin/construct.gradle
It appears like this:
plugins // We wish to use Kotlin id("org.jetbrains.kotlin.jvm") model "1.7.21" apply false // We wish to have the ability to publish the plugin id("com.gradle.plugin-publish") model "1.1.0" apply false
filling /amd-plugin/plugin/construct.gradle
It appears like this:
plugins
id("java-gradle-plugin")
id("org.jetbrains.kotlin.jvm")
id("com.gradle.plugin-publish")
dependencies
testImplementation("junit:junit:4.13.2")
java
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
These 3 information (settings.gradle
2x construct.gradle
) make up the infrastructure of our new mission. Gradle ought to now have the ability to construct that mission efficiently, nonetheless it would not do something, so it is nonetheless not of a lot use.
Hopefully you’ll be able to discover that all the pieces beneath amd-plugin
appears like a standalone Gradle mission, and that is as a result of it’s. 🙂
Now we’re going to create a composite construct of our unique mission with this newly created one. After getting two Gradle tasks, it is a single line to mix them right into a composite construct. Right here we’re composing a plugin, so we embody it in pluginManagement
in the event you needed to compose supply code you’d put it with the everyday embody on the finish.
In your root mission settings.gradle
:
pluginManagement
repositories
gradlePluginPortal()
google()
mavenCentral()
includeBuild("amd-plugin") // This line permits our plugin mission to be included as a composite construct
dependencyResolutionManagement
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories
google()
mavenCentral()
rootProject.identify = "CompositePlugin"
embody ':app'
embody ':mylibrary'
As soon as you have declared the composite construct like this, it is best to have the ability to ‘sync’ your Android Studio IDE and the amd-plugin
it can begin exhibiting up as a multi-module mission that’s included.
We now have now linked two Gradle tasks collectively to construct them as a composite. The very last thing left is to fill our plugin to do one thing. Reminder; We’re going to wrap the Dropbox Affected Module Detector plugin, so we will enhance its performance.
To create a plugin, we have to declare to Gradle what our plugin is and the place it’s, this helps Gradle to create the container by which our plugin exists. This implies altering our /amd-plugin/plugin/construct.gradle
and including the gradlePlugin
closing:
gradlePlugin
plugins
blundAffectedModsPlugin
id = "com.blundell.amd.plugin"
implementationClass = "com.blundell.amd.BlundellAffectedModulesPlugin" // That is the totally certified identify and path to the plugin ( we are going to create subsequent )
Whereas we’re at this file, let’s add a dependency on the third-party (AMD) plugin to our dependencies
to dam:
dependencies implementation( "com.dropbox.affectedmoduledetector:affectedmoduledetector:0.2.0" ) testImplementation("junit:junit:4.13.2")
Now that we’ve got a dependency on the AMD plugin, we will entry its public API. We additionally declare our plugin class, create the corresponding supply code for that. Create a brand new file: amd-plugin/plugin/src/predominant/kotlin/com/blundell/amd/BlundellAffectedModulesPlugin.kt
BlundellAffectedModulesPlugin.kt
is our plugin, subsequently we lengthen from Gradle org.gradle.api.Plugin
:
package deal com.blundell.amd import com.dropbox.affectedmoduledetector.AffectedModuleConfiguration import com.dropbox.affectedmoduledetector.AffectedModuleDetectorPlugin import org.gradle.api.Plugin import org.gradle.api.Mission class BlundellAffectedModulesPlugin : Plugin<Mission> override enjoyable apply(mission: Mission) = mission.run mission.plugins.apply(AffectedModuleDetectorPlugin::class.java) pluginManager.withPlugin("com.dropbox.affectedmoduledetector") val config = rootProject.extensions.findByType(AffectedModuleConfiguration::class.java)!! config.logFolder = "$mission.buildDir/amd-output" config.logFilename = "output.log" logger.lifecycle("We will now work together with the plugin programmatically (as above).")
That is what this code does:
- We apply the AffectedModuleDetectorPlugin in order that anybody who applies this plugin applies the third occasion plugin
- With the AMD plugin utilized, we get the AMD plugin configuration and configure it to print logs to our
/construct
folder when working
As soon as the plugin is created, the very last thing we have to do is have our predominant
mission apply our BlundellAffectedModulesPlugin
in order that it may be used. That is carried out within the root of the principle mission. construct.gradle
:
buildscript ext compose_ui_version = '1.3.2' // Prime-level construct file the place you'll be able to add configuration choices widespread to all sub-projects/modules. plugins id 'com.android.utility' model '7.3.1' apply false id 'com.android.library' model '7.3.1' apply false id 'org.jetbrains.kotlin.android' model '1.6.10' apply false id 'com.blundell.amd.plugin' // This is applicable our composite plugin
And that’s! As soon as you have utilized the plugin, you’ll be able to sync your IDE to select up the newest modifications. Then in the event you run an AMD plugin command like:
./gradlew runAffectedUnitTests -Paffected_module_detector.allow
You will notice your personal plugin working and wrapping the third one.
Congratulations in your composite compilation. You may take this additional and create your own tasks so you’ll be able to have much more management of what runs and when, the being the main pointNow that you’ve got a composite construct, you’ll be able to programmatically work together with different plugins and have all that code in your IDE. Permitting you to debug in a single place issues that had been usually tough to trace throughout a number of tasks, and break your work down into smaller, remoted components.
I hope the article roughly Making use of a third-party Gradle plugin as a composite plugin
provides sharpness to you and is helpful for including collectively to your information