Search code examples
gradlepluginsbuild.gradlegradle-pluginbyte-buddy

How to add a source code file as a build dependency and/or access definitions in a source file?


I have:

plugins {
    id 'java'
    id 'net.bytebuddy.byte-buddy-gradle-plugin' version "$byteBuddyVersion"
}

byteBuddy {
    transformation {
        plugin = io.github.leoframework.testing.Logger.class
    }
}

Logger.java is part of the project and in the Byte Buddy plugin. I need to reference which file/class within my build script to invoke the plugin. The class file needs to be loaded after compiling Logger.java.


Solution

  • If it's part of the current project, it is not that trivial, unfortunately. Gradle is executed in its own JVM process that is run before the compilation what makes this somewhat a chicken and egg problem.

    To overcome this, you'd need to compile the classes first, then define a subproject that depend on these classes dynamically and then execute the plugin from this subproject once the dependent classes are available.