Skip to content

Commit 0d3a85a

Browse files
authored
KTOR-8162: Generate version catalogs (#4875)
* Add ktor-version-catalog generation * Move BOM publishing to ktorbuild.publish
1 parent cccf7af commit 0d3a85a

File tree

5 files changed

+121
-10
lines changed

5 files changed

+121
-10
lines changed

build-logic/src/main/kotlin/ktorbuild.publish.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ plugins.withId("ktorbuild.kmp") {
6666
configureJavadocArtifact()
6767
}
6868

69+
pluginManager.withPlugin("java-platform") {
70+
publishing.publications.register<MavenPublication>("maven") {
71+
from(components["javaPlatform"])
72+
}
73+
}
74+
75+
pluginManager.withPlugin("version-catalog") {
76+
publishing.publications.register<MavenPublication>("maven") {
77+
from(components["versionCatalog"])
78+
}
79+
}
80+
6981
private fun Project.configureSigning() {
7082
extra["signing.gnupg.keyName"] = (System.getenv("SIGN_KEY_ID") ?: return)
7183
extra["signing.gnupg.passphrase"] = (System.getenv("SIGN_KEY_PASSPHRASE") ?: return)

gradle/artifacts/publishJvmAndCommonPublications.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ io.ktor:ktor-utils-jvm/sources.jar
623623
io.ktor:ktor-utils/.jar
624624
io.ktor:ktor-utils/kotlin-tooling-metadata.json
625625
io.ktor:ktor-utils/sources.jar
626+
io.ktor:ktor-version-catalog/.toml
626627
io.ktor:ktor-websocket-serialization-jvm/.jar
627628
io.ktor:ktor-websocket-serialization-jvm/javadoc.jar
628629
io.ktor:ktor-websocket-serialization-jvm/sources.jar

ktor-bom/build.gradle.kts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ import ktorbuild.*
77
plugins {
88
id("ktorbuild.base")
99
id("java-platform")
10-
id("maven-publish")
10+
id("ktorbuild.publish")
1111
}
1212

13-
publishing.publications {
14-
create<MavenPublication>("maven") {
15-
from(components.findByName("javaPlatform"))
16-
}
17-
}
18-
19-
// Should be applied after publications were configured
20-
apply(plugin = "ktorbuild.publish")
21-
2213
val allPublications = projectsWithTag(ProjectTag.Library)
2314
.flatMapValue { libraryProject ->
2415
libraryProject.publishing.publications

ktor-version-catalog/build.gradle.kts

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2014-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
import ktorbuild.*
6+
7+
plugins {
8+
id("ktorbuild.base")
9+
id("version-catalog")
10+
id("ktorbuild.publish")
11+
}
12+
13+
val publishedProjectsProvider = projectsWithTag(ProjectTag.Published, Project::getName)
14+
15+
// A hack to prevent all projects evaluation if version catalog generation wasn't requested
16+
// Issue: https://github.com/gradle/gradle/issues/33568
17+
gradle.taskGraph.whenReady {
18+
if (allTasks.any { it.name == VersionCatalogPlugin.GENERATE_CATALOG_FILE_TASKNAME }) {
19+
catalog.versionCatalog {
20+
configureVersionCatalog(
21+
group = project.group.toString(),
22+
version = project.version.toString(),
23+
publishedProjects = publishedProjectsProvider.get().toSet(),
24+
)
25+
}
26+
}
27+
}
28+
29+
fun VersionCatalogBuilder.configureVersionCatalog(
30+
group: String,
31+
version: String,
32+
publishedProjects: Set<String>,
33+
) {
34+
// Versions
35+
val versionAlias = version("ktor", version)
36+
37+
// Libraries
38+
for (projectName in publishedProjects - excludedProjects) {
39+
if (projectName == "ktor-version-catalog") continue
40+
library(
41+
/* alias = */ libraryAlias(projectName, publishedProjects + hierarchyRoots),
42+
/* group = */ group,
43+
/* artifact = */ projectName,
44+
).versionRef(versionAlias)
45+
}
46+
library("gradlePlugin", "io.ktor.plugin", "plugin").versionRef(versionAlias)
47+
48+
// Plugins
49+
plugin("ktor", "io.ktor.plugin").versionRef(versionAlias)
50+
}
51+
52+
// Projects that are published but should be excluded from the version catalog.
53+
// This means we don't want our users to use these dependencies.
54+
val excludedProjects = setOf(
55+
"ktor-client",
56+
"ktor-server",
57+
"ktor-server-host-common",
58+
)
59+
60+
val hierarchyRoots = setOf(
61+
"ktor",
62+
"ktor-client",
63+
"ktor-server",
64+
)
65+
66+
// Handle special cases when automatic mapping doesn't work well
67+
val manualOverrides = mapOf(
68+
// We want to preserve the hierarchy in case we add more config formats
69+
"ktor-server-config-yaml" to "server-config-yaml",
70+
// Fix a typo in the project name
71+
"ktor-websocket-serialization" to "websockets-serialization",
72+
// Prefer '*-jakarta' artifacts over the deprecated ones
73+
"ktor-client-jetty-jakarta" to "client-jetty",
74+
"ktor-client-jetty" to "client-jetty-legacy",
75+
"ktor-server-jetty-jakarta" to "server-jetty",
76+
"ktor-server-jetty" to "server-jetty-legacy",
77+
"ktor-server-servlet-jakarta" to "server-servlet",
78+
"ktor-server-servlet" to "server-servlet-legacy",
79+
"ktor-server-tomcat-jakarta" to "server-tomcat",
80+
"ktor-server-tomcat" to "server-tomcat-legacy",
81+
)
82+
83+
/**
84+
* Generates a library alias for a given [projectName] to use in the version catalog.
85+
*
86+
* This function converts hierarchical project names into appropriately structured aliases.
87+
* For example, `ktor-client-content-negotiation` becomes `client-contentNegotiation`.
88+
*/
89+
fun libraryAlias(projectName: String, possiblePrefixes: Set<String>): String {
90+
if (projectName in manualOverrides) return manualOverrides.getValue(projectName)
91+
92+
var prefix = projectName.substringBeforeLast("-")
93+
var suffix = projectName.substringAfterLast("-")
94+
95+
while (prefix !in possiblePrefixes) {
96+
val lastWord = prefix.substringAfterLast("-")
97+
prefix = prefix.substringBeforeLast("-")
98+
suffix = lastWord + suffix.replaceFirstChar { it.uppercase() }
99+
}
100+
101+
return if (prefix != "ktor") {
102+
"${libraryAlias(prefix, possiblePrefixes)}-$suffix"
103+
} else {
104+
suffix
105+
}
106+
}

settings.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ projects {
186186
+"ktor-test-dispatcher"
187187
+"ktor-java-modules-test"
188188
+"ktor-dokka"
189+
+"ktor-version-catalog"
189190
}
190191

191192
// region Project hierarchy DSL

0 commit comments

Comments
 (0)