首页 > 解决方案 > 我怎样才能让 gradle 5 预编译脚本插件在包中时开始工作

问题描述

我想通过使用gradle 5.3.1 引入的预编译脚本插件build.gradle.kts功能来模块化我的文件。

当我直接有我的简单hello-world.gradle.kts文件时它工作正常buildSrc/src/main/kotlin

tasks.register("hello-world") { println("hello world") }

并将其包含在我的 main 的 plugins 部分中build.gradle.kts

plugins {
   `hello-world`
}

我现在可以使用gradle hello-world并查看预期的输出。

但是,当我将相同的脚本放入buildSrc/src/main/kotlin/custom/hello-world-custom.gradle.kts(添加package custom到脚本中)时,它会失败,尽管文档指出:

同样,只要 src/main/kotlin/my/java-library-convention.gradle.kts 有一个 my.java-library-convention 的包声明,它就会产生一个插件 ID 为 my.java-library-convention。

主要build.gradle.kts

plugins {
   `custom.hello-world-custom`
}

但相反,我收到一个错误:

 Script compilation error:

  Line 3:   `custom.hello-world-custom`
        ^ Unresolved reference: `custom.hello-world-custom`

任何想法如何解决这一问题?

更新:为了重现这一点,我创建了一个包含不同“hello world”任务的小型仓库。

标签: gradle

解决方案


从文档中不太清楚,但我找到了解决方案:

该包必须在反引号之外定义:

plugins {
 `hello-world`
 custom.`hello-world-custom`
}

推荐阅读