简体   繁体   English

如何在Grails项目中包含Groovy

[英]How to include groovy in a grails project

I'm an idiot or something, and I don't know how to add groovy into src/groovy and make it work. 我是个白痴,还是什么,我不知道如何将groovy添加到src / groovy并使之正常工作。 Lets say I got some meta stuff in my bootstrap and I want to move those calls to a class I can call from unit tests or wherever based on this question: Correct way to metaprogram in grails so its available in unit tests 可以说我在引导程序中有一些元数据,我想将这些调用移到可以从单元测试或任何地方基于以下问题调用的类: 正确的grails元编程方式,因此可以在单元测试中使用

So if I put this in my bootstrap (which has import myproject.* at the top) it works. 因此,如果我将其放在我的引导程序中(在顶部具有import myproject.* ),它将起作用。

ExpandoMetaClass.enableGlobally()
Integer.metaClass.gimmeAP = {->return 'p'}
assert 3.gimmeAP() == 'p'

So I'm using STS and I go into src/groovy and say "New GroovyClass" add it to package myproject and fill it in as so: 所以我正在使用STS,然后进入src / groovy并说“ New GroovyClass”将其添加到包myproject中,并按以下方式填写:

package yakit

class MetaThangs {
  def doMetaThangs() {
    ExpandoMetaClass.enableGlobally()

    Integer.metaClass.gimmeAP = {->return 'p'}
  }
}

Then I call this in bootstrap: 然后我将其称为bootstrap:

MetaThangs.doMetaThangs()
assert 3.gimmeAP() == 'p'

I get the error: 我得到错误:

Running Grails application..
2012-02-07 14:12:13,332 [main] ERROR context.GrailsContextLoader  - Error executing bootstraps: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
org.codehaus.groovy.runtime.InvokerInvocationException: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at grails.util.Environment.evaluateEnvironmentSpecificBlock(Environment.java:251)
at grails.util.Environment.executeForEnvironment(Environment.java:244)
at grails.util.Environment.executeForCurrentEnvironment(Environment.java:220)
at org.grails.tomcat.TomcatServer.start(TomcatServer.groovy:212)
at grails.web.container.EmbeddableServer$start.call(Unknown Source)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy:158)
at _GrailsRun_groovy$_run_closure5_closure12.doCall(_GrailsRun_groovy)
at _GrailsSettings_groovy$_run_closure10.doCall(_GrailsSettings_groovy:280)
at _GrailsSettings_groovy$_run_closure10.call(_GrailsSettings_groovy)
at _GrailsRun_groovy$_run_closure5.doCall(_GrailsRun_groovy:149)
at _GrailsRun_groovy$_run_closure5.call(_GrailsRun_groovy)
at _GrailsRun_groovy.runInline(_GrailsRun_groovy:116)
at _GrailsRun_groovy.this$4$runInline(_GrailsRun_groovy)
at _GrailsRun_groovy$_run_closure1.doCall(_GrailsRun_groovy:59)
at RunApp$_run_closure1.doCall(RunApp:33)
at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
at gant.Gant.withBuildListeners(Gant.groovy:427)
at gant.Gant.this$2$withBuildListeners(Gant.groovy)
at gant.Gant$this$2$withBuildListeners.callCurrent(Unknown Source)
at gant.Gant.dispatch(Gant.groovy:415)
at gant.Gant.this$2$dispatch(Gant.groovy)
at gant.Gant.invokeMethod(Gant.groovy)
at gant.Gant.executeTargets(Gant.groovy:590)
at gant.Gant.executeTargets(Gant.groovy:589)
Caused by: groovy.lang.MissingMethodException: No signature of method: static lrnmeta.MetaThangs.doMetaThangs() is applicable for argument types: () values: []
Possible solutions: doMetaThangs()
at BootStrap$_closure1.doCall(BootStrap.groovy:5)
... 26 more
Application context shutting down...
Application context shutdown.

Is it really telling me I should be typing 'doMetaThangs()' instead of 'doMetaThangs()'? 真的是在告诉我我应该输入“ doMetaThangs()”而不是“ doMetaThangs()”吗?

UPDATE: based on @mkoryak answer I have tried changing the method declaration in MetaThangs.groovy to: 更新:基于@mkoryak的答案,我试图将MetaThangs.groovy中的方法声明更改为:

static def doMetaThangs(){
  ...
}

It didn't work at first, but eventually it came around. 起初它没有用,但最终又来了。

doMetaThangs is not static, but you are calling it as if it was. doMetaThangs不是静态的,但是您好像在调用它一样。

either add the static modifier to the method, or call it on an instance of the class, not the class. 将static修饰符添加到方法中,或在类的实例(而不是类)上调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM