Skip to content

Commit a692ee9

Browse files
committed
Gradle cache config: bytebuddy
1 parent 5c669d6 commit a692ee9

File tree

2 files changed

+58
-16
lines changed

2 files changed

+58
-16
lines changed

gradle-plugins/src/main/kotlin/io.opentelemetry.instrumentation.muzzle-generation.gradle.kts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import io.opentelemetry.javaagent.muzzle.generation.ClasspathByteBuddyPlugin
22
import io.opentelemetry.javaagent.muzzle.generation.ClasspathTransformation
3-
import net.bytebuddy.ClassFileVersion
4-
import net.bytebuddy.build.gradle.ByteBuddySimpleTask
3+
import io.opentelemetry.javaagent.muzzle.generation.ConfigurationCacheFriendlyByteBuddyTask
54
import net.bytebuddy.build.gradle.Transformation
65

76
plugins {
@@ -50,7 +49,7 @@ val languageTasks = LANGUAGES.map { language ->
5049
return@map null
5150
}
5251
val compileTask = tasks.named(compileTaskName)
53-
createLanguageTask(compileTask, "byteBuddy${language.replaceFirstChar(Char::titlecase)}")
52+
createLanguageTask(compileTask, language)
5453
}.filterNotNull()
5554

5655
tasks {
@@ -60,29 +59,43 @@ tasks {
6059
}
6160

6261
fun createLanguageTask(
63-
compileTaskProvider: TaskProvider<*>, name: String): TaskProvider<*> {
64-
return tasks.register<ByteBuddySimpleTask>(name) {
65-
setGroup("Byte Buddy")
66-
outputs.cacheIf { true }
67-
classFileVersion = ClassFileVersion.JAVA_V8
68-
var transformationClassPath = inputClasspath
62+
compileTaskProvider: TaskProvider<*>, language: String): TaskProvider<*> {
63+
val taskName = "byteBuddy${language.replaceFirstChar { it.uppercase() }}"
64+
val mainSourceSet = sourceSets.main.get()
65+
66+
// Create the input classpath from the existing logic in the main part
67+
val inputClasspath = (mainSourceSet.output.resourcesDir?.let { codegen.plus(project.files(it)) }
68+
?: codegen)
69+
.plus(mainSourceSet.output.dirs) // needed to support embedding shadowed modules into instrumentation
70+
.plus(configurations.runtimeClasspath.get())
71+
72+
val byteBuddyTask = tasks.register(taskName, ConfigurationCacheFriendlyByteBuddyTask::class.java) {
73+
dependsOn(compileTaskProvider, mainSourceSet.processResourcesTaskName)
74+
75+
transformations.add(createTransformation(inputClasspath, pluginName))
76+
77+
// Configure the ByteBuddy task properties directly during task creation
6978
val compileTask = compileTaskProvider.get()
7079
// this does not work for kotlin as compile task does not extend AbstractCompile
7180
if (compileTask is AbstractCompile) {
7281
val classesDirectory = compileTask.destinationDirectory.asFile.get()
73-
val rawClassesDirectory: File = File(classesDirectory.parent, "${classesDirectory.name}raw")
74-
.absoluteFile
75-
dependsOn(compileTask)
82+
val rawClassesDirectory = File(classesDirectory.parent, "${classesDirectory.name}raw").absoluteFile
83+
84+
// Configure the compile task to write to rawClassesDirectory
7685
compileTask.destinationDirectory.set(rawClassesDirectory)
86+
87+
// Configure ByteBuddy task properties
7788
source = rawClassesDirectory
7889
target = classesDirectory
7990
classPath = compileTask.classpath.plus(rawClassesDirectory)
80-
transformationClassPath = transformationClassPath.plus(files(rawClassesDirectory))
81-
dependsOn(compileTask, sourceSet.processResourcesTaskName)
82-
}
8391

84-
transformations.add(createTransformation(transformationClassPath, pluginName))
92+
// Clear and set transformations with correct classpath
93+
transformations.clear()
94+
transformations.add(createTransformation(inputClasspath.plus(files(rawClassesDirectory)), pluginName))
95+
}
8596
}
97+
98+
return byteBuddyTask
8699
}
87100

88101
fun createTransformation(classPath: FileCollection, pluginClassName: String): Transformation {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.opentelemetry.javaagent.muzzle.generation
2+
3+
import net.bytebuddy.build.Plugin
4+
import net.bytebuddy.build.gradle.ByteBuddySimpleTask
5+
import org.gradle.api.tasks.TaskAction
6+
import java.io.IOException
7+
8+
/**
9+
* Byte Buddy task variant that avoids touching Gradle's Project API from the task action so the
10+
* task remains compatible with Gradle configuration cache.
11+
*/
12+
open class ConfigurationCacheFriendlyByteBuddyTask : ByteBuddySimpleTask() {
13+
14+
@TaskAction
15+
@Throws(IOException::class)
16+
override fun apply() {
17+
val sourceDir = source
18+
val targetDir = target
19+
20+
if (sourceDir != targetDir && deleteRecursively(targetDir)) {
21+
logger.debug("Deleted all target files in {}", targetDir)
22+
}
23+
24+
doApply(
25+
Plugin.Engine.Source.ForFolder(sourceDir),
26+
Plugin.Engine.Target.ForFolder(targetDir)
27+
)
28+
}
29+
}

0 commit comments

Comments
 (0)