在Grails服务中放置@Grab注释的位置

时间:2013-05-03 16:12:47

标签: grails dependencies

我有以下groovy代码,我在grails控制台插件上执行它。它工作正常。 但是,我想在grails的服务中重用它。

@Grab( 'net.sourceforge.nekohtml:nekohtml:1.9.18' )

import org.cyberneko.html.parsers.SAXParser
def html = '''<p>
             |  I wanted to try to <span id="blk1">match</span> the inner part
             |  of the string<span id="blk2"> between </span> the span tags <span>where</span>
             |</p>'''.stripMargin()

def content = new XmlSlurper( new SAXParser() ).parseText( html )

List<String> spans = content.'**'.findAll { it.name() == 'SPAN' && it.@id?.text()?.startsWith( 'blk' ) }*.text()

ATTEMPT1:

    import groovy.grape.Grape;
    @Grab( 'net.sourceforge.nekohtml:nekohtml:1.9.18' )
class MyService {
//..

}

ERR:

*** java.lang.instrument ASSERTION FAILED ***: "!errorOutstanding" with message can't create byte arrau at ../../../src/share/instrument/JPLISAgent.c line: 793

我试图在执行上面代码的方法之前放置@Grab注释,我有一个错误。 我试着在课前放@Grab,结果相同;这也是一个错误。

我在哪里放@Grab注释。

ATTEMPT2:

当我在BuildConfig.groovy中添加上述依赖项时,

runtime( 'net.sourceforge.nekohtml:nekohtml:1.9.18')

ERR: 我收到以下错误:

grails> --stacktrace -Dserver.port=9090 run-app
| Packaging Grails application.....
| Error Error running script --stacktrace -Dserver.port=9090 run-app: org/apache/xerces/dom/DeferredElementNSImpl (NOTE: Stack trace has been filtered. Use --verbose to see entire trace.)
java.lang.NoClassDefFoundError: org/apache/xerces/dom/DeferredElementNSImpl
    at org.apache.xerces.dom.DeferredDocumentImpl.getNodeObject(Unknown Source)
    at org.apache.xerces.dom.DeferredDocumentImpl.synchronizeChildren(Unknown Source)
    at org.apache.xerces.dom.CoreDocumentImpl.getDocumentElement(Unknown Source)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at _GrailsPackage_groovy$_run_closure5.doCall(_GrailsPackage_groovy:74)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding.withTargetEvent(GantBinding.groovy:90)
    at org.codehaus.gant.GantBinding.this$4$withTargetEvent(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at _GrailsPackage_groovy$_run_closure2.doCall(_GrailsPackage_groovy:58)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding.withTargetEvent(GantBinding.groovy:90)
    at org.codehaus.gant.GantBinding.this$4$withTargetEvent(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.processClosure(GantMetaClass.java:81)
    at org.codehaus.gant.GantMetaClass.processArgument(GantMetaClass.java:95)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:128)
    at RunApp$_run_closure1.doCall(RunApp.groovy:30)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantMetaClass.invokeMethod(GantMetaClass.java:133)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16_closure18.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding.withTargetEvent(GantBinding.groovy:90)
    at org.codehaus.gant.GantBinding.this$4$withTargetEvent(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy:185)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at org.codehaus.gant.GantBinding$_initializeGantBinding_closure5_closure16.doCall(GantBinding.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at gant.Gant$_dispatch_closure5.doCall(Gant.groovy:381)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy:415)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at gant.Gant$_dispatch_closure7.doCall(Gant.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    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$this$2$withBuildListeners.callCurrent(Unknown Source)
    at gant.Gant.dispatch(Gant.groovy:415)
    at gant.Gant.this$2$dispatch(Gant.groovy)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at gant.Gant.invokeMethod(Gant.groovy)
    at gant.Gant.executeTargets(Gant.groovy:591)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
    at gant.Gant.executeTargets(Gant.groovy:590)
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243)
| Error Error running script --stacktrace -Dserver.port=9090 run-app: org/apache/xerces/dom/DeferredElementNSImpl

ATTEMPT3: 同样,在BuildConfig.groovy中添加依赖项并没有解决问题。

   dependencies {
        //....
       // .......
       compile( 'net.sourceforge.nekohtml:nekohtml:1.9.18'){
            excludes 'xercesImpl'
        }
    }

参考:http://www.grailsforum.co.uk/showthread.php/2143-New-to-grails-problem-with-JAVA-jar-dependencies#post_message_2323

即使我使用上面的解决方案,它只在编译时有效。

ATTEMPT4 : 我执行以下命令来添加依赖项;但徒劳无功:

grails install-dependency net.sourceforge.nekohtml:nekohtml:1.9.18 --dir=lib

ERR 我得到了,这个错误信息:

 Packaging Grails application.....
| Error Error running script  run-app: org/apache/xerces/dom/DeferredElementNSImpl (Use --stacktrace to see the full trace)

1 个答案:

答案 0 :(得分:0)

放置所有库的最佳做法:

  1. lib文件夹中,然后是参考资料,或者在

  2. in conf/BuildConfig.groovy
  3. 但是,如果您想使用Grape,请使用以下语法:

    import groovy.grape.Grape
    Grape.grab(group:'net.sourceforge.nekohtml', module:'nekohtml', version:'1.9.18')
    

    groovy.grape.Grape.initGrape()
    @Grab(group='net.sourceforge.nekohtml', module='nekohtml', version='1.9.18')
    public class MyService{
    
    
    }