Catalog update

Versions of framework libraries, the plugin and datamodels are never edited by hand in a project. They move through two dependency catalogs, and two mechanisms keep every project on them: the release pipelines write into the catalogs, and updateKlokLibsToml copies them into the project.

Two catalogs

Aspect Platform catalog Tenant catalog
Repository klok/framework/klok-gradle-dependency-catalog, public <tenant>/framework/<tenant>-gradle-dependency-catalog, private
File on main klok-libs.versions.toml <tenant>-libs.versions.toml
Accessor in the build klokLibs tenantLibs
Copy in the project gradle/klok-libs.versions.toml gradle/tenant-libs.versions.toml
Content the framework, the plugin, the aligned third-party versions and klok's datamodels the tenant's own datamodels and libraries
Read with no token the project's GitLab token: klokGitAuthToken or GITLAB_TOKEN as PRIVATE-TOKEN, else CI_JOB_TOKEN as JOB-TOKEN
Missing or unreadable fails the build a warning; the build goes on

The tenant catalog also carries the tenant's agent skills under agents/, written by KlokDocumentationEgress; see Agent instructions. klok.dev's tenant Ensure creates the catalog from klok/environment/project-template-dependency-catalog, and it can be created by hand from the same template.

Tenant klok is the exception. Every klok repository is private and the platform catalog is the only public project on git.klok.dev, so klok's datamodels stay in the platform catalog under klokLibs. klok's private catalog is klok/framework/klok-tenant-dependency-catalog and carries agents/ only; a klok project gets the empty tenant catalog (below), and its settings.gradle is the same as every other tenant's.

The naming rule is one function, CatalogNaming.tenantCatalog(tenant), ported to KlokDocumentationEgress, klok.dev and klok-cli and driven by the shared fixture tenant-catalog-naming-fixtures.json (klok, acme, degerfors, klokdemo; Acme and an empty tenant are refused, never lower-cased). A tenant is the GitLab root group, a lower-case slug.

Declaring both catalogs

settings.gradle declares the two catalogs with two lines, and both files are committed:

dependencyResolutionManagement {
    versionCatalogs {
        klokLibs { from files('gradle/klok-libs.versions.toml') }
        tenantLibs { from files('gradle/tenant-libs.versions.toml') }
    }
}

from files(...) is a literal path and the file must exist, which decides two things. The tenant file has a fixed name: a tenant-named file (acme-libs.versions.toml) would force a tenant-specific settings.gradle into every project, while the accessor tenantLibs is fixed anyway. And a project commits an empty tenant catalog before its first refresh, so the block never fails: for tenant klok, for a tenant whose catalog does not exist yet and for a checkout without a token. The empty catalog is the four tables with nothing in them (KlokCatalogContract.EMPTY_TENANT_CATALOG), which is what the task writes when it has nothing to fetch:

# Klok tenant catalog commit: none (not refreshed yet)
# The tenant's version catalog (tenantLibs): the tenant's own datamodels and libraries.
# Written by ./gradlew updateKlokLibsToml from the tenant dependency catalog; never edit by hand.
[versions]

[libraries]

[bundles]

[plugins]

The integration template ships both lines and both files. A tenant's datamodel is then a catalog alias like any other, implementation tenantLibs.employeerecordV1, and klok's datamodels stay klokLibs.klokOdlV1. A datamodel of another tenant is an inline coordinate until it is needed more than once (the plugin registers one tenantLibs); reading it needs the consumer's root group on that catalog's job token allowlist, the same grant as for the jar.

Into the catalog: update_klok_catalog

After a successful publish, the shared CI job (klok-catalog-update.yml in build-ci-templates, included by the Java and datamodel templates) reads the build/klok-release.properties files the plugin wrote, maps the published artifacts to catalog keys through the catalog's [libraries] table (or the key referenced from [plugins] for the plugin itself), updates or adds the versions and pushes the change to the catalog's main with [skip ci]. The target follows the tenant: klok's releases go to the platform catalog, any other tenant's to its own catalog (Catalog update in the build-ci-templates documentation). The push uses the tenant's CI deploy key on the catalog it writes.

The plugin itself is on both sides of this. It also has gradle/klok-libs.versions.toml, and a task updateKlokLibsToml of the last released plugin, which its build_job runs before it compiles. Its module is named in [libraries] of the catalog, so klok catalog release-dependents starts its pipeline when something it declares is released. Why it can read a catalog it is in: Design decisions.

Out of the catalog: updateKlokLibsToml

./gradlew updateKlokLibsToml

The platform catalog

Downloads the catalog from klokLibsUpdate.url (default: GitLab's raw file API for klok-libs.versions.toml on the catalog's main, which needs no token) and replaces klokLibsUpdate.targetPath (default gradle/klok-libs.versions.toml), but only with a catalog the plugin can use:

  • the download has a 15 s connect and a 60 s read timeout, so a stalled connection fails the task instead of holding the job until its timeout;
  • the body is parsed before anything is replaced. It must have the [versions], [libraries], [bundles] and [plugins] tables, every version.ref and bundle member must resolve, and the aliases the plugin looks up by name must exist (KlokCatalogContract): plugins.gradle, the libraries quarkus-bom, quarkus-camel-bom and pulsarClient, and the bundles klokIntegrationImpl, klokApplicationImpl and klokDatamodelImpl. Otherwise the task fails naming what is wrong and the catalog commit, and the previous copy is left untouched;
  • the catalog commit GitLab served (X-Gitlab-Commit-Id) is written as the first line of the copy (# Klok dependency catalog commit: <sha> (<url>)) and into build/klok/catalog.properties, and every build logs it ([KlokPlugin] Klok dependency catalog commit: <sha>), so a build can always name the catalog it used.

When a project's copy lacks one of those aliases anyway (an old or hand edited copy), the build fails at configuration with a message naming the alias instead of a bare NoSuchElementException or a build without the framework. updateKlokLibsToml itself still runs in that state, so the copy can be refreshed.

The tenant catalog

The same run then fetches <tenant>-libs.versions.toml from the tenant catalog (klokLibsUpdate.tenantUrl, derived from the resolved tenant and the GitLab base URL) into klokLibsUpdate.tenantTargetPath (default gradle/tenant-libs.versions.toml), with the project's GitLab token. The tenant part never fails the build; a project must build before its tenant has a catalog, and a laptop without a token must still refresh the platform catalog. What happens instead:

Situation What the task does First line of the copy
no tenant is known writes the empty catalog, no HTTP call # Klok tenant catalog commit: none (no tenant known)
tenant klok writes the empty catalog, no HTTP call # Klok tenant catalog commit: none (tenant klok has no tenant libs catalog)
HTTP 404 warns no tenant catalog at <path>; create it with klok.dev's tenant Ensure; writes the empty catalog when the project has none, otherwise keeps the copy none (no tenant catalog at <path>) or unchanged
HTTP 401 or 403 warns with the token hint (klokGitAuthToken in ~/.gradle/gradle.properties or GITLAB_TOKEN); keeps the copy, or writes the empty catalog when there is none none (could not read <path>: HTTP 401) or unchanged
200, not usable warns naming the problems; the copy is untouched unchanged
200, usable replaces the copy # Klok tenant catalog commit: <sha> (<url>)

A tenant catalog is usable when it has the four tables and every version.ref and bundle member resolves (KlokCatalogContract.tenantProblems); empty tables are fine, and no alias is required, because the plugin looks nothing up in it by name.

build/klok/catalog.properties records both copies, and every build logs the tenant header next to the platform commit ([KlokPlugin] Klok tenant catalog commit: <sha> (<url>), or none (<reason>)):

commit=ad95c997…
url=https://git.klok.dev/api/v4/projects/klok%2Fframework%2Fklok-gradle-dependency-catalog/repository/files/klok-libs.versions.toml/raw?ref=main
tenant=acme
tenant.commit=0badf00d…
tenant.url=https://git.klok.dev/api/v4/projects/acme%2Fframework%2Facme-gradle-dependency-catalog/repository/files/acme-libs.versions.toml/raw?ref=main

Which tenant

klok.tenant when it names one (it already resolves klokTenant and KLOK_TENANT, and default-tenant does not count), else GitLab's CI_PROJECT_ROOT_NAMESPACE, else the root group of CI_PROJECT_PATH, else no tenant: the platform catalog only, with a warning. The value must be a lower-case slug; Acme is refused with a warning rather than lower-cased, because the catalog path is derived from it. The same resolution feeds updateKlokAgents.

When it runs

The Java build template runs the task in build_job for every project that has the platform copy and keeps both refreshed copies as artifacts, so the release publishes with the same versions; the datamodel template runs it before publishing a generated project. Developers run it locally when they want the latest framework build or the tenant's newest datamodel, and the templates ship copies that are refreshed the same way.

Because the plugin version is itself in the platform catalog, refreshing the file also moves a project to the newest plugin; Gradle picks it up on the next invocation.

Keys and naming

Artifact Catalog Catalog key
dev.klok.framework:klok-common platform klokCommon
dev.klok.framework:klok-brokerclient-java platform klokBrokerclient (alias klokBrokerclientJava)
dev.klok.framework:klok-quarkus-extension platform klokQuarkusExtension
dev.klok.framework:klok-gradle-plugin platform klokGradlePlugin (referenced from [plugins])
dev.klok.klok.datamodel:<name>-v<n> platform derived from the project name, KlokOdl_v1 becomes klokOdlV1
dev.klok.<tenant>.datamodel:<name>-v<n> the tenant's derived the same way, EmployeeRecord_v1 becomes employeeRecordV1

A datamodel that is not yet in its catalog is added with a derived key and a library alias; a project can override the key with the CI variable KLOK_CATALOG_VERSION_KEY.