5

I am in the process of migrating buildSrc convention plugins into standalone plugins. There are a lot of examples for creating Gradle plugins for Project objects, but a real dearth for Settings and Gradle. I want to centralize the list of repositories that we use in our gradle.settings.kts files, so I've created a Gradle Settings plugin: RepositoriesPlugin.

It is implemented in the same manner at a Project plugin, however, I am unsure about how to interpret the following quote from Gradle's docs: "A plugin can instead receive a parameter of type Settings, in which case the plugin can be applied in a settings script.", as stated in the documentation at this link: Gradle 7.2 Doc

The following example shows how I've applied my settings plugin in a settings.gradle.kts file. Is this how a settings plugin is applied, as per the documentation?

I've included the basic settings plugin code, below, too.

I'd appreciate your help to clarify that I am doing this properly. I am sure that this posting will help others who stray away from just Project plugins.

Thanks for your time and interest..

// settings.gradle.kts

pluginManagement {
    plugins {
        id("com.abitofhelp.gradle.plugins.repositoriesplugin") version "1.0.0-1"
    }

    settings.extensions
        getByType(RepositoriesPluginExtension::class).apply {
            localRepoName = "local-repo"
            localRepositoryPath = "../../local-repo"
        }

    // Set the plugin repositories for all projects.
    //repositories {
    //    maven { name = "localRepo"; url = uri(file("./local-repo")) }
    //    gradlePluginPortal()
    //    mavenCentral()
    //}
}
// repositoriesplugin.kt
open class RepositoriesPlugin: Plugin<Settings> {
    override fun apply(settings: Settings) {
        val extension: RepositoriesPluginExtension =
            settings.extensions.create("repositoriesPlugin", RepositoriesPluginExtension::class.java)

        val localRepositoryName = extension.localRepositoryName?.let { it }?: "../../localRepo"
        val localRepositoryPath = extension.localRepositoryPath?.let { it }?: "../../local-repo"

        settings.pluginManagement.repositories.apply {
            // Set the PLUGIN REPOSITORIES for all subprojects.
            maven { repository ->
                repository.name = localRepositoryName
                repository.url = URI.create(localRepositoryPath)
            }
            gradlePluginPortal()
            mavenCentral()
        }

        settings.dependencyResolutionManagement.repositories.apply {
            // Set the DEPENDENCY REPOSITORIES for all subprojects.
            maven {
                it.name = localRepositoryName
                it.url = URI.create(localRepositoryPath)
            }
            mavenCentral()
            gradlePluginPortal()
        }
    }
}
4
  • is your question how to use a plugin from local directory ? ? because all the path setup seems to be is for repository declaration.
    – PrasadU
    Sep 14, 2021 at 5:25
  • 1
    Hi Prasad, No, The issue is that I have created a Gradle Settings Plugin (not a Project Plugin), and there is no documentation about how to apply/use it with my builds. Basically, how do I use the Settings Plugin with my settings.gradle.kts file? Using Project Plugins are simple... :) Sep 15, 2021 at 6:50
  • but what custom behaviour do you need ? above it appears to add maven repo -- this is directly available via gradle settings spec docs.gradle.org/current/userguide/…
    – PrasadU
    Sep 15, 2021 at 8:31
  • Does the PluginAware documentation help? I think in any settings.gradle.kts you can activate a plugin with apply { plugin("my.settings.plugin.id") }. I think there's a dearth because while Plugins are commonly for Projects, they can be applied to any object. It just so happens that Projects have the plugins {} block - but that's a special case.
    – aSemy
    Nov 12, 2021 at 18:29

3 Answers 3

1

I just had to figure this out myself. You weren't far off with your initial attempt but the plugins block actually has to go after the pluginManagement block. The basic layout of my working implementation looks like this:

// settings.gradle.kts

rootProject.name = "myProject"

pluginManagement {
    repositories {
        maven {
            url = uri("/tmp/maven-local")
            gradlePluginPortal()
        }
    }
}

plugins {
    id("com.example.my-plugin") version "1.0-SNAPSHOT"
}
1

As Justin Warkentin already noted, you first need to add your repo in the pluginManagement section and then you can apply your plugin like any other.

If you want to bypass the repository, you can also add the plugin project as composite build via includeBuild in the pluginManagement section.

In my example I have combined both methods. If the plugin project exists locally, this one is used, otherwise the version from the repo is used. This way you can (temporarily) add your plugin project to develop on it and test it directly in including projects.

pluginManagement {
    repositories {
        // your plugin repo; maybe...
        mavenLocal()
    }

    // use local copy of your plugin if available,
    // otherwise load plugin from repo
    def name = 'your_plugin_project'
    if (file(name).isDirectory()) {
        includeBuild(name)
    }
}

plugins {
    // apply your plugin
    id 'your.plugin.id'
}
1
  • Didn't realise that I can do includeBuild inside pluginManagement itself. Saved my day
    – Vibin
    Mar 2 at 18:01
0

I believe you need to add the plugin to settings.gradle classpath via buildscript closure. pluginManagement.plugins makes the plugin availabile to the root (& sub) project build.gradle classpath, but I think that is a different classpath from the settings.gradle script.

To add to settings gradle, give this a try:

// settings.gradle
    
buildscript {
    repositories {
        maven { url "http://repo.where.plugin.is.published" }
    }

    classpath("complete.maven.coordinates:your-settings-plugin:version")
}

pluginManager.apply(your.settings.plugin.id) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.