Skip to content

Commit

Permalink
Add maven publish plugin to Gradle projects.
Browse files Browse the repository at this point in the history
Only adds the plugin and POM configuration enabling publishing to local repository, not yet configured for actual world publishing.

RELNOTES=N/A
PiperOrigin-RevId: 713019870
  • Loading branch information
danysantiago authored and Dagger Team committed Jan 7, 2025
1 parent 9daa0ae commit 8b4f9b6
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 8 deletions.
7 changes: 7 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ kotlin {
dependencies {
implementation(gradleApi())
implementation(libs.kotlin.gradlePlugin)
implementation(libs.publishPlugin)
}

gradlePlugin {
Expand All @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
class KotlinJvmConventionPlugin : Plugin<Project> {

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 {
Expand All @@ -41,8 +41,4 @@ class KotlinJvmConventionPlugin : Plugin<Project> {
}
}
}

companion object {
private const val KGP_JVM_ID = "org.jetbrains.kotlin.jvm"
}
}
Original file line number Diff line number Diff line change
@@ -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<Project> {
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 = " ")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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`")
}
}
1 change: 1 addition & 0 deletions gradle-projects/dagger-runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import dagger.gradle.build.daggerSources

plugins {
alias(libs.plugins.dagger.kotlinJvm)
alias(libs.plugins.dagger.publish)
}

daggerSources {
Expand Down
9 changes: 8 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
6 changes: 5 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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" }
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" }

0 comments on commit 8b4f9b6

Please sign in to comment.