Question: I wanted to parse manually the file `gr...
# plugin-development
j
Question: I wanted to parse manually the file
gradle/libs.versions.toml
for my plugin refreshVersions But I noticed that there is version catalog API available in
build.gradle.kts
Copy code
// build.gradle.kts
val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
println("Library aliases: ${versionCatalog.libraryAliases}")
Exactly what I need! But if I put the same code in my plugin:
Copy code
open class RefreshVersionsMigrateTask : DefaultTask() {

    @TaskAction
    fun versionsCatalog() {
        val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
        println("Library aliases: ${versionCatalog.dependencyAliases}")
    }
}
Then I get:
Execution failed for task ':refreshVersionsMigrate'.
Extension of type 'VersionCatalogsExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension]
Is there a way to make it work or should I parse the file
libs.versions.toml
manually?
v
Besides that it is not the same code (
libraryAliases
vs.
dependencyAliases
), you just call it on the wrong object.
You look in the
extensions
of the
Task
which also is
ExtensionAware
but does not have the extension you are after and if you query
extensions
explicitly there is no delegation strategy.
Use
project.extensions
and it should work just fine.
Maybe someone should post a feature request to add the
ExtensionAware
instance to the error message, I also fell into that trap a few times already.
I don't think using the API you mention is going to help you because it represents a realized catalog. Typically won't give you where a version is defined.
j
It works, thanks a lot to you both!
I don't think using the API you mention is going to help you because it represents a realized catalog.
That's fine, I'm not modifying the file
libs.versions.toml
, I'm using its content to rewrite the files
build.gradle(.kts)
https://github.com/jmfayard/gradle-versions-catalog/commit/c9f9dfe12861ea409c06416fc2d25f150067e9fb
m
mmm, having the versions in a separate file kind of defeats the concept, no?
j
Generating the
libs.versions.toml
and updating the
build.gradle(.kts)
is half of the work of integrating refreshVersions and the versions catalog. The second half will be that refreshVersions add the available versions updates as comments in the
libs.versions.toml
directly, if the versions are present there.