raphw / byte-buddy

Runtime code generation for the Java virtual machine.
https://bytebuddy.net
Apache License 2.0
6.23k stars 804 forks source link

Feature Request: ByteBuddyJarTask for multiple JARs #1711

Closed LarsBodewig closed 1 week ago

LarsBodewig commented 1 week ago

I try to transform multiple JAR files using the ByteBuddyJarTask of the gradle plugin. Since the task only accepts one source and target file, I have to create a separate task for each JAR file.

This might be more of a general gradle issue, but creating a separate task for each file is very cumbersome if the files are not available at configuration time. Currently I invoke a second gradle build after the files are available like this:

build.gradle

// downloads the libs to local folder
def downloadLibs = tasks.create('downloadLibs', Copy) {
    from configurations.lib
    into layout.buildDirectory.dir('libs')
}
// starts a new gradle build after libs are downloaded to have another configuration phase
tasks.create('runByteBuddy', GradleBuild) {
    buildFile file('byteBuddy.gradle')
    tasks = List.of('transformLibs')
    dependsOn downloadLibs
}

byteBuddy.gradle

// transforms all libs
tasks.create('transformLibs') {
    group 'util'
    dependsOn tasks.withType(ByteBuddyJarTask)
}
// the tasks need to be created in the configuration phase, but after the downloadLibs task ran
fileTree('build/libs') { include '*.jar' }.each { jar ->
    def tName = 'byteBuddyJar' + jar.name.replaceAll('[^A-z0-9]', '')
    def t = tasks.create(tName, ByteBuddyJarTask) {
        source = file("build/libs/" + jar.name)
        target = file("build/transformedLibs/" + jar.name)
        ...
    }
}

This seems like a bad way to use gradle.

If the ByteBuddyJarTask was changed or if there was another task that takes a directory as source and target, it would work with a dynamic list of JAR files.

raphw commented 1 week ago

In AbstractByteBuddyTask, there is already a static method apply that allows to trigger a transformation. From this, you could create a custom Task type that fulfills your needs. If you wanted to write a generic one that I can add to Byte Buddy, I am happy to take it into the library! You could take the Maven task as an example. If you do not find the time, I might find it some time in the future, you are right that this should be easier.