@GrabConfig的替代方案?

时间:2013-05-09 21:04:27

标签: unit-testing gradle groovy javamail

我正在使用javax.mail库发送可能包含或不包含附件的电子邮件。

我也使用Groovy 2.0.6编写这个脚本,并在Eclipse中开发它并使用Gradle 1.5运行单元测试。我正在编写的脚本将在未来的许多不同位置部署在jar中。因此,需要从我的脚本中引用javax.mail,而不是仅手动添加到机器的类路径中。

为此,我在我的脚本中使用以下语句:

@GrabConfig(systemClassLoader=true)
@Grab(group='javax.mail', module='mail', version='1.4.7')

我的问题是,当包含@GrabConfig语句时,我无法使用Gradle运行单元测试。它仅使用@Grab语句运行良好,但当@GrabConfig在那里时失败。我收到的错误信息是:

:compileJava UP-TO-DATE
:compileGroovy
startup failed:

General error during conversion: No suitable ClassLoader found for grab

java.lang.RuntimeException: No suitable ClassLoader found for grab
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.codehaus.groovy.reflection.CachedConstructor.invoke(CachedConstructor.java:77)
        at org.codehaus.groovy.runtime.callsite.ConstructorSite$ConstructorSiteNoUnwrapNoCoerce.callConstructor(ConstructorSite.java:102)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallConstructor(CallSiteArray.java:57)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:182)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callConstructor(AbstractCallSite.java:190)
        at groovy.grape.GrapeIvy.chooseClassLoader(GrapeIvy.groovy:181)
        at groovy.grape.GrapeIvy$chooseClassLoader.callCurrent(Unknown Source)
        at groovy.grape.GrapeIvy.grab(GrapeIvy.groovy:247)
        at groovy.grape.Grape.grab(Grape.java:141)
        at groovy.grape.GrabAnnotationTransformation.visit(GrabAnnotationTransformation.java:312)
        at org.codehaus.groovy.transform.ASTTransformationVisitor$3.call(ASTTransformationVisitor.java:319)
        at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:903)
        at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:566)
        at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:542)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:519)
        at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:498)
        at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:118)
        at org.gradle.api.internal.tasks.compile.ApiGroovyCompiler.execute(ApiGroovyCompiler.java:39)
        at org.gradle.api.internal.tasks.compile.daemon.CompilerDaemonServer.execute(CompilerDaemonServer.java:52)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
            at java.lang.reflect.Method.invoke(Method.java:597)
            at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
            at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
            at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
            at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
            at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
            at java.lang.Thread.run(Thread.java:662)

    1 error

    :compileGroovy FAILED

    FAILURE: Build failed with an exception.

根据No suitable classloader found for grab@GrabConfig使代码不可测试。

对于我的情况,是否有@GrabConfig的替代方案?

2 个答案:

答案 0 :(得分:2)

您可以使用gradle-one-jar插件将自己的和第三方代码打包到一个可执行的Jar中。或者,您可以使用Gradle的application插件创建带有启动脚本的Zip分发。

答案 1 :(得分:1)

您可以在build.gradle中禁用葡萄,如下所示:

test {                                        
  systemProperty 'groovy.grape.enable', 'false'  
}

compileGroovy {
  groovyOptions.forkOptions.jvmArgs = [ '-Dgroovy.grape.enable=false' ]
}
compileTestGroovy {
  groovyOptions.forkOptions.jvmArgs = [ '-Dgroovy.grape.enable=false' ]
}