11import io.opentelemetry.javaagent.muzzle.generation.ClasspathByteBuddyPlugin
22import 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
54import net.bytebuddy.build.gradle.Transformation
65
76plugins {
@@ -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
5655tasks {
@@ -60,29 +59,43 @@ tasks {
6059}
6160
6261fun 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
88101fun createTransformation (classPath : FileCollection , pluginClassName : String ): Transformation {
0 commit comments