【问题标题】:Custom Gradle Plugin ID not found未找到自定义 Gradle 插件 ID
【发布时间】:2012-09-16 04:49:39
【问题描述】:

我正在编写一个 Gradle 插件,但无法让 apply plugin: 命令在使用该插件的 Gradle 脚本中工作。我正在使用 Gradle 1.1。

我已经用clean build 构建了插件,我现在正试图通过一个平面仓库将它添加到 Gradle 构建中。这似乎有效,但 Gradle 没有发现有一个 ID 为 test-plugin 的插件。插件settings.gradle中的项目名称为test-pluginMETA-INF/gradle-plugins中的属性文件也为test-plugin.properties。我不确定在哪里可以指定插件 ID。

项目中使用test-pluginbuild.gradle文件:

repositories {
  flatDir name: 'libs', dirs: "../build/libs"
}

dependencies {
  compile 'test:test-plugin:0.1'
}

apply plugin: 'test-plugin'

来自 Gradle 的错误:

What went wrong:
A problem occurred evaluating root project 'tmp'.
Plugin with id 'test-plugin' not found.

【问题讨论】:

    标签: plugins build gradle


    【解决方案1】:

    插件 Jar 必须添加为 构建脚本依赖项

    buildscript {
        repositories { flatDir name: 'libs', dirs: "../build/libs" }
        dependencies { classpath 'test:test-plugin:0.1' }
    }
    
    apply plugin: "test-plugin"
    

    【讨论】:

    • 如何添加多个插件?
    • dependencies 块中添加每个插件的classpath,并为每个插件添加一个apply-plugin: "your-plugin" 行。
    • 如果在多项目中使用,请确保buildscript块出现在顶级项目的build.gradle而不是那些build .gradle 子项目
    【解决方案2】:

    如果你想在你的构建脚本中实现一个插件,那么你有两个选择。

    选项 1
    apply plugin: YourCustomPluginClassName

    选项 2

    plugins {
        id 'your.custom.plugin.id'
    }
    

    apply plugin: 用于通过类名指定插件时(例如apply plugin: JavaPlugin
    plugins { } 用于通过其 ID 指定插件时(例如 plugins { id 'java' }
    参考Gradle Plugins by tutorialspoint

    如果您选择选项 1,您的自定义插件将需要通过 3 种方式中的一种方式引入您的构建脚本。

    1. 您可以直接在 Gradle 构建脚本中对其进行编码。
    2. 您可以将其放在 buildSrc 下(例如 buildSrc/src/main/groovy/MyCustomPlugin)。
    3. 您可以在 buildscript 方法中将自定义插件作为 jar 导入。
      有关 buildscript 方法的信息,请参阅 Gradle Goodness by Mr. Haki

    如果您选择选项 2,那么您需要创建一个插件 ID。创建以下文件 buildSrc/src/main/resources/META-INF/gradle-plugins/[desired.plugin.id].properties

    implementation-class=package.namespace.YourCustomPluginClassName 复制并粘贴到您新创建的.properties 文件中。将 package.namespace.YourCustomPluginClassName 替换为所需插件类的完全限定类名。

    请参阅Custom Plugins by Gradle 了解更多信息。

    【讨论】:

    • 应用插件:YourCustomPluginClassName 似乎是一种已弃用的插件使用方式(2019)。
    • apply plugin:writing custom plugins 的 Gradle 教程的许多示例
    • 不是“implementation-class=desired.plugin.id”,而是“implementation-class=package.namespace.PluginClassName”
    • @OlgaPshenichnikova,据我所知,您是绝对正确的。我已经更新了答案以反映您的评论。好收获!
    【解决方案3】:

    我也遇到了同样的问题,没有找到自定义插件 ID。就我而言,我只是忘记添加“src/main/resources/META-INF/gradle-plugins”属性文件。属性文件的名称必须与带有“.properties”扩展名的插件 ID 的名称匹配。

    文件必须包含一行:

    implementation-class=(your fully qualified plugin classpath)
    

    这就是插件 id 如何解析为类名的完整机制。

    此外,插件需要作为依赖项添加,如上一个答案中所指出的那样。 android 文档声明您应该使用与您的唯一域名相关联的名称。即:名称 'test-plugin' 不是很好的形式,但像 'com.foo.gradle.test-plugin' 这样的 id 会更好。

    【讨论】:

      【解决方案4】:

      确保您的顶级build.gradle 使用正确的classpath 来引用构建的*.jar 文件的路径。

      一些插件,比如maven-publish,会构建jar并将其保存到mavenLocal中的特定位置,但路径可能看不清楚。

      您应该查看 jar 文件的文件路径,并确保它与您的 classpath 匹配,但映射并不立即明显:

      buildscript {
        dependencies {
          // This *MUST* match the local file path of the jar file in mavenLocal, which is:
          // ~/.m2/repository/com/company/product/plugin/product-gradle-plugin/1.0/product-gradle-plugin-1.0.jar
          classpath 'com.company.product.plugin:product-gradle-plugin:1.0'
        }
      }
      

      注意不要使用错误的类路径,它可以引用一个目录而不是实际的jar文件;像这样:

      buildscript {
        dependencies {
          // This is wrong, because it refers to the mavenLocal FOLDER "product-gradle-plugin",
          // instead of the jar FILE "product-gradle-plugin-1.0.jar"
          // However, a gradle sync will still resolve it as a valid classpath!!
          classpath 'com.company.product:product-gradle-plugin:1.0'
        }
      }
      

      更多信息:

      【讨论】:

        【解决方案5】:

        除了@Bonifacio2 写的内容之外,还有一条路径META-INF/gradle-plugins,并在IntelliJ 中显示为META-INF.gradle-plugins。不惜一切代价不要犯我直接将其创建为目录META-INF.gradle-plugins 的愚蠢错误,因为您基于另一个示例,并且永远不会工作。另一个技巧是从另一个 intelliJ 项目复制,因为这是添加的:gradle-plugins

        【讨论】:

          【解决方案6】:

          嗯,不妨试试;

              configure <org.jsonschema2pojo.gradle.JsonSchemaExtension> {
               this.sourceFiles = files("${project.rootDir}/schemas")
              }
          

          【讨论】:

          • 请避免仅发布代码答案并提供一些解释。
          猜你喜欢
          • 1970-01-01
          • 2021-10-23
          • 1970-01-01
          • 1970-01-01
          • 2020-05-03
          • 2019-02-14
          • 1970-01-01
          • 2016-04-30
          • 2018-03-10
          相关资源
          最近更新 更多