This message was deleted.
# community-support
s
This message was deleted.
v
It is not the "only" way, but a rather hacky way. Any usage of
ext
or
extra
is actually hacky and most often a work-around for not doing things properly. And also any usage of legacy script plugins (the things you use with "apply from" is a smell and should not be used. The idiomatic and recommended way to centralize and reuse build logic is to use convention plugins in
buildSrc
or an included build like
gradle/build-logic/
, for example implemented as precompiled script plugins.
a
If I'm reading this right, this seems to be for planting re-usable logic for use in monorepos or multi-build projects. But in my case, I just have the one project with a
build.gradle.kts
that's getting rather large. Is it not possible to just wrap some code in a function, put it in another .kts file, and import it into
build.gradle.kts
?
v
You are not reading it right 🙂 You can as well do it for single-project builds without problem. I also use that to split the build logic for such builds into multiple files.
And no, it is not really possible in a clean way to do that. A Kotlin function you define in a build script is only visible in that build script. Imagine the content of a build script as being inside a class, that's roughly what actually happens. So unless you use dirty hacks like storing the functions in
extra
properties, this is not possible. But using convention plugins is much cleaner and more maintainable and does not really have much overhead.
a
If you have a util function (or similar) that you want to extract from a build script you can move it to a
.kt
file in buildSrc and import it https://stackoverflow.com/questions/70670733/how-to-extend-the-gradle-kotlin-dsl-with-custom-extension-functions/71594449#71594449
v
Yeah, but by then, you can also just use a convention plugin 😄
a
Thanks for the input! I've been trying to spin something from it, but I'm getting some strange behaviour even though I think I'm following the docs to a tee. I now have a
<project-root>/buildSrc/src/main/kotlin/somepackage/EnvVarChecker.kt
with a function in it, which I'd like to call from
build.gradle.kts
. At the top of
build.gradle.kts
, I
import somepackage.EnvVarChecker
, and IntelliJ successfully finds the package and offers navigation, auto-complete etc. So IntelliJ gets it. But when I
gradle build
, I get
build.gradle.kts:2:8: Unresolved reference: somepackage
. So Gradle itself does not seem to "see" the package inside
buildSrc
. Any ideas?
Note: The
build.gradle.kts
in the picture is empty. The docs said
buildSrc
should have a file with that name in order to be recognized.
v
Unless I remember wrongly,
buildSrc
should just have
java-library
and
groovy
plugins applied by default. But I wonder that IntelliJ shows them as sources then. Maybe a bug.
a
Oh, I see what happened. IntelliJ immediately marked
buildSrc/src/main/kotlin
as "Sources root", which is why it provided autocomplete/navigation. I unmarked it as such, now IntelliJ also has no idea what EnvVarChecker is. Yay, I guess?
a
What plugins do you have in
buildSrc/build.gradle.kts
? Try adding
Copy code
plugins {
    `kotlin-dsl`
}
(but don't apply other Kotlin plugins, like
kotlin("jvm")
- Gradle might get confused)
v
embeddedKotlin("jvm")
should be fine as long as no precompiled script plugins are wanted.
Instead of
kotlin-dsl