未找到 ID 为 'id' 的插件(gradle 自定义插件)
Plugin with id 'id' not found (gradle custom plugin)
我在项目中创建了一个简单的 gradle 插件。根据文档,它应该没问题,但是当我尝试使用它时,我得到 Plugin with id 'show-date-plugin' not found.
我指的是 Doc:
https://docs.gradle.org/current/userguide/custom_plugins.html
You can put the source for the plugin in the
rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take
care of compiling and testing the plugin and making it available on
the classpath of the build script. The plugin is visible to every
build script used by the build. However, it is not visible outside the
build, and so you cannot reuse the plugin outside the build it is
defined in.
这是我的build.gradle
plugins {
id 'java'
id 'groovy'
}
group 'info.garagesalesapp'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
compile gradleApi()
compile localGroovy()
compile project(':json-display')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
apply plugin: 'project-report'
apply plugin: 'show-date-plugin'
这是项目中的插件位置:
那我做错了什么?
您没有按照文档进行操作。引用中的路径是 rootProject/buildSrc/src/main/groovy
,但您不使用 buildSrc
目录,而是将插件源代码包含到您的项目源代码中。由于这些源仅在执行特定 compileJava
/ compileGroovy
时构建,因此它们在构建脚本中不可用。
您可以将 buildSrc
目录视为一个简单的子项目,您也可以在那里创建一个 build.gradle
文件。如果不创建,将使用隐式构建文件内容。
似乎插件 ID 不能用于 buildSrc
插件。需要通过指定实现的全名(带包)来应用插件 class:
apply plugin: ShowDatePlugin
如果您的插件按预期工作,我建议将代码提取到一个单独的插件项目中以实现更好的可重用性。
我遇到了同样的错误,有时我们需要进一步阅读文档,因为它可能不太清楚。
我的问题是我没有添加 buildSrc/build.gradle 文件,所以找不到插件 ID:
buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}
文档中确实提到了这一点,但在其他部分中有所提及
最后,pre-compiled插件似乎不能应用在root build.gradle的allprojects
配置,所以我需要将其添加到每个模块构建文件中。
我在项目中创建了一个简单的 gradle 插件。根据文档,它应该没问题,但是当我尝试使用它时,我得到 Plugin with id 'show-date-plugin' not found.
我指的是 Doc:
https://docs.gradle.org/current/userguide/custom_plugins.html
You can put the source for the plugin in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling and testing the plugin and making it available on the classpath of the build script. The plugin is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the plugin outside the build it is defined in.
这是我的build.gradle
plugins {
id 'java'
id 'groovy'
}
group 'info.garagesalesapp'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.12'
compile gradleApi()
compile localGroovy()
compile project(':json-display')
testCompile group: 'junit', name: 'junit', version: '4.12'
}
apply plugin: 'project-report'
apply plugin: 'show-date-plugin'
这是项目中的插件位置:
那我做错了什么?
您没有按照文档进行操作。引用中的路径是 rootProject/buildSrc/src/main/groovy
,但您不使用 buildSrc
目录,而是将插件源代码包含到您的项目源代码中。由于这些源仅在执行特定 compileJava
/ compileGroovy
时构建,因此它们在构建脚本中不可用。
您可以将 buildSrc
目录视为一个简单的子项目,您也可以在那里创建一个 build.gradle
文件。如果不创建,将使用隐式构建文件内容。
似乎插件 ID 不能用于 buildSrc
插件。需要通过指定实现的全名(带包)来应用插件 class:
apply plugin: ShowDatePlugin
如果您的插件按预期工作,我建议将代码提取到一个单独的插件项目中以实现更好的可重用性。
我遇到了同样的错误,有时我们需要进一步阅读文档,因为它可能不太清楚。
我的问题是我没有添加 buildSrc/build.gradle 文件,所以找不到插件 ID:
buildSrc/build.gradle
plugins {
id 'groovy-gradle-plugin'
}
文档中确实提到了这一点,但在其他部分中有所提及
最后,pre-compiled插件似乎不能应用在root build.gradle的allprojects
配置,所以我需要将其添加到每个模块构建文件中。