简体   繁体   English

编写 gradle 需要二进制插件版本的社区插件

[英]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.

相关问题 自定义 Gradle 插件 - 共享数据 beetwen 插件 - Custom Gradle plugin - sharing data beetwen plugins org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'org.jetbrains.kotlin.android', version: '1.6.21', apply: false] - org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'org.jetbrains.kotlin.android', version: '1.6.21', apply: false] Gradle 插件不得包含版本 - Gradle plugin must not include version 使用插件 dsl 语法应用 hibernate-gradle-plugin? - Apply hibernate-gradle-plugin using plugins dsl syntax? gradle插件的版本管理如何封装? - How do I encapsulate version management for gradle plugins? 如何使用 Kotlin 将导入的插件合并到 Gradle 中的自定义插件 - How to consolidate imported plugins to custom plugin in Gradle using Kotlin Maven-publish gradle插件会跳过该版本 - Maven-publish gradle plugin skips the version 如何在 gradle 中强制降级插件版本? - How to force downgrade plugin version in in gradle? 使用不兼容版本的 Kotlin 编译模块。 其元数据的二进制版本是 1.7.1,预期版本是 1.5.1 撰写中的错误 - Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.7.1, expected version is 1.5.1 error in compose 错误:Android Gradle插件仅支持Kotlin Gradle插件版本1.3.0及更高版本 - ERROR: The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM