2

我正在使用该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对于我的情况,有什么替代方法吗?

4

2 回答 2

2

You can use the gradle-one-jar plugin to package your own and third-party code into a single executable Jar. Alternatively, you can use Gradle's application plugin to create a Zip distribution with start scripts.

于 2013-05-10T07:16:58.767 回答
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' ]
}
于 2013-07-20T02:53:56.550 回答