I want to replace the auto injected log object, which is of type org.apache.commons.logging.Log with an object of type org.slf4j.Logger, so that I can use it properly with Logback.
Therefore I need to create a ...Transformer class (written in Java) – that’s what I got from Graeme Rocher over at the “grails-user” mailing list. I’m also aware that I have to pack this ...Transformer class within a plugin and make it a *.jar archive which I can load within the lib/ folder of the plugin. But I guess I’m doing something wrong here as I have the class, along with a META-INF folder which contains the MANIFEST.MF file as well as another folder services which holds the following file org.codehaus.groovy.transform.ASTTransformation which holds just one String: the canonical name of the ...Transformer class.
Now, if I try to do a grails clean everything is fine, BUT if I try to run grails package-plugin the console comes up with a java.lang.ClassNotFoundException.
Clipping from Stacktrace:
| Packaging Grails application...
| Error Fatal error during compilation org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Could not instantiate global transform class my.package.ast.LoggingTransformation specified at jar:file:/C:/Source/MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception java.lang.ClassNotFoundException: my.package.ast.LoggingTransformation
1 error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Could not instantiate global transform class my.package.ast.LoggingTransformation specified at jar:file:/C:/Source/MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation because of exception java.lang.ClassNotFoundException: my.package.ast.LoggingTransformation
Does anybody have some experience with Grails plugins which handle with AstTransformer and could give me some advice on this? Is there a good tutorial out there which I haven’t seen so far?
Please let me know 😉
so, after some research, browsing and finally asking at the Grails Mailing List (see the mailing list archives at: http://grails.1312388.n4.nabble.com/Grails-user-f1312389.html I found a solution.
my goal was to create a Globals ASTTransformation, to inject a
org.slf4j.Loggerobject instead of the usualorg.apache.commons.logging.Logobject into every Artefact class without annotation.so, here are the steps:
I created Java class similar to https://github.com/grails/grails-core/blob/master/grails-logging/src/main/groovy/org/codehaus/groovy/grails/compiler/logging/LoggingTransformer.java but with my own implementation of the
org.slf4j.Loggerobject. It is crucial that you place the Java.class under the following package:org.codehaus.groovy.grails.compilerasand pack it into a JAR along with its
MANIFEST.MFfile within theMETA-INF/folder. AMETA-INF/servicesdirectory with all its stuff is not needed as Graeme Rocher stated:So, I guess this statement was more related to my specific problem as I only have one
@AstTransformerclass within my plugin, but that’s just a guess. And I haven’t searched for further informations on this topic. Maybe some other developer here who needs this could do some research and share his solution within this thread…The JAR should be imported to the plugin and placed under the
lib/directory. After this you should be able to dograils clean,grails compileandgrails package-plugin.If you want to replace the
logimplementation, as I did, you should exclude thegrails-loggingandgrails-plugin-log4jJARs from your designated project’s classpath. This is done in theBuildConfig.groovyfile:Now install your plugin
grails install-plugin \path\to\plugin.zipand everthing should work as expected.Hope this helps…