Apply custom gradle plugin locally in the same project
Asked Answered
P

2

6

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.

Pipsqueak answered 23/5, 2018 at 18:0 Comment(0)
A
4

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.

Acclaim answered 23/5, 2018 at 20:44 Comment(0)
S
0

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

Schelling answered 12/1, 2021 at 0:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.