简体   繁体   English

在同一个项目中本地应用自定义 gradle 插件

[英]Apply custom gradle plugin locally in the same project

I wrote a gradle plugin that I only want to use in my own project.我写了一个 gradle 插件,我只想在我自己的项目中使用。 Here's a simplified file structure:这是一个简化的文件结构:

/root project
  |
  + build.gradle
  + settings.gradle
  + build/
  + some module/
      |
      + build.gradle
      + src/
      + build/
      + plugin/
         |
         + build.gradle
         + src/
         + build/

I add the plugin to the module by referencing the jar file我通过引用 jar 文件将插件添加到模块中

// From '/some module/build.gradle'

buildscript {
  dependencies {
    classpath files('./plugin/build/libs/payload-plugin-0.0.1-SNAPSHOT.jar')
  }
}

apply plugin: 'my-custom-plugin'

It works fine at first, but there's a problem.起初它工作正常,但有一个问题。 If you clean the project and try to build it again, it fails, because the './plugin/build/libs/payload-plugin-0.0.1-SNAPSHOT.jar' no longer exists, and it will not try rebuilding the plugin module, as this counts as a configuration error.如果你清理项目并再次尝试构建它,它会失败,因为'./plugin/build/libs/payload-plugin-0.0.1-SNAPSHOT.jar'不再存在,它不会尝试重建插件模块,因为这算作配置错误。

I tried building the plugin only using gradlew :somemodule:plugin:build but it checks the buildscript of `:somemodule' first, so it does not work.我尝试仅使用gradlew :somemodule:plugin:build但它首先检查了 `:somemodule' 的gradlew :somemodule:plugin:build脚本,所以它不起作用。

Also tried classpath project(':somemodule:plugin') instead of jar reference, but it says that plugin is not found.还尝试了classpath project(':somemodule:plugin')而不是 jar 引用,但它说找不到插件。

Mixing the code for your gradle plugin with the project code the plugin is supposed to help build is probably a bad idea.将 gradle 插件的代码与该插件应该帮助构建的项目代码混合在一起可能是一个坏主意。

You should check out the gradle documentation on organizing your build logic and specifically I would have probably started by sticking your plugin source in the buildSrc directory .您应该查看有关组织构建逻辑的 gradle 文档,特别是我可能会首先将您的插件源粘贴到buildSrc 目录中

If you put your plugin source in a directory called buildSrc as per the documentation above, it will be automatically compiled and included in your build classpath.如果您按照上述文档将插件源代码放在名为buildSrc的目录中,它将被自动编译并包含在您的构建类路径中。 It will also not be deleted when you clean your project.当您清理项目时,它也不会被删除。 All you really need is the apply plugin: x statement.您真正需要的是apply plugin: x语句。

It should be noted that even though buildSrc is convenient, it is still there for the development phase of your plugin.应该注意的是,尽管buildSrc很方便,但它仍然存在于插件的开发阶段。 If you intend to use the plugin more often, in more places, or otherwise share it, it is probably good to actually build a jar with the plugin.如果您打算在更多地方更频繁地使用该插件,或者以其他方式共享它,那么实际使用该插件构建一个 jar 可能会很好。

It is an old question , but for the new visitors: you can build the plugin code while you build the code.这是一个老问题,但对于新访问者:您可以在构建代码的同时构建插件代码。 there can be even a parent project that build all the plugins.甚至可以有一个构建所有插件的父项目。 please check this answer请检查这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM