3

can there be a maven plugin instead of javaagent to reduce startup time of an application? Many ORM tools have both javaagent and maven plugin, so it should be possible - is it? Or at least something like "CompiledClassFilesBuilder" similar in functionality to AgentBuilder :-)

Regards, Pavel

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
Pavel Arnošt
  • 97
  • 2
  • 8

1 Answers1

7

It is perfectly possible to use Byte Buddy from within a Maven plugin. You can implement a ClassFileLocator for the Maven target after the compile phase and redefine the classes within this folder using a ByteBuddy instance. The API is identical to that of the AgentBuilder inside a transformer.

You would need to implement such a plugin, though. There is none currently existing.

Do however note that this static transformation has its limitations. Using a plugin, it is not possible to redefine bootstrap classes of the VM. It would also be difficult to change library classes as they are external artifacts. Finally, Byte Buddy can register live callbacks that need to be set wt runtime.

UPDATE: I just added the plugin in question and will release a Maven Byte Buddy plugin in version 1.4.21. A transformation can be added as follows:

<plugin>
  <groupId>net.bytebuddy</groupId>
  <artifactId>byte-buddy-maven-plugin</artifactId>
  <executions>
    <execution>
      <goals>
        <goal>transform</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <transformations>
      <transformation>
        <plugin>net.bytebuddy.test.SimplePlugin</plugin>
        <!-- Optionally, specify groupId, artifactId, version of the class -->
      </transformation>
    </transformations>
    <!-- Optionally, add 'initialization' block with EntryPoint class -->
  </configuration>
</plugin>

The two relevant interfaces Plugin and EntryPoint are added to the library directly and are straight-forward to implement.

Mark Chesney
  • 1,082
  • 12
  • 20
Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • Hi Rafael! Do you think this approach is suitable for adding a static field containing license info at compile time? This would avoid having to remember about it, and reduce the code we have to write. – LppEdd Aug 27 '22 at 09:43
  • Yes, that would be a rather trivial change. Given that the class is part lf your project what I assume it is. – Rafael Winterhalter Sep 01 '22 at 06:54
  • Thanks! I was able to setup a test plugin and it's being called correctly. A question tho. I see a File is passed in to the plugin, which represents the classes root dir, but is it possible to get, somehow, access to the actual source code File (e.g. to check for a comment)? Or is that out of scope? – LppEdd Sep 01 '22 at 12:07
  • 1
    Not via the plugin. You'd need to pass that information via an annotation, for example. – Rafael Winterhalter Sep 02 '22 at 11:02