首页 > 解决方案 > 具有自定义组 ID 的 Gradle 插件

问题描述

摇篮 6.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?还是我违反了插件定义与那个旧插件的合同?

标签: gradlegradle-plugin

解决方案


为了使用更新/首选plugins { }的 DSL,自定义插件必须发布插件标记工件

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

如果插件无法更新,那么您仍然可以使用该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}")
            }
        }
    }
}

有关更多详细信息,请参阅插件解析规则


推荐阅读