Skip to content

Commit 01a02a8

Browse files
authored
Enable gradle config cache (#7893)
1 parent 67a886e commit 01a02a8

5 files changed

Lines changed: 81 additions & 34 deletions

File tree

buildSrc/src/main/kotlin/io/opentelemetry/gradle/OtelVersionClassPlugin.kt

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
package io.opentelemetry.gradle
22

3+
import org.gradle.api.DefaultTask
34
import org.gradle.api.Plugin
45
import org.gradle.api.Project
6+
import org.gradle.api.file.DirectoryProperty
57
import org.gradle.api.plugins.JavaPlugin
68
import org.gradle.api.plugins.JavaPluginExtension
9+
import org.gradle.api.provider.Property
10+
import org.gradle.api.tasks.Input
11+
import org.gradle.api.tasks.OutputDirectory
12+
import org.gradle.api.tasks.TaskAction
713
import org.gradle.kotlin.dsl.the
814
import java.io.File
915

@@ -18,31 +24,48 @@ class OtelVersionClassPlugin : Plugin<Project> {
1824
override fun apply(project: Project) {
1925
project.plugins.apply(JavaPlugin::class.java)
2026

21-
project.tasks.register("generateOtelVersionClass") {
22-
doLast {
23-
writeFile(project)
24-
}
27+
val outDir = project.layout.buildDirectory.dir("generated/sources/version/java/main")
28+
29+
project.tasks.register("generateOtelVersionClass", GenerateOtelVersionClassTask::class.java) {
30+
projectGroup.set("${project.group}")
31+
projectName.set(project.name)
32+
projectVersion.set("${project.version}")
33+
outputDirectory.set(outDir)
2534
}
2635
// Add dependency on this task
27-
project.tasks.getByName("compileJava") {
36+
project.tasks.matching { it.name == "compileJava" || it.name == "sourcesJar" }.configureEach {
2837
dependsOn("generateOtelVersionClass")
2938
}
3039
// Add new source dir to the "main" source set
31-
val outDir = buildOutDir(project)
3240
val java = project.the<JavaPluginExtension>()
3341
java.sourceSets.getByName("main").java {
3442
srcDir(outDir)
3543
}
3644
}
45+
}
46+
47+
abstract class GenerateOtelVersionClassTask : DefaultTask() {
48+
@get:Input
49+
abstract val projectGroup: Property<String>
50+
51+
@get:Input
52+
abstract val projectName: Property<String>
3753

38-
private fun writeFile(project: Project) {
39-
val group = "${project.group}".replace('.', '/')
40-
val projectName = project.name.replace('-', '/')
41-
val outDir = buildOutDir(project)
42-
val filename = "$group/$projectName/internal/OtelVersion.java"
54+
@get:Input
55+
abstract val projectVersion: Property<String>
56+
57+
@get:OutputDirectory
58+
abstract val outputDirectory: DirectoryProperty
59+
60+
@TaskAction
61+
fun generate() {
62+
val group = projectGroup.get().replace('.', '/')
63+
val name = projectName.get().replace('-', '/')
64+
val outDir = outputDirectory.get().asFile
65+
val filename = "$group/$name/internal/OtelVersion.java"
4366
val outFile = File(outDir, filename)
44-
val packageName = "${project.group}.${project.name.replace('-', '.')}.internal"
45-
val classBody = getClassBody("${project.version}", packageName)
67+
val packageName = "${projectGroup.get()}.${projectName.get().replace('-', '.')}.internal"
68+
val classBody = getClassBody(projectVersion.get(), packageName)
4669

4770
outFile.parentFile.mkdirs()
4871
outFile.writeText(classBody)
@@ -63,8 +86,4 @@ class OtelVersionClassPlugin : Plugin<Project> {
6386
}
6487
""".trimIndent()
6588
}
66-
67-
private fun buildOutDir(project: Project): File {
68-
return File(project.layout.buildDirectory.asFile.get(), "generated/sources/version/java/main")
69-
}
7089
}

gradle.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
org.gradle.parallel=true
22
org.gradle.caching=true
3+
org.gradle.configuration-cache=true
4+
org.gradle.configuration-cache.parallel=true
35

46
org.gradle.priority=low
57

integration-tests/graal-incubating/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ graalvmNative {
3939
}
4040
}
4141
toolchainDetection.set(false)
42+
// Disable metadata repository to work around configuration cache incompatibility
43+
// https://github.com/graalvm/native-build-tools/issues/477
44+
metadataRepository {
45+
enabled.set(false)
46+
}
47+
}
48+
49+
// GraalVM Native Build Tools plugin is not yet compatible with configuration cache
50+
// https://github.com/graalvm/native-build-tools/issues/477
51+
tasks.configureEach {
52+
if (name.startsWith("native")) {
53+
notCompatibleWithConfigurationCache("GraalVM Native Build Tools plugin is not compatible with configuration cache")
54+
}
4255
}

integration-tests/graal/build.gradle.kts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ graalvmNative {
3636
}
3737
}
3838
toolchainDetection.set(false)
39+
// Disable metadata repository to work around configuration cache incompatibility
40+
// https://github.com/graalvm/native-build-tools/issues/477
41+
metadataRepository {
42+
enabled.set(false)
43+
}
44+
}
45+
46+
// GraalVM Native Build Tools plugin is not yet compatible with configuration cache
47+
// https://github.com/graalvm/native-build-tools/issues/477
48+
tasks.configureEach {
49+
if (name.startsWith("native")) {
50+
notCompatibleWithConfigurationCache("GraalVM Native Build Tools plugin is not compatible with configuration cache")
51+
}
3952
}

sdk-extensions/incubator/build.gradle.kts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,36 @@ val deleteJs2pTmp by tasks.registering(Delete::class) {
146146
}
147147

148148
val buildGraalVmReflectionJson = tasks.register("buildGraalVmReflectionJson") {
149+
val buildDir = buildDirectory
149150
val targetFile = File(
150-
buildDirectory,
151+
buildDir,
151152
"resources/main/META-INF/native-image/io.opentelemetry/io.opentelemetry.sdk.extension.incubator/reflect-config.json"
152153
)
154+
val sourcePackage =
155+
"io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model"
156+
val sourcePackagePath = sourcePackage.replace(".", "/")
157+
val classesDir =
158+
File(
159+
buildDir,
160+
"classes/java/main/$sourcePackagePath"
161+
)
162+
163+
inputs.dir(classesDir)
164+
outputs.file(targetFile)
153165

154166
onlyIf { !targetFile.exists() }
155167

156168
dependsOn("compileJava")
157169

158170
doLast {
159171
println("Generating GraalVM reflection config at: ${targetFile.absolutePath}")
160-
val sourcePackage =
161-
"io.opentelemetry.sdk.extension.incubator.fileconfig.internal.model"
162-
val sourcePackagePath = sourcePackage.replace(".", "/")
163-
164-
val classesDir =
165-
File(
166-
buildDirectory,
167-
"classes/java/main/$sourcePackagePath"
168-
)
169172

170173
val classes = mutableListOf<String>()
171-
fileTree(classesDir).forEach {
172-
val path = it.path
173-
174-
val className = path
175-
.substringAfter(sourcePackagePath)
176-
.removePrefix("/")
174+
classesDir.walkTopDown().filter { it.isFile && it.extension == "class" }.forEach { file ->
175+
val relativePath = file.toRelativeString(classesDir)
176+
val className = relativePath
177177
.removeSuffix(".class")
178-
.replace("/", ".")
178+
.replace(File.separatorChar, '.')
179179
classes.add("$sourcePackage.$className")
180180
}
181181
classes.sort()

0 commit comments

Comments
 (0)