Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply custom gradle plugin locally in the same project

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.

like image 389
Ben Avatar asked Oct 16 '22 18:10

Ben


1 Answers

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.

like image 158
Matias Bjarland Avatar answered Oct 21 '22 04:10

Matias Bjarland