I'm wondering if its possible to share a class acr...
# gradle
c
I'm wondering if its possible to share a class across build-modules and app modules? I have a module
build-logic
that is declared in my
settings.gradle
as
includeBuild("build-logic")
where I have written some convention plugins shared across my projects build files. I'd like to declare an enum class that I can reference in both my
build-logic
module as-well as within my projects application modules... Is that possible?
👀 1
v
Have another build that builds the enum, then include that build in your build logic build and your production build and add a dependency
c
okay, that makes sense in theory
v
Or you could use a symlink if you can.
c
so its possible to
includeBuild
multiple times with different modules?
in my root project gradle settings
e
if it's just an
enum
with no dependencies, you could cheat with adding it to multiple sourcesets
(might confuse the IDE though)
c
hmm yeah, its just an enum for now but want to try and future proof this incase it grows
e
that would at least be more portable than symlinking
go with a separate/included build, then
v
so its possible to
includeBuild
multiple times with different modules?
of course
e
keep in mind that it should use
embeddedKotlin
or older, for Gradle buildscript compatibility, even if your main project's Kotlin version is newer
👍🏼 1
c
got it, thanks for the pointers! I've got a path forward
👌 1
then include that build in your build logic build
@Vampire by this, do you mean by just a simple implementation in the dependencies?
or is there another way to add dependencies between builds?
v
You
includeBuild
that build in the settings script and declare a dependency that is then resolved by building the included build
Practically the same that is happening with your plugins in
build-logic
, just that you
includeBuild
outside
pluginManagement { ... }
for normal dependencies
👍🏼 1
v
If it will work please share the repo to peek 👀 :D
1
c
I absolutely do not recommend you do this, but this is a plugin that configures itself using its own source code: https://gitlab.com/opensavvy/automation/gradle-conventions/-/tree/main/plugin It works essentially like @ephemient said: there is a
meta-plugin
includeBuild
that includes the source code of the parent build, which is imported as a plugin into the parent build, which then declares the same source code again to finally declare the resulting plugin. Of course, this means the source code of the plugin is built twice. The IDE also warns that the same files are included in two source sets. However, everything works fine. Again, I really do not recommend you use this in general. In my case, this is a specific repository that only contains convention plugins, and I really didn't want to duplicate publishing logic into itself, so I accepted to have some weirdness.
Also, doing it this way, I didn't manage to get the
.kts
syntax to work, so it uses the plugin class syntax, but that's not a big problem