Skip to content

Commit 5c15858

Browse files
committed
build: style-gate build-logic with ktlint, pin its toolchain, and guard the publication on the kotlin-jvm plugin
1 parent 0263f7f commit 5c15858

2 files changed

Lines changed: 77 additions & 38 deletions

File tree

build-logic/build.gradle.kts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,37 @@
1212
// classpath.
1313
plugins {
1414
`kotlin-dsl`
15+
// Style-gate this included build's own scripts. `build-logic` is a separate build with its
16+
// own settings, so the root build's `subprojects { ktlint }` block does not reach it; without
17+
// this the convention-plugin `.kts` files would escape the repository's Kotlin style checks.
18+
// The version is pinned literally (not via the version catalog) to keep this build catalog-free
19+
// — see the rationale in `settings.gradle.kts`; it must match `ktlint-plugin` in
20+
// `gradle/libs.versions.toml`.
21+
id("org.jlleitschuh.gradle.ktlint") version "12.1.1"
1522
}
1623

1724
repositories {
1825
mavenCentral()
1926
gradlePluginPortal()
2027
}
28+
29+
// Pin the toolchain so this included build compiles reproducibly regardless of the JDK running
30+
// the Gradle daemon. This is plugin code for the build JVM, not shipped bytecode, so the version
31+
// only needs to be recent enough for `kotlin-dsl`.
32+
kotlin {
33+
jvmToolchain(21)
34+
}
35+
36+
ktlint {
37+
ignoreFailures.set(false)
38+
}
39+
40+
// `kotlin-dsl` adds its plugin wrappers and DSL accessors (under build/generated-sources) to the
41+
// `main` Kotlin source set, so the source-set ktlint tasks would otherwise lint tool-generated
42+
// code. Drop the generated tree from those tasks' inputs; the hand-written convention scripts under
43+
// src/ — and the build script via `runKtlintCheckOverKotlinScripts` — are still checked.
44+
val generatedSourcesDir = layout.buildDirectory.dir("generated-sources").get().asFile
45+
tasks.withType<org.jlleitschuh.gradle.ktlint.tasks.BaseKtLintCheckTask>().configureEach {
46+
val handWritten = source.filter { file -> !file.startsWith(generatedSourcesDir) }.files
47+
setSource(handWritten)
48+
}

build-logic/src/main/kotlin/dexpace.published-module.gradle.kts

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,54 +23,65 @@ plugins {
2323

2424
// Coordinates. The group and version are the same for every published module and match the
2525
// values declared on the root project; keeping the single literal here makes a coordinate
26-
// change a one-file edit instead of a nine-file edit.
26+
// change a one-file edit instead of a nine-file edit. The root `build.gradle.kts` still declares
27+
// its own `group`/`version`, so a version bump must update both literals until they are sourced
28+
// from one place (e.g. a `gradle.properties` entry read by both).
2729
group = "org.dexpace"
2830
version = "0.0.1-alpha.1"
2931

30-
publishing {
31-
publications {
32-
create<MavenPublication>("library") {
33-
from(components["java"])
34-
pom {
35-
name.set(project.name)
36-
description.set("Dexpace Java SDK — ${project.name}")
37-
url.set("https://github.com/dexpace/java-sdk")
38-
licenses {
39-
license {
40-
name.set("MIT License")
41-
url.set("https://github.com/dexpace/java-sdk/blob/main/LICENSE")
42-
distribution.set("repo")
32+
// The `library` publication is built from the `java` software component, which only exists once a
33+
// `java`/`java-library`/`kotlin("jvm")` plugin is applied. Every current consumer applies
34+
// `kotlin("jvm")`, but this plugin neither applies nor requires one, so the whole publication +
35+
// signing setup is guarded on the Kotlin JVM plugin. Without the guard, a module that opted in
36+
// without a Java/Kotlin plugin would fail with an opaque "SoftwareComponent with name java not
37+
// found".
38+
pluginManager.withPlugin("org.jetbrains.kotlin.jvm") {
39+
publishing {
40+
publications {
41+
create<MavenPublication>("library") {
42+
from(components["java"])
43+
pom {
44+
name.set(project.name)
45+
description.set("Dexpace Java SDK — ${project.name}")
46+
url.set("https://github.com/dexpace/java-sdk")
47+
licenses {
48+
license {
49+
name.set("MIT License")
50+
url.set("https://github.com/dexpace/java-sdk/blob/main/LICENSE")
51+
distribution.set("repo")
52+
}
4353
}
44-
}
45-
developers {
46-
developer {
47-
id.set("dexpace")
48-
name.set("Dexpace SDK Team")
54+
developers {
55+
developer {
56+
id.set("dexpace")
57+
name.set("Dexpace SDK Team")
58+
}
59+
}
60+
scm {
61+
connection.set("scm:git:https://github.com/dexpace/java-sdk.git")
62+
developerConnection.set("scm:git:ssh://github.com/dexpace/java-sdk.git")
63+
url.set("https://github.com/dexpace/java-sdk")
4964
}
50-
}
51-
scm {
52-
connection.set("scm:git:https://github.com/dexpace/java-sdk.git")
53-
developerConnection.set("scm:git:ssh://github.com/dexpace/java-sdk.git")
54-
url.set("https://github.com/dexpace/java-sdk")
5565
}
5666
}
5767
}
58-
}
59-
repositories {
60-
// Local staging repository. CI must override this to publish to a real remote.
61-
maven {
62-
name = "local"
63-
url = uri(rootProject.layout.buildDirectory.dir("staging-repo"))
68+
repositories {
69+
// Local staging repository. CI must override this to publish to a real remote.
70+
maven {
71+
name = "local"
72+
url = uri(rootProject.layout.buildDirectory.dir("staging-repo"))
73+
}
6474
}
6575
}
66-
}
6776

68-
signing {
69-
isRequired = (System.getenv("CI") == "true")
70-
val signingKey = project.findProperty("signing.key") as String? ?: System.getenv("SIGNING_KEY")
71-
val signingPassword = project.findProperty("signing.password") as String? ?: System.getenv("SIGNING_PASSWORD")
72-
if (!signingKey.isNullOrBlank() && !signingPassword.isNullOrBlank()) {
73-
useInMemoryPgpKeys(signingKey, signingPassword)
77+
signing {
78+
isRequired = (System.getenv("CI") == "true")
79+
val signingKey = project.findProperty("signing.key") as String? ?: System.getenv("SIGNING_KEY")
80+
val signingPassword =
81+
project.findProperty("signing.password") as String? ?: System.getenv("SIGNING_PASSWORD")
82+
if (!signingKey.isNullOrBlank() && !signingPassword.isNullOrBlank()) {
83+
useInMemoryPgpKeys(signingKey, signingPassword)
84+
}
85+
sign(publishing.publications["library"])
7486
}
75-
sign(publishing.publications["library"])
7687
}

0 commit comments

Comments
 (0)