在同一个项目中本地应用定制gradle插件 [英] Apply custom gradle plugin locally in the same project

查看:183
本文介绍了在同一个项目中本地应用定制gradle插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个gradle插件,我只想在我自己的项目中使用。
这里有一个简化的文件结构:

$ $ $ $ $ $ $ $
+ build.gradle
+ settings.gradle
+ build /
+某些模块/
|
+ build.gradle
+ src /
+ build /
+ plugin /
|
+ build.gradle
+ src /
+ build /

通过引用jar文件将插件添加到模块中

  //从'/ some module / build.gradle'

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

应用插件:'my-custom-plugin'

它起初工作正常,但有一个问题。如果清理该项目并尝试重新构建它,则会失败,因为'./ plugin / build / libs / payload-plugin-0.0.1-SNAPSHOT.jar'不再存在,并且它不会尝试重建插件模块,因为这被视为配置错误。



我试着用 gradlew:somemodule:plugin:build 来构建插件,但是它检查`:buildscript'



还尝试了 classpath project(':somemodule:plugin')

解决方案

将您的gradle插件的代码与项目代码混合在一起该插件应该帮助构建可能是一个坏主意。



您应该查看组织你的构建逻辑,特别是我可能会开始将你的插件源放在 buildSrc目录

如果按照上述文档将插件源放在名为 buildSrc 的目录中,它将自动编译并包含在您的构建类路径中。在清理项目时它也不会被删除。你真正需要的是 apply插件:x 语句。

值得注意的是,尽管 buildSrc 很方便,但它仍然适用于您的插件的开发阶段。如果您打算更频繁地在更多地方使用插件,或者以其他方式共享插件,那么使用该插件实际构建jar可能是件好事。

I wrote a gradle plugin that I only want to use in my own project. 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

// 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.

I tried building the plugin only using gradlew :somemodule:plugin:build but it checks the buildscript of `:somemodule' first, so it does not work.

Also tried classpath project(':somemodule:plugin') instead of jar reference, but it says that plugin is not found.

解决方案

Mixing the code for your gradle plugin with the project code the plugin is supposed to help build is probably a bad idea.

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.

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. It will also not be deleted when you clean your project. All you really need is the apply plugin: x statement.

It should be noted that even though buildSrc is convenient, it is still there for the development phase of your plugin. 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.

这篇关于在同一个项目中本地应用定制gradle插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆