diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index bd53bec9613..66dc027da2e 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -11,6 +11,7 @@ kotlin { dependencies { implementation(gradleApi()) implementation(libs.kotlin.gradlePlugin) + implementation(libs.publishPlugin) } gradlePlugin { @@ -20,4 +21,10 @@ gradlePlugin { implementationClass = "dagger.gradle.build.KotlinJvmConventionPlugin" } } + plugins { + register("publish") { + id = libs.plugins.dagger.publish.get().pluginId + implementationClass = "dagger.gradle.build.PublishConventionPlugin" + } + } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/dagger/gradle/build/KotlinJvmConventionPlugin.kt b/buildSrc/src/main/kotlin/dagger/gradle/build/KotlinJvmConventionPlugin.kt index 4a04ea26f5a..e5dd52df9df 100644 --- a/buildSrc/src/main/kotlin/dagger/gradle/build/KotlinJvmConventionPlugin.kt +++ b/buildSrc/src/main/kotlin/dagger/gradle/build/KotlinJvmConventionPlugin.kt @@ -26,9 +26,9 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion class KotlinJvmConventionPlugin : Plugin { override fun apply(project: Project) { - project.pluginManager.apply(KGP_JVM_ID) + project.pluginManager.apply(project.getPluginIdByName("kotlinJvm")) - project.plugins.withId(KGP_JVM_ID) { + project.plugins.withId(project.getPluginIdByName("kotlinJvm")) { val kotlinProject = project.extensions.getByName("kotlin") as KotlinJvmProjectExtension kotlinProject.explicitApi() kotlinProject.jvmToolchain { @@ -41,8 +41,4 @@ class KotlinJvmConventionPlugin : Plugin { } } } - - companion object { - private const val KGP_JVM_ID = "org.jetbrains.kotlin.jvm" - } } \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/dagger/gradle/build/PublishConventionPlugin.kt b/buildSrc/src/main/kotlin/dagger/gradle/build/PublishConventionPlugin.kt new file mode 100644 index 00000000000..360ff71b154 --- /dev/null +++ b/buildSrc/src/main/kotlin/dagger/gradle/build/PublishConventionPlugin.kt @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2025 The Dagger Authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package dagger.gradle.build + +import com.vanniktech.maven.publish.MavenPublishBaseExtension +import com.vanniktech.maven.publish.SonatypeHost +import org.gradle.api.Plugin +import org.gradle.api.Project + +class PublishConventionPlugin : Plugin { + override fun apply(project: Project) { + project.pluginManager.apply(project.getPluginIdByName("publish")) + + project.plugins.withId(project.getPluginIdByName("publish")) { + val publishExtension = project.extensions.getByName("mavenPublishing") as MavenPublishBaseExtension + publishExtension.apply { + coordinates( + groupId = "com.google.dagger", + artifactId = project.name, + version = project.findProperty("PUBLISH_VERSION").toString() + ) + publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL) + pom { + name.set(project.name.asPomName()) + description.set("A fast dependency injector for Android and Java.") + url.set("https://github.com/google/dagger") + scm { + url.set("https://github.com/google/dagger/") + connection.set("scm:git:git://github.com/google/dagger.git") + } + issueManagement { + system.set("GitHub Issues") + url.set("https://github.com/google/dagger/issues") + } + licenses { + license { + name.set("The Apache Software License, Version 2.0") + url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") + } + } + organization { + name.set("Google, Inc.") + url.set("https://www.google.com") + } + } + } + } + } + + /** + * Converts the Gradle project name to a more appropriate name for the POM file. + * + * For example: 'dagger-compiler' to 'Dagger Compiler' + */ + private fun String.asPomName(): String { + val parts = split("-").map { first().uppercaseChar() + drop(1) } + return parts.joinToString(separator = " ") + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/dagger/gradle/build/VersionCatalogs.kt b/buildSrc/src/main/kotlin/dagger/gradle/build/VersionCatalogs.kt index 07e910a6b5c..c9f034bbeec 100644 --- a/buildSrc/src/main/kotlin/dagger/gradle/build/VersionCatalogs.kt +++ b/buildSrc/src/main/kotlin/dagger/gradle/build/VersionCatalogs.kt @@ -30,4 +30,13 @@ internal fun Project.getVersionByName(name: String): String { } else { error("Could not find a version for `$name`") } +} + +internal fun Project.getPluginIdByName(name: String): String { + val plugin = versionCatalog.findPlugin(name) + return if (plugin.isPresent) { + plugin.get().map { it.pluginId }.get() + } else { + error("Could not find plugin id for `$name`") + } } \ No newline at end of file diff --git a/gradle-projects/dagger-runtime/build.gradle.kts b/gradle-projects/dagger-runtime/build.gradle.kts index cb0054f5ddf..8e4941dfd71 100644 --- a/gradle-projects/dagger-runtime/build.gradle.kts +++ b/gradle-projects/dagger-runtime/build.gradle.kts @@ -2,6 +2,7 @@ import dagger.gradle.build.daggerSources plugins { alias(libs.plugins.dagger.kotlinJvm) + alias(libs.plugins.dagger.publish) } daggerSources { diff --git a/gradle.properties b/gradle.properties index 4901d0a727d..9b5bb06fcbb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -15,4 +15,11 @@ org.gradle.caching=true org.gradle.configuration-cache=true # Kotlin code style for this project: "official" or "obsolete": -kotlin.code.style=official \ No newline at end of file +kotlin.code.style=official + +# Don't include the stdlib as a dependency by default +kotlin.stdlib.default.dependency=false + +# Publish version +# TODO(danysantiago): Find a configurable location for the publishing version. +PUBLISH_VERSION=LOCAL-SNAPSHOT \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index eac1aad0215..c48b116e874 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,6 +5,7 @@ junit = "4.13" jvmTarget = "1.8" kotlin = "2.0.21" kotlinTarget = "1.9" +publish = "0.30.0" truth = "1.4.0" [libraries] @@ -15,8 +16,11 @@ jspecify = { module = "org.jspecify:jspecify", version = "1.0.0" } junit = { module = "junit:junit", version.ref = "junit" } kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } +publishPlugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "publish" } truth = { module = "com.google.truth:truth", version.ref = "truth" } [plugins] dagger-kotlinJvm = { id = "dagger.gradle.build.jvm" } -kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } \ No newline at end of file +dagger-publish = { id = "dagger.gradle.build.publish" } +kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +publish = { id = "com.vanniktech.maven.publish", version.ref = "publish" } \ No newline at end of file