Apply custom gradle plugin locally in the same project

北战南征 提交于 2019-12-08 02:07:48

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/50494708/apply-custom-gradle-plugin-locally-in-the-same-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!