Grails 2.1.1 - 如何用AstTransformer开发一个插件? [英] Grails 2.1.1 - How to develop a plugin with an AstTransformer?
问题描述
我想替换自动注入的 log
对象,该对象的类型为 org.apache.commons.logging.Log
类型为 org.slf4j.Logger
的对象,以便我可以在Logback中正确使用它。
因此,我需要创建一个 ... Transformer
class(用Java编写) - 这就是我从Graeme Rocher在grails-user邮件列表中获得的。我也意识到,我必须在插件中打包这个 ... Transformer
类并使其成为一个* .jar压缩文件,我可以在 lib /
插件的文件夹。但我想我在这里做错了,因为我有这个类,以及包含 MANIFEST.MF 的
文件以及另一个文件夹
services
,它包含以下文件 org.codehaus.groovy.transform.ASTTransformation
,它只包含一个字符串: ... Transformer
类的规范名称。
现在,如果我尝试做一个 grails clean
一切正常,但是如果我尝试运行 grails package-plugin
使用 java.lang.ClassNotFoundException
。
从Stacktrace剪切:
|打包Grails应用程序...
|错误编译期间致命错误org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:
无法实例化在jar中指定的全局转换类my.package.ast.LoggingTransformation:file:/ C:/ Source /MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation由于java.lang.ClassNotFoundException异常:my.package.ast.LoggingTransformation
1错误
org.codehaus.groovy.control.MultipleCompilationErrorsException:启动失败:
无法实例化全局转换类my.package.ast.LoggingTransformation指定在jar:file:/ C:/Source/MyGrailsAST/lib/replace-logging-logback-ast.jar!/META-INF/services/org.codehaus.groovy.transform.ASTTransformation因为异常java.lang.ClassNotFoundException: my.package.ast.LoggingTransformation
有没有人有使用Grails插件的经验, AstTransformer
,可以给我一些建议吗?
请让我知道;)
我的目标是创建一个 Globals ASTTransformation ,以注入 org.slf4j.Logger
对象,而不是通常的 org.apache.commons.logging.Log
对象到每个Artefact类中,而不用注释。
所以,下面是步骤:
我创建了类似于 https://github.com/grails/grails-co re / blob / master / grails-logging / src / main / groovy / org / codehaus / groovy / grails / compiler / logging / LoggingTransformer.java ,但使用我自己的 org实现。 slf4j.Logger
对象。将Java.class放在以下包中是至关重要的: org.codehaus.groovy.grails.compiler
as
Grails扫描在此包中使用@AstTransformer注解的类。 ( Graeme Rocher )
并将其打包到JAR以及 MANIFEST .MF
文件在 META-INF /
文件夹中。 Graeme Rocher 指出: $ b $ META-INF / services
b
您不需要META-INF / services的东西,我会将其删除,因为它可能会使事情变得复杂。
所以,我猜这个陈述与我的具体问题更相关,因为我的插件中只有一个 @AstTransformer
类,但这只是一个猜测。我还没有搜索关于这个主题的更多信息。 插件并放在 lib /
目录下。在此之后,您应该可以执行 grails clean
, grails compile
和 grails package-plugin 。
如果您想要替换 log
实现,就像我一样,应该从指定项目的类路径中排除 grails-logging
和 grails-plugin-log4j
JAR。这在 BuildConfig.groovy
文件中完成:
inherits(全球){
不包括grails-plugin-log4j,grails-logging
}
现在安装您的插件 grails install-plugin \path\to\plugin.zip
,并且everthing应该按预期工作。
希望这有助于......
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.Logger
object instead of the usual org.apache.commons.logging.Log
object 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.Logger
object. It is crucial that you place the Java.class under the following package: org.codehaus.groovy.grails.compiler
as
Grails scans for classes that are annotated with @AstTransformer in this package. (Graeme Rocher)
and pack it into a JAR along with its MANIFEST.MF
file within the META-INF/
folder. A META-INF/services
directory with all its stuff is not needed as Graeme Rocher stated:
You do not need the META-INF/services stuff and I would remove it as it is probably complicating matters.
So, I guess this statement was more related to my specific problem as I only have one @AstTransformer
class 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 do grails clean
, grails compile
and grails package-plugin
.
If you want to replace the log
implementation, as I did, you should exclude the grails-logging
and grails-plugin-log4j
JARs from your designated project's classpath. This is done in the BuildConfig.groovy
file:
inherits("global") {
excludes "grails-plugin-log4j", "grails-logging"
}
Now install your plugin grails install-plugin \path\to\plugin.zip
and everthing should work as expected.
Hope this helps...
这篇关于Grails 2.1.1 - 如何用AstTransformer开发一个插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!