Skip to content

Commit 37e249b

Browse files
danysantiagoDagger Team
authored andcommitted
Initial setup of Gradle as a build system for Dagger
At the repository root, the root project 'dagger-parent' configures common build functionality and includes the various projects that will be placed 'gradle-projects', for now only the runtime is configured. The buildSrc directory is a special Gradle sub-project to share common build logic between sub-projects, it will become more useful in time as the projects gets more complex and shared build logic is needed. Dependencies and versions are defined in a Version Catalog in gradle/libs.versions.toml, these will eventually need to be validated to be in-sync with Bazel's WORKSPACE. RELNOTES=N/A PiperOrigin-RevId: 708343535
1 parent e9e8deb commit 37e249b

File tree

14 files changed

+606
-0
lines changed

14 files changed

+606
-0
lines changed

buildSrc/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Dagger's Gradle build logic
2+
3+
See https://docs.gradle.org/current/userguide/organizing_gradle_projects.html#sec:build_sources

buildSrc/build.gradle.kts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
plugins {
2+
alias(libs.plugins.kotlinJvm)
3+
}
4+
5+
kotlin {
6+
jvmToolchain(18)
7+
}
8+
9+
dependencies {
10+
implementation(gradleApi())
11+
implementation(libs.kotlin.gradlePlugin)
12+
}

buildSrc/settings.gradle.kts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
pluginManagement {
2+
repositories {
3+
google()
4+
mavenCentral()
5+
gradlePluginPortal()
6+
}
7+
}
8+
dependencyResolutionManagement {
9+
repositories {
10+
google()
11+
mavenCentral()
12+
}
13+
versionCatalogs {
14+
create("libs") {
15+
from(files("../gradle/libs.versions.toml"))
16+
}
17+
}
18+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (C) 2024 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.NamedDomainObjectContainer
20+
import org.gradle.api.Project
21+
import org.gradle.api.file.SourceDirectorySet
22+
import org.gradle.api.plugins.JavaPluginExtension
23+
import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension
24+
import org.jetbrains.kotlin.gradle.plugin.KotlinSourceSet
25+
import java.nio.file.Path
26+
import kotlin.io.path.Path
27+
import kotlin.io.path.isDirectory
28+
29+
private typealias JavaSourceSet = org.gradle.api.tasks.SourceSet
30+
31+
@DslMarker
32+
annotation class DaggerGradleDsl
33+
34+
@DaggerGradleDsl
35+
class DaggerSourceSet(
36+
private val project: Project,
37+
private val kotlinSourceSets: NamedDomainObjectContainer<KotlinSourceSet>,
38+
private val javaSourceSets: NamedDomainObjectContainer<JavaSourceSet>,
39+
) {
40+
val main: SourceSet = object : SourceSet {
41+
override fun setPackages(packages: List<String>) {
42+
val packagePaths = packages.map { Path(it) }
43+
kotlinSourceSets.getByName("main").kotlin
44+
.includePackages("${project.rootDir}/java", packagePaths)
45+
javaSourceSets.getByName("main").java
46+
.includePackages("${project.rootDir}/java", packagePaths)
47+
}
48+
}
49+
val test: SourceSet = object : SourceSet {
50+
override fun setPackages(packages: List<String>) {
51+
val packagePaths = packages.map { Path(it) }
52+
kotlinSourceSets.getByName("test").kotlin
53+
.includePackages("${project.rootDir}/javatests", packagePaths)
54+
javaSourceSets.getByName("test").java
55+
.includePackages("${project.rootDir}/javatests", packagePaths)
56+
}
57+
}
58+
59+
interface SourceSet {
60+
fun setPackages(packages: List<String>)
61+
}
62+
}
63+
64+
/**
65+
* Configure project's source set based on Dagger's project structure.
66+
*
67+
* Specifically it will include sources in the packages specified by [DaggerSourceSet.SourceSet.setPackages].
68+
*/
69+
fun Project.daggerSources(block: DaggerSourceSet.() -> Unit) {
70+
val kotlinExtension = extensions.findByType(KotlinProjectExtension::class.java)
71+
?: error("The daggerSources() configuration must be applied to a Kotlin (JVM) project.")
72+
val javaExtension = extensions.findByType(JavaPluginExtension::class.java)
73+
?: error("The daggerSources() configuration must be applied to a Kotlin (JVM) project.")
74+
val daggerSources = DaggerSourceSet(this, kotlinExtension.sourceSets, javaExtension.sourceSets)
75+
black.invoke(daggerSources)
76+
}
77+
78+
/**
79+
* Includes sources from the given [packages] into this source set.
80+
*
81+
* Only sources within the package directory are included and not its sub-packages.
82+
*/
83+
private fun SourceDirectorySet.includePackages(
84+
basePath: String,
85+
packages: Iterable<Path>,
86+
) {
87+
val packagesDirectories = packages.flatMap { it.expandParts() }.toSet()
88+
setSrcDirs(listOf(basePath)).include {
89+
val path = Path(it.path)
90+
if (Path(basePath).resolve(path).isDirectory()) {
91+
path in packagesDirectories
92+
} else {
93+
path.parent in packages
94+
}
95+
}
96+
}
97+
98+
/**
99+
* Expands a [Path] to includes it parents.
100+
*
101+
* i.e. for `"foo/bar"` it will expand to `setOf("foo", foo/bar")`
102+
*/
103+
private fun Path.expandParts(): Set<Path> {
104+
return buildSet {
105+
var path: Path? = this@expandParts
106+
while (path != null) {
107+
add(path)
108+
path = path.parent
109+
}
110+
}
111+
}

gradle-projects/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
### Dagger's Gradle projects directories
2+
3+
Each directory is a Gradle sub-project that maps to an artifact and whose sources are part of the
4+
Bazel project structure. At the root of the repository is the parent project that includes the
5+
ones in these directory.
6+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import dagger.gradle.build.daggerSources
2+
3+
plugins {
4+
id(libs.plugins.kotlinJvm.get().pluginId)
5+
}
6+
7+
// TODO(danysantiago): Add proguard files as META-INF resources
8+
daggerSources {
9+
main.setPackages(
10+
listOf(
11+
"dagger",
12+
"dagger/assisted",
13+
"dagger/internal",
14+
"dagger/multibindings",
15+
)
16+
)
17+
test.setPackages(
18+
listOf(
19+
"dagger",
20+
"dagger/internal",
21+
)
22+
)
23+
}
24+
25+
// TODO(danysantiago): Move configuration to a buildSrc plugin so it is applied to all projects
26+
kotlin {
27+
jvmToolchain(18)
28+
compilerOptions {
29+
languageVersion = KotlinVersion.KOTLIN_1_8
30+
apiVersion = KotlinVersion.KOTLIN_1_8
31+
jvmTarget = JvmTarget.JVM_1_8
32+
}
33+
}
34+
35+
dependencies {
36+
api(libs.javax.inject)
37+
api(libs.jakarta.inject)
38+
api(libs.jspecify)
39+
40+
testImplementation(libs.junit)
41+
testImplementation(libs.truth)
42+
testImplementation(libs.guava.jre)
43+
}

gradle.properties

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Project-wide Gradle settings.
2+
# IDE (e.g. IntelliJ or Android Studio) users:
3+
# Gradle settings configured through the IDE *will override* any settings specified in this file.
4+
# For more details on how to configure your build environment visit
5+
# http://www.gradle.org/docs/current/userguide/build_environment.html
6+
# Specifies the JVM arguments used for the daemon process.
7+
# The setting is particularly useful for tweaking memory settings.
8+
org.gradle.jvmargs=-Xmx4g -Xms4g -Dfile.encoding=UTF-8
9+
kotlin.daemon.jvmargs=-Xmx4g -Xms4g -Dfile.encoding=UTF-8
10+
11+
# Enable caching between builds.
12+
org.gradle.caching=true
13+
14+
# Enable configuration caching between builds.
15+
org.gradle.configuration-cache=true
16+
17+
# Kotlin code style for this project: "official" or "obsolete":
18+
kotlin.code.style=official

gradle/libs.versions.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[versions]
2+
kotlin = "2.0.21"
3+
guava = "33.0.0-jre"
4+
junit = "4.13"
5+
truth = "1.4.0"
6+
7+
[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" }
10+
jakarta-inject = { module = "jakarta.inject:jakarta.inject-api", version = "2.0.1" }
11+
javax-inject = { module = "javax.inject:javax.inject", version = "1" }
12+
jspecify = { module = "org.jspecify:jspecify", version = "1.0.0" }
13+
guava-jre = { module = "com.google.guava:guava", version.ref = "guava" }
14+
junit = { module = "junit:junit", version.ref = "junit" }
15+
truth = { module = "com.google.truth:truth", version.ref = "truth" }
16+
17+
[plugins]
18+
kotlinJvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }

gradle/wrapper/gradle-wrapper.jar

42.6 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)