Grails 2.1.1 - How to develop a plugin with AstTransformer?

I want to replace an automatically entered log object that is of type org.apache.commons.logging.Log with an object of type org.slf4j.Logger , so that I can use it correctly with Logback.

So I need to create a class ...Transformer (written in Java) is what I got from Graham Roche on the grails-user mailing list. I also know that I need to pack this class ...Transformer inside the plugin and make it a * .jar archive, which I can load in the lib/ plugin folder. But I assume that I am doing something wrong here, since I have a class, as well as a META-INF folder that contains the MANIFEST.MF file, as well as another services folder that contains the following org.codehaus.groovy.transform.ASTTransformation file org.codehaus.groovy.transform.ASTTransformation that contains only one String: the canonical name of the class ...Transformer .

Now, if I try to make grails clean , everything will be fine, BUT if I try to run grails package-plugin , the console will java.lang.ClassNotFoundException .

Circumcision 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 anyone have any experience with Grails plugins that handle using AstTransformer and can give me some tips on this? Is there a good tutorial that I haven't seen yet?

Please let me know;)

+3
source share
1 answer

so after some research, viewing, and finally a query on 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 global ASTTransformation to add an org.slf4j.Logger object instead of the usual org.apache.commons.logging.Log object in each Artefact class without annotation.

So here are the steps:

I created a 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.Logger object. It is imperative to place Java.class in the following package: org.codehaus.groovy.grails.compiler as

Grails scans classes annotated with @AstTransformer in this package. (Graham Rocher)

and pack it in the JAR along with the MANIFEST.MF file in the META-INF/ folder. A META-INF/services catalog with all its materials is not needed, as Graham Rocher said:

You do not need META-INF / services material, and I would delete it, as this probably complicates matters.

So, I think this statement was more related to my specific problem, since I have only one @AstTransformer class in my plugin, but this is just an assumption. And I did not look for additional information on this topic. Maybe some other developer who needs this can do some research and share their decision on this topic ...

The JAR must be imported into the plugin and placed in the lib/ directory. After that you can make grails clean , grails compile and grails package-plugin .

If you want to replace the log implementation, just like me, you must exclude the grails-logging and grails-plugin-log4j JAR files from the specified project class path. This is done in the BuildConfig.groovy file:

 inherits("global") { excludes "grails-plugin-log4j", "grails-logging" } 

Now install the grails install-plugin \path\to\plugin.zip and everything should work as expected.

Hope this helps ...

+1
source

All Articles