I recently came across the same issue and I wrote a small Maven plugin to apply Javassist class transformations during build. I shared the code https://github.com/drochetti/javassist-maven-plugin
You guessed that you should use the process-classes phase, and the complex part is setting up the class. After some testing and errors, I was able to guess the whole ClassPath problem from Project, Dependencies and Javassist (please refer to com.github.drochetti.javassist.maven.JavassistMojo.execute() if you want to check the solution).
Below are some guidelines on the GitHub link, but basically you need to:
1 - configure the plugin on pom.xml
<plugin> <groupId>com.github.drochetti</groupId> <artifactId>javassist-maven-plugin</artifactId> <version>1.0.0-SNAPSHOT</version> <configuration> <includeTestClasses>false</includeTestClasses> <transformerClasses> <transformerClass>com.domain.ToStringTransformer</transformerClass> </transformerClasses> </configuration> <executions> <execution> <phase>process-classes</phase> <goals> <goal>javassist</goal> </goals> </execution> </executions> </plugin>
2 - Implement a ClassTransformer , here is an example:
public class ToStringTransformer extends ClassTransformer { @Override protected boolean filter(CtClass candidateClass) throws Exception { CtClass myInterface = ClassPool.getDefault().get(MyInterface.class.getName()); return !candidateClass.equals(myInterface) && candidateClass.subtypeOf(myInterface); } @Override protected void applyTransformations(CtClass classToTransform) throws Exception {
Note. To implement a transformer, you need to add a plug-in depending on your project, but donβt worry, since it is used only during assembly, it can be provided in scope, this will not be a dependency of your final build.
I hope this helps! Let me know if you need more help.
Daniel
source share