'Compose gradle community plugins requiring a version in a binary plugin

I'm writing a binary plugin that's pushed as an artifact to a remote repository to be re-used. One of the things I want to accomplish with this plugin is to compose a set of additional plugins that should always be present. When composing official gradle plugins like the java plugin, that works fine. But I can't find the strategy for composing a community plugin requiring a version that would use this syntax in a build.gradle.kts file:

  plugins {
    id("com.diffplug.spotless") version "6.1.0"
}

All of the APIs I'm discovering in the gradle plugin library makes no reference of specifying a version, which makes me think I need to configure it elsewhere, like how a build can specify defaults in the pluginManagement block in settings.gradle.

This is a distilled version of how I'm trying to apply this.

import org.gradle.api.Plugin
import org.gradle.api.Project

class MyPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        // This works OK
        target.plugins.apply("java")

        // This is a community plugin, so it requires a version be set and fails
        target.plugins.apply("com.diffplug.spotless")
    }
}

Is there an API I'm missing? Or am I approaching this from the wrong direction?



Solution 1:[1]

Solved this by following aSemy's advice in a comment. I stopped trying to declare community plugins by ID and switched to applying the class of the plugin, after adding it to my plugin's classpath.

build.gradle.kts

dependencies {
    api("com.diffplug.spotless:spotless-plugin-gradle:6.3.0")
}

Plugin class:

import com.diffplug.gradle.spotless.SpotlessPlugin
import org.gradle.api.Plugin
import org.gradle.api.Project

class MyPlugin : Plugin<Project> {
    override fun apply(target: Project) {
        target.plugins.apply("java")
        target.plugins.apply(SpotlessPlugin::class.java)
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian