https://kotlinlang.org logo
#gradle
Title
# gradle
l

Loïc Lamarque

12/13/2023, 9:37 PM
Hello, hope you are doing good. Does anybody knows if this is possible to share Kotlin code between build scripts and settings script ? For instance, I would like to share an enum of subprojects.
Copy code
// In file 'Subproject.kt'
fun Settings.include(subproject: Subproject): Unit = include("$subproject")
fun DependencyHandler.project(subproject: Subproject): ProjectDependency = project(":$subproject")
enum class Subproject {
    First,
    Second;

    override fun toString(): String = name.lowercase()
}

// In file 'settings.gradle.kts'
include(Subproject.First)
include(Subproject.Second)

// In file 'second/build.gradle.kts'
dependencies { implementation(project(Subproject.First)) }
j

Javier

12/13/2023, 10:44 PM
you can use
buildSrc
for that
buy I would enable
enableFeaturePreview("TYPESAFE_PROJECT_ACCESSORS")
so you can just do
Copy code
implementation(projects.subproject.first)
v

Vampire

12/14/2023, 2:05 AM
No he cannot, unless he uses a really ancient version of Gradle.
buildSrc
is not build before the settings script execution, but after. You can though have code in a project you add to the settings classpath either in the
buildscript
block or via a (possibly no-action) settings plugin that you apply, that can also come from an included build. Then those classes would also be available in the build scripts. But I agree with Javier that it might make much more sense to enable the project accessors instead.
2
l

Loïc Lamarque

12/14/2023, 10:58 AM
Interesting, thank you @Javier. I will check the type-safe project accessors. @Vampire Does the shared classes would be available in the settings script while using a settings plugin? By the way, I'm still using Gradle 7.5. 🙃
v

Vampire

12/14/2023, 11:20 AM
That's not ancient enough for using
buildSrc
in settings script. 🙂
Yes, if the classes are part of the jar of a settings plugin, then you can use the classes in the settings script, as long as the plugin jar is added to the settings script classpath, which easiest is achieved by applying it.
l

Loïc Lamarque

12/14/2023, 6:14 PM
@Vampire Do you have any resource on that topic please? I will make further research on that for learning how to do it. Because I tried to use code from the
buildSrc
in the settings script, but Gradle couldn't resolve it...
v

Vampire

12/14/2023, 6:50 PM
On which topic? I'm unsure what you want to have. As for
buildSrc
I said you cannot use anything from there in the settings script unless you are on an ancient Gradle version.
l

Loïc Lamarque

12/15/2023, 7:41 AM
@Vampire Ah! So should I focus on creating a settings plugin for doing so?
v

Vampire

12/15/2023, 7:52 AM
That's what I said, yes. A settings plugin that does no action is the easiest way to get code onto the settings classpath, especially if it is not a published artifact but coming from an included build.
l

Loïc Lamarque

12/15/2023, 8:16 AM
@Vampire What do you mean about "plugin that does no action"?
j

Javier

12/15/2023, 8:17 AM
an empty plugin implementation that the
apply
function does nothing
l

Loïc Lamarque

12/15/2023, 8:18 AM
Oh okay thank you @Javier. There is still something that I don't understand in the process: where should I put shareable code in the settings plugin if it does nothing?
v

Vampire

12/15/2023, 8:28 AM
Just in the same source set as the plugin. The plugin is just an easy way to bring the classes onto the class path
l

Loïc Lamarque

12/15/2023, 8:30 AM
@Vampire Simple as that?! 😮 I will try this out. Thank you for your time and consideration!
👌 1