Skip to content

Commit 3418609

Browse files
danysantiagoDagger Team
authored andcommitted
Move JDK toolchain, Kotlin language and JVM target configuration to convention plugin.
Also trying to keep libs.versions.toml ordered alphabetically. RELNOTES=N/A PiperOrigin-RevId: 711866498
1 parent 0927b9a commit 3418609

File tree

5 files changed

+103
-19
lines changed

5 files changed

+103
-19
lines changed

buildSrc/build.gradle.kts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
plugins {
2-
alias(libs.plugins.kotlinJvm)
2+
`kotlin-dsl`
33
}
44

55
kotlin {
6-
jvmToolchain(18)
6+
jvmToolchain {
7+
languageVersion.set(libs.versions.jdk.map(JavaLanguageVersion::of))
8+
}
79
}
810

911
dependencies {
1012
implementation(gradleApi())
1113
implementation(libs.kotlin.gradlePlugin)
1214
}
15+
16+
gradlePlugin {
17+
plugins {
18+
register("kotlinJvm") {
19+
id = libs.plugins.dagger.kotlinJvm.get().pluginId
20+
implementationClass = "dagger.gradle.build.KotlinJvmConventionPlugin"
21+
}
22+
}
23+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (C) 2025 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.gradle.build
18+
19+
import org.gradle.api.Plugin
20+
import org.gradle.api.Project
21+
import org.jetbrains.kotlin.gradle.dsl.KotlinJvmProjectExtension
22+
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
23+
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
24+
import org.gradle.jvm.toolchain.JavaLanguageVersion
25+
26+
class KotlinJvmConventionPlugin : Plugin<Project> {
27+
28+
override fun apply(project: Project) {
29+
project.pluginManager.apply(KGP_JVM_ID)
30+
31+
project.plugins.withId(KGP_JVM_ID) {
32+
val kotlinProject = project.extensions.getByName("kotlin") as KotlinJvmProjectExtension
33+
kotlinProject.explicitApi()
34+
kotlinProject.jvmToolchain {
35+
languageVersion.set(JavaLanguageVersion.of(project.getVersionByName("jdk")))
36+
}
37+
kotlinProject.compilerOptions.apply {
38+
languageVersion.set(KotlinVersion.fromVersion(project.getVersionByName("kotlinTarget")))
39+
apiVersion.set(KotlinVersion.fromVersion(project.getVersionByName("kotlinTarget")))
40+
jvmTarget.set(JvmTarget.fromTarget(project.getVersionByName("jvmTarget")))
41+
}
42+
}
43+
}
44+
45+
companion object {
46+
private const val KGP_JVM_ID = "org.jetbrains.kotlin.jvm"
47+
}
48+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2025 The Dagger Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dagger.gradle.build
18+
19+
import org.gradle.api.Project
20+
import org.gradle.api.artifacts.VersionCatalog
21+
import org.gradle.api.artifacts.VersionCatalogsExtension
22+
23+
internal val Project.versionCatalog: VersionCatalog
24+
get() = project.extensions.getByType(VersionCatalogsExtension::class.java).find("libs").get()
25+
26+
internal fun Project.getVersionByName(name: String): String {
27+
val version = versionCatalog.findVersion(name)
28+
return if (version.isPresent) {
29+
version.get().requiredVersion
30+
} else {
31+
error("Could not find a version for `$name`")
32+
}
33+
}

gradle-projects/dagger-runtime/build.gradle.kts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import dagger.gradle.build.daggerSources
2-
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
3-
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
42

53
plugins {
6-
id(libs.plugins.kotlinJvm.get().pluginId)
4+
alias(libs.plugins.dagger.kotlinJvm)
75
}
86

97
// TODO(danysantiago): Add proguard files as META-INF resources
@@ -24,16 +22,6 @@ daggerSources {
2422
)
2523
}
2624

27-
// TODO(danysantiago): Move configuration to a buildSrc plugin so it is applied to all projects
28-
kotlin {
29-
jvmToolchain(18)
30-
compilerOptions {
31-
languageVersion = KotlinVersion.KOTLIN_1_8
32-
apiVersion = KotlinVersion.KOTLIN_1_8
33-
jvmTarget = JvmTarget.JVM_1_8
34-
}
35-
}
36-
3725
dependencies {
3826
api(libs.javax.inject)
3927
api(libs.jakarta.inject)

gradle/libs.versions.toml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
[versions]
2-
kotlin = "2.0.21"
32
guava = "33.0.0-jre"
3+
jdk = "18"
44
junit = "4.13"
5+
jvmTarget = "1.8"
6+
kotlin = "2.0.21"
7+
kotlinTarget = "1.9"
58
truth = "1.4.0"
69

710
[libraries]
8-
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
9-
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
11+
guava-jre = { module = "com.google.guava:guava", version.ref = "guava" }
1012
jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version = "2.0.1" }
1113
javax-inject = { module = "javax.inject:javax.inject", version = "1" }
1214
jspecify = { module = "org.jspecify:jspecify", version = "1.0.0" }
13-
guava-jre = { module = "com.google.guava:guava", version.ref = "guava" }
1415
junit = { module = "junit:junit", version.ref = "junit" }
16+
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
17+
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
1518
truth = { module = "com.google.truth:truth", version.ref = "truth" }
1619

1720
[plugins]
21+
dagger-kotlinJvm = { id = "dagger.gradle.build.jvm" }
1822
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

0 commit comments

Comments
 (0)