I’m writing a Gradle plugin that, when applied on a JavaCompile
or KotlinCompile
task:
- Reads compiled classes (.class) from
sourceSet.output.classesDirs
, source sets are selected by the user through a configuration key - Performs some bytecode transformations
- Write transformed classes into a separate
build/transformedClasses
directory - Have subsequent tasks, i.e. a Jar task, take
build/transformedClasses
instead ofbuild/classes
as input, seamlessly for the plugin consumer.
I’ve managed to program 1 to 3, but I don’t know how or if 4 can be done.
The pseudo-code I have right now is:
tasks.withType(JavaCompile) { compileTask ->
tasks.register("transform" + compileTask.name, MyTransform::class.java) {
dependsOn(compileTask)
inputSourceSets.set(sourceSets)
sourceSetsToTransform(extension.selectedSourceSets)
}
}
As an output, what I’m trying to achieve is to have
- original
build/classes
compiled classes as a reference - transformed classes in
build/transformedClasses
and used for whatever comes next.
Is there a canonical way of doing such a thing or other projects that achieve something similar? I have had some success by changing the java.destinationDirectory
and the compiledBy
values of the selected source sets, but that does not allow me to keep both the original and transformed classes.