Hi all. So we recently started publishing version...
# general
m
Hi all. So we recently started publishing version catalogs to replace our maven bom approach. I was wondering if there’s any good way to take a .toml file and resolve it against a particular group of repositories to make sure everything is resolvable. It would make a nice safeguard against publishing a catalog with bad dependencies in it.
v
Iirc in the meantime you can list the dependencies that are present in the version catalog, so you should be able to programmatically check it.
g
I'm sometimes producing java platform base on version catalog:
Copy code
fun DependencyHandler.derivePlatformFromCatalog(catalog: VersionCatalog) =
  derivePlatformFromCatalog(catalog) { "api" }

fun DependencyHandler.derivePlatformFromCatalog(catalog: VersionCatalog, classifier: (String) -> String): Unit = run {
  catalog.libraryAliases.filter { it.endsWith(".bom") }.forEach { alias ->
    val library = catalog.getLibrary(alias).get()
    if (library.versioned) add(classifier(alias), platform(library))
  }

  catalog.libraryAliases.filterNot { it.endsWith(".bom") }.forEach { alias ->
    constraints {
      val library = catalog.getLibrary(alias).get()
      if (library.versioned) add(classifier(alias), library)
    }
  }
}

private val MinimalExternalModuleDependency.versioned: Boolean
  get() = versionConstraint.requiredVersion.isNotBlank()
      || versionConstraint.strictVersion.isNotBlank()
      || versionConstraint.preferredVersion.isNotBlank()
      || versionConstraint.rejectedVersions.isNotEmpty()

fun VersionCatalog.getLibrary(alias: String): Provider<MinimalExternalModuleDependency> =
  findLibrary(alias).orElseThrow { NoSuchElementException("library $alias not found in catalog $name") }
To get version catalog itself just use
project.the<VersionCatalogExtension>().named("libs")
These code requires naming other platforms in catalog with
bom
as last segment and
javaPlaform.allowDependencies()
to import other BOMs/platforms into current
m
I think i found a bit simpler way:
Copy code
buildscript {
    dependencies {
        classpath("nl.littlerobots.vcu:plugin:0.3.1")
    }
}

val toml = file("gradle/libsExternal.versions.toml").inputStream().use {
    VersionCatalogParser().parse(it)
}

val deps = toml.libraries.values.map { library ->
    val versionObject = library.resolvedVersion(toml)
    val resolved = when (versionObject) {
        is nl.littlerobots.vcu.model.VersionDefinition.Simple -> {
            versionObject.version
        }
        else -> {
            versionObject.toString()
        }
    }

    "${library.group}:${library.name}:${resolved}"
}

dependencies {
    deps.forEach {
        implementation(it)
    }
}
and then just run the “depenencies” target
v
Basically what I said, just that you don't need to re-parse the toml, but can just list the already parsed one
m
yeah. this fails miserable. dependencies doesn’t fail if something can’t be found. but on top of that, most of what i’m searching for are “.aar” files which seem to not be automatically handled by gradle. Mind you i don’t need it to unpack anything, just find stuff.
g
You can always create an resolvable configuration and iterate it. It will force gradle to fetch all dependencies (including transitive by default). My example was for similar case but without resolution since I produce platform/bom with dependency constraints.
m
@grossws I ended up creating another project that sets up the android library plugin. Without that, i'm having trouble resolving .aar files. I'm open to some other way to get the dependency manager to find .aar files, but i wasn't able to figure that out. It just looks for .jar dependencies. And since my version catalog is android specific, i don't want to have @aar all over the place in there.