Home > Software design >  How to put custom Gradle plugins on an another projects build classpath?
How to put custom Gradle plugins on an another projects build classpath?

Time:07-19

I have a published gradle plugin which looks something like this:

|__root
|     |
|     |
|     |__java
|     |    |__SomeJavaClass.java
|     |    |__SomeJavaClass2.java
|     |
|     |__kotlin
|          |__MyPluginClass.kts
|          |__MyTaskClass.kts
|          |__Utils.kts   

I would like to include this plugin as a project in my multi project build instead of publishing it to a repo for easier developement.

This plugin has 3 Kotlin files Project A is using. MyPluginClass.kts has my own plugin class, MyTaskClass.kts has my own task class and Utils.kts contains only kotlin functions. The java classes are used in MyPlugin and MyTask.

It is being put on Project A's build.gradle.kts classpath as

classpath("com.my:custom.plugin:version")

A very simplified project structure I would like to achieve:

root
| 
|__Project A
|     |
|     |__build.gradle.kts
|     |__x.gradle.kts
|     |__y.gradle.kts
|     |__settings.gradle
|
|__Project build-logic
|     |
|     |__build.gradle.kts
|     |
|     |__java
|     |    |__SomeJavaClass.java
|     |    |__SomeJavaClass2.java
|     |
|     |__kotlin
|          |__MyPluginClass.gradle.kts
|          |__MyTaskClass.gradle.kts
|          |__Utils.gradle.kts   

I'm trying to create plugins from those kotlin files and include them in my main build because i need them precompiled, but i cant seem to find a way to put them on Project A's classpath when build.gradle is running there. Is it even possible? what would be the proper solution?

I'm using gradle 7.3

CodePudding user response:

You can combine multiple, independent, Gradle projects using composite builds. The same is also true for Gradle plugins.

Including a Gradle plugin from another directory is documented in the Testing Gradle plugins section.

So if you have a shared-build-logic project that provides a Gradle plugin with an ID my.shared-build-logic, and you want to use that in project-a.

my-projects/
├── shared-build-logic/
│   ├── src/
│   │   └── ...
│   ├── build.gradle.kts
│   └── settings.gradle.kts
└── project-a/
    ├── src/
    │   └── ...
    ├── build.gradle.kts
    └── settings.gradle.kts

Then you need to include shared-build-logic in your project-a Gradle settings file.

// my-projects/project-a/settings.gradle.kts

rootProject.name = "project-a"

pluginManagement {
  includeBuild("../shared-build-logic")
}

Now, so long as shared-build-logic correctly provides a Gradle plugin, in project-a you can reference that plugin.

// my-projects/project-a/settings.gradle.kts

plugins {
  id("my.shared-build-logic")
}

CodePudding user response:

Finnaly figured out. I didn't create plugins from build-logic, left them as kotlin files. Since it is a separate gradle project, i can just build the project and point project A's classpath to the produced jar file like

buildscript {
    dependencies {
        classpath(files("path.to.the.jar"))
    }
}

this way i can access methods and the MyTask from Project A.

The other issue was that i wanted to apply MyPlugin like

plugins{
    id("my-plugin-name")
}

which wasnt working, i guess because it is a class. But now since i had the whole project jar on my build classpath, i can do this:

apply<my-plugin-name>()
  • Related