:wave: TL;DR Gradle Version Catalog use in buildSr...
# gradle
v
👋 TL;DR Gradle Version Catalog use in buildSrc
.kt
file doesn’t detect use - declarations within the
.toml
file are not reported as used. I’m trying to add use of version catalog with
.toml
file into my existing project, which uses buildSrc. When
libs
is accessed from any module
build.gradle.kts
if I “click-through” on it, it leads to the declaration in the
.toml
file, which is marked as used. But if it is accessed in buildSrc “click-through” leads to the
LibrariesForLibs
class, while in the
.toml
file it stays marked as unused. • I added the
libs.versions.toml
file with dependencies to
gradle
folder • I added the
/builsSrc/settings.gradle.kts
with
Copy code
dependencyResolutionManagement {
    versionCatalogs {
        create("libs") {
            from(files("../gradle/libs.versions.toml"))
        }
    }
}
• In
/builsSrc/build.gradle.kts
dependencies I added
Copy code
implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location))
This enables the use of
LibrariesForLibs
within the buildSrc with:
Copy code
val libs = the<LibrariesForLibs>()
(see the screenshots) Can it be made so that it marks it as used in the
.toml
file when used from extension in
.kt
in buildSrc?
I have also noticed that if the placed in (any) function, it is used and click through leads to toml file. Otherwise it leads to LibrariesForLibs, and stays unused in toml. See the screenshot.
v
You are using a really evil and dirty hack-around (by me) and then really expect the IDE to understand that this is using the version catalog from behind through the chest into the eye? o_O
I don't think this will ever be recognized as used in that case, and imho rightfully. 🙂
v
Thanks for the reply Bjorn. Can you please elaborate on “dirty hack-around”, or in other-words what should I stop using and why? I just started playing with catalogs and buildSrc.
v
It's Björn or Bjoern, not Bjorn. How many dirty hack-arounds invented by me do you use? 😄 I'm talking about the one used in what you showed. The one that makes the version catalog accessors available in
buildSrc
classes. The one you just replicated above in your unordered list. You don't need to stop using it, but if you use it, you should not wonder if for example things like usage detection in the IDE does not work as expected. The officially supported way to use the version catalog in cases where my hack-around is needed, is to use the
VersionCatalogsExtension
and its string-y API, instead of type-safe accessors.
v
OK, thanks Bjoern. I’ll try `VersionCatalogsExtension`s.
v
But just to be clear. That also will not mark the version catalog entries used. There is no connection from those source files to the version catalog file either way.
In your code you say "when I am applied I expect the project where I am applied to provide a version catalog with entry X and I want to have its value". This is true for the string-y API as well as my accessors hack-around. Both only work, because the project where the plugin is applied to indeed happens to have a version catalog with the expected entry. But from IDE PoV those entries are not directly used either way.
👌 1
gratitude thank you 1