Making use of a third-party Gradle plugin as a composite plugin | World Tech

kind of Making use of a third-party Gradle plugin as a composite plugin

will lid the most recent and most present help all over the world. proper to make use of slowly so that you perceive nicely and accurately. will accrual your information nicely and reliably

This submit exhibits you methods to wrap a third-party Gradle plugin in your individual plugin, so you possibly can work together with it programmatically. We’ll use Gradle composite builds to permit us to construct suddenly, decreasing the construct, check, and launch cycle.

Have you ever ever used a Gradle plugin and needed to do some tweaking to it, however don’t need the effort of forking it or deploying one other model to depend upon? With Gradle composite builds, you possibly can create a Gradle plugin that your construct can depend upon. Then, inside that plugin, have it apply the third social gathering 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 folks. Examples of such plugins might be ktlint, the Android Gradle plugin, the affected module detector, something you wrote your self or by your staff/firm that’s in a separate Gradle root.

What’s a composite building?

A composite construct is solely a construct that features different builds. In some ways, a composite construct is much like a Gradle multi-project construct, besides that as a substitute of together with solely tasksfull builds are included.

Composite builds can help you:
– mix builds which are usually 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 Dropbox. We’ll create our personal Gradle plugin, embody it as a composite construct, after which have that plugin apply the AMD plugin.

The consequence might be that our multi-module challenge will depend upon the AMD plugin and we can run AMD duties, whereas on the similar time having the ability to increase the habits of the AMD plugin nonetheless we wish as a result of it’s wrapped in our personal plugin.

All of the code for this submit is on the market within the repository right here. One thing we’re not going to cowl is methods to apply the AMD plugin in a typical Gradle manner, nonetheless that is coated within the repository and accessible on this department. If you wish to leap proper into the composite resolution, it is accessible on this department.

The very first thing to do while you wish to create a composite construct is to create the Gradle folder and construction. Our challenge is a brand new primary Android challenge from the Android Studio IDE clean template. It has an app module and we now have 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 challenge). 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. Providing 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 possibly can see the configuration of this folder construction, right here on GitHub.

filling /amd-plugin/settings.gradle It seems 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.title = "blundell-amd-plugin" // Identify this challenge

embody(":plugin") // Guarantee our module (challenge) is used

filling /amd-plugin/construct.gradle It seems like this:

plugins 
    // We wish to use Kotlin
    id("org.jetbrains.kotlin.jvm") model "1.7.21" apply false
    // We would like 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 seems 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.gradle2x construct.gradle) make up the infrastructure of our new challenge. Gradle ought to now be capable to construct that challenge efficiently, nonetheless it would not do something, so it is nonetheless not of a lot use.

Hopefully you possibly can discover that every part under amd-plugin seems like a standalone Gradle challenge, and that is as a result of it’s. 🙂

Now we’re going to create a composite construct of our authentic challenge with this newly created one. After you have 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 pluginManagementif you happen to needed to compose supply code you’ll put it with the everyday embody on the finish.

In your root challenge settings.gradle:

pluginManagement 
    repositories 
        gradlePluginPortal()
        google()
        mavenCentral()
    
    includeBuild("amd-plugin") // This line permits our plugin challenge to be included as a composite construct

dependencyResolutionManagement 
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories 
        google()
        mavenCentral()
    

rootProject.title = "CompositePlugin"
embody ':app'
embody ':mylibrary'

As soon as you’ve got declared the composite construct like this, it is best to be capable to ‘sync’ your Android Studio IDE and the amd-plugin it is going to begin displaying up as a multi-module challenge that’s included.

The synced challenge (Venture View) in Android Studio.

We’ve 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 are able to improve 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 through 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 title 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 now have a dependency on the AMD plugin, we are able to 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/essential/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.Venture

class BlundellAffectedModulesPlugin : Plugin<Venture> 

    override enjoyable apply(challenge: Venture) = challenge.run 
        challenge.plugins.apply(AffectedModuleDetectorPlugin::class.java)
        pluginManager.withPlugin("com.dropbox.affectedmoduledetector") 
            val config = rootProject.extensions.findByType(AffectedModuleConfiguration::class.java)!!
            config.logFolder = "$challenge.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 social gathering plugin
  • With the AMD plugin utilized, we get the AMD plugin configuration and configure it to print logs to our /construct folder when operating

As soon as the plugin is created, the very last thing we have to do is have our essential challenge apply our BlundellAffectedModulesPlugin in order that it may be used. That is performed within the root of the principle challenge. construct.gradle:

buildscript 
    ext 
        compose_ui_version = '1.3.2'
    

// Prime-level construct file the place you possibly can add configuration choices frequent 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’ve got utilized the plugin, you possibly can sync your IDE to select up the most recent adjustments. Then if you happen to run an AMD plugin command like:

./gradlew runAffectedUnitTests -Paffected_module_detector.allow

You will notice your individual plugin operating and wrapping the third one.

Congratulations in your composite compilation. You’ll be able to take this additional and create your individual duties so you possibly can have much more management of what will get executed and when, the principle level being, now that you’ve a composite construct, you possibly can programmatically work together with different plugins and have all that code in your SDI. 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 want the article virtually Making use of a third-party Gradle plugin as a composite plugin

provides acuteness to you and is beneficial for tallying to your information

Applying a third-party Gradle plugin as a composite plugin

Leave a Reply

x