Skip to content

Commit 8b4f9b6

Browse files
danysantiagoDagger Team
authored and
Dagger Team
committed
Add maven publish plugin to Gradle projects.
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
1 parent 9daa0ae commit 8b4f9b6

File tree

7 files changed

+105
-8
lines changed

7 files changed

+105
-8
lines changed

buildSrc/build.gradle.kts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ kotlin {
1111
dependencies {
1212
implementation(gradleApi())
1313
implementation(libs.kotlin.gradlePlugin)
14+
implementation(libs.publishPlugin)
1415
}
1516

1617
gradlePlugin {
@@ -20,4 +21,10 @@ gradlePlugin {
2021
implementationClass = "dagger.gradle.build.KotlinJvmConventionPlugin"
2122
}
2223
}
24+
plugins {
25+
register("publish") {
26+
id = libs.plugins.dagger.publish.get().pluginId
27+
implementationClass = "dagger.gradle.build.PublishConventionPlugin"
28+
}
29+
}
2330
}

buildSrc/src/main/kotlin/dagger/gradle/build/KotlinJvmConventionPlugin.kt

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import org.gradle.jvm.toolchain.JavaLanguageVersion
2626
class KotlinJvmConventionPlugin : Plugin<Project> {
2727

2828
override fun apply(project: Project) {
29-
project.pluginManager.apply(KGP_JVM_ID)
29+
project.pluginManager.apply(project.getPluginIdByName("kotlinJvm"))
3030

31-
project.plugins.withId(KGP_JVM_ID) {
31+
project.plugins.withId(project.getPluginIdByName("kotlinJvm")) {
3232
val kotlinProject = project.extensions.getByName("kotlin") as KotlinJvmProjectExtension
3333
kotlinProject.explicitApi()
3434
kotlinProject.jvmToolchain {
@@ -41,8 +41,4 @@ class KotlinJvmConventionPlugin : Plugin<Project> {
4141
}
4242
}
4343
}
44-
45-
companion object {
46-
private const val KGP_JVM_ID = "org.jetbrains.kotlin.jvm"
47-
}
4844
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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 com.vanniktech.maven.publish.MavenPublishBaseExtension
20+
import com.vanniktech.maven.publish.SonatypeHost
21+
import org.gradle.api.Plugin
22+
import org.gradle.api.Project
23+
24+
class PublishConventionPlugin : Plugin<Project> {
25+
override fun apply(project: Project) {
26+
project.pluginManager.apply(project.getPluginIdByName("publish"))
27+
28+
project.plugins.withId(project.getPluginIdByName("publish")) {
29+
val publishExtension = project.extensions.getByName("mavenPublishing") as MavenPublishBaseExtension
30+
publishExtension.apply {
31+
coordinates(
32+
groupId = "com.google.dagger",
33+
artifactId = project.name,
34+
version = project.findProperty("PUBLISH_VERSION").toString()
35+
)
36+
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
37+
pom {
38+
name.set(project.name.asPomName())
39+
description.set("A fast dependency injector for Android and Java.")
40+
url.set("https://github.com/google/dagger")
41+
scm {
42+
url.set("https://github.com/google/dagger/")
43+
connection.set("scm:git:git://github.com/google/dagger.git")
44+
}
45+
issueManagement {
46+
system.set("GitHub Issues")
47+
url.set("https://github.com/google/dagger/issues")
48+
}
49+
licenses {
50+
license {
51+
name.set("The Apache Software License, Version 2.0")
52+
url.set("https://www.apache.org/licenses/LICENSE-2.0.txt")
53+
}
54+
}
55+
organization {
56+
name.set("Google, Inc.")
57+
url.set("https://www.google.com")
58+
}
59+
}
60+
}
61+
}
62+
}
63+
64+
/**
65+
* Converts the Gradle project name to a more appropriate name for the POM file.
66+
*
67+
* For example: 'dagger-compiler' to 'Dagger Compiler'
68+
*/
69+
private fun String.asPomName(): String {
70+
val parts = split("-").map { first().uppercaseChar() + drop(1) }
71+
return parts.joinToString(separator = " ")
72+
}
73+
}

buildSrc/src/main/kotlin/dagger/gradle/build/VersionCatalogs.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,13 @@ internal fun Project.getVersionByName(name: String): String {
3030
} else {
3131
error("Could not find a version for `$name`")
3232
}
33+
}
34+
35+
internal fun Project.getPluginIdByName(name: String): String {
36+
val plugin = versionCatalog.findPlugin(name)
37+
return if (plugin.isPresent) {
38+
plugin.get().map { it.pluginId }.get()
39+
} else {
40+
error("Could not find plugin id for `$name`")
41+
}
3342
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import dagger.gradle.build.daggerSources
22

33
plugins {
44
alias(libs.plugins.dagger.kotlinJvm)
5+
alias(libs.plugins.dagger.publish)
56
}
67

78
daggerSources {

gradle.properties

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,11 @@ org.gradle.caching=true
1515
org.gradle.configuration-cache=true
1616

1717
# Kotlin code style for this project: "official" or "obsolete":
18-
kotlin.code.style=official
18+
kotlin.code.style=official
19+
20+
# Don't include the stdlib as a dependency by default
21+
kotlin.stdlib.default.dependency=false
22+
23+
# Publish version
24+
# TODO(danysantiago): Find a configurable location for the publishing version.
25+
PUBLISH_VERSION=LOCAL-SNAPSHOT

gradle/libs.versions.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ junit = "4.13"
55
jvmTarget = "1.8"
66
kotlin = "2.0.21"
77
kotlinTarget = "1.9"
8+
publish = "0.30.0"
89
truth = "1.4.0"
910

1011
[libraries]
@@ -15,8 +16,11 @@ jspecify = { module = "org.jspecify:jspecify", version = "1.0.0" }
1516
junit = { module = "junit:junit", version.ref = "junit" }
1617
kotlin-gradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
1718
kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
19+
publishPlugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "publish" }
1820
truth = { module = "com.google.truth:truth", version.ref = "truth" }
1921

2022
[plugins]
2123
dagger-kotlinJvm = { id = "dagger.gradle.build.jvm" }
22-
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
24+
dagger-publish = { id = "dagger.gradle.build.publish" }
25+
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
26+
publish = { id = "com.vanniktech.maven.publish", version.ref = "publish" }

0 commit comments

Comments
 (0)