Gradle 带有自定义组 ID 的插件

Gradle plugin with custom group id

Gradle6.1

我在 Gradle 中使用来自自定义存储库的自定义插件时遇到新插件配置模式的困难。

buildscript {
    repositories {
        maven {
            url = uri("https://custom")
        }
        mavenCentral()
        jcenter()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
}

plugins {
    java
    idea
    id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}

我收到这个错误:

Plugin [id: 'com.custom.gradle.plugin.myplugin', version: '1.1.0'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.custom.gradle.plugin.myplugin:com.custom.gradle.plugin.myplugin:1.1.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository

Gradle 将使用插件 ID 作为其组 ID。

如果我使用旧方法,它会起作用:

buildscript {
    repositories {
        maven {
            url = uri("https://custom")
        }
        mavenCentral()
        jcenter()
        maven {
            url = uri("https://plugins.gradle.org/m2/")
        }
    }
    dependencies {
        classpath("com.custom:com.custom.gradle.plugin.myplugin:1.1.0")
    }
}

apply(plugin = "com.custom.gradle.plugin.myplugin")

有没有办法用'id'命令指定组id?还是我违反了插件定义与旧插件的约定?

为了使用 newer/preferred plugins { } DSL, the custom plugin must publish a plugin marker artifact.

如果可以修改自定义插件,那么我建议更新以使用 Java Gradle Plugin Development plugin,这将为您创建标记。

如果插件无法更新,那么您仍然可以使用plugins { }块,但您需要手动解析插件:

主要build.gradle:

plugins {
    id("com.custom.gradle.plugin.myplugin") version "1.1.0"
}

然后在settings.gradle手动解析插件:

pluginManagement {
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.custom.gradle.plugin.myplugin") {
                useModule("com.custom:com.custom.gradle.plugin.myplugin:${requested.version}")
            }
        }
    }
}

参见插件解析规则 了解更多详情。