[英]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.在编写官方 gradle 插件时,如 java 插件,效果很好。 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:但是我找不到编写社区插件的策略,该插件需要一个在 build.gradle.kts 文件中使用此语法的版本:
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.我在 gradle 插件库中发现的所有 API 都没有提及指定版本,这让我觉得我需要在其他地方配置它,比如构建如何在 settings.gradle 的pluginManagement
块中指定默认值。
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?有没有我缺少的 API? Or am I approaching this from the wrong direction?或者我是从错误的方向接近这个?
Solved this by following aSemy's advice in a comment.通过在评论中遵循 aSemy 的建议解决了这个问题。 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.在将它添加到我的插件的类路径之后,我停止尝试通过 ID 声明社区插件并切换到应用插件的 class。
build.gradle.kts build.gradle.kts
dependencies {
api("com.diffplug.spotless:spotless-plugin-gradle:6.3.0")
}
Plugin class:插件 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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.