使用GroovyShell从Gradle运行Groovy脚本:线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / cli / ParseException [英] Running Groovy script from Gradle using GroovyShell: Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

查看:1669
本文介绍了使用GroovyShell从Gradle运行Groovy脚本:线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / cli / ParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的Gradle构建脚本运行一个groovy命令行脚本。



我在我的Gradle脚本中使用了这段代码:

  def groovyShell = new GroovyShell(); 
groovyShell.run(file('script.groovy'),['arg1','arg2'] as String [])

直到我的Groovy脚本(script.groovy)使用CliBuilder类时,事情才能正常工作。然后我得到以下异常:


org.codehaus.groovy.runtime.InvokerInvocationException:java.lang.NoClassDefFoundError:org / apache / commons / cli / ParseException
...
导致:java.lang.ClassNotFoundException:org.apache.commons.cli.ParseException

我发现了很多有类似问题和错误的人,但是解决方案很难从我阅读的众多帖子中提取。很多人建议将commons-cli jar放在classpath中,但是对于GroovyShell这样做对我来说并不明显。另外,我已经在script.groovy中为我需要的库声明了@Grapes和@Grab,所以它应该有它需要的一切。

解决方案

感谢这个不被接受的SO答案,我终于找到了我需要做的事情:

  //定义我们自己的配置
配置{
addToClassLoader
}
//列出我们的shell脚本在他们的classLoader中需要的依赖关系:
dependencies {
addToClassLoader组:'commons-cli',name :'commons-cli',版本:'1.2'
}
//现在将这些依赖关系添加到根classLoader:
URLClassLoader loader = GroovyObject.class.classLoader
配置。 addToClassLoader.each {文件文件 - >
loader.addURL(file.toURL())
}

//现在没有更多的异常了:
def groovyShell = new GroovyShell() ;
groovyShell.run(file('script.groovy'),['arg1','arg2'] as String [])

你可以找到关于classLoaders的更多细节,以及为什么这个解决方案能够工作在此论坛帖子中

快乐脚本!



(在你回答我自己的问题之前,我赞同我的意见,


I want to run a groovy command-line script from my Gradle build script.

I'm using this code in my Gradle script:

def groovyShell = new GroovyShell();
groovyShell.run(file('script.groovy'), ['arg1', 'arg2'] as String[])

Things work fine until my Groovy script (script.groovy) uses the CliBuilder class. Then I get the following exception:

org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException ... Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException

I found lots of people with similar problems and errors, but "the solution" was difficult to extract from the numerous posts I read. Lots of people suggested putting the commons-cli jar on the classpath, but doing so for the GroovyShell was not at all apparent to me. Also, I had already declared @Grapes and @Grab for my required libraries in the script.groovy, so it should have everything it needed.

解决方案

Thanks to this unaccepted SO answer, I finally found what I needed to do:

//define our own configuration
configurations{
    addToClassLoader
}
//List the dependencies that our shell scripts will require in their classLoader:
dependencies {
    addToClassLoader group: 'commons-cli', name: 'commons-cli', version: '1.2'
}
//Now add those dependencies to the root classLoader:
URLClassLoader loader = GroovyObject.class.classLoader
configurations.addToClassLoader.each {File file ->
    loader.addURL(file.toURL())
}

//And now no more exception when I run this:
def groovyShell = new GroovyShell();
groovyShell.run(file('script.groovy'), ['arg1', 'arg2'] as String[])

You can find more details about classLoaders and why this solution works in this forum post.

Happy scripting!

(Before you downvote me for answering my own question, read this)

这篇关于使用GroovyShell从Gradle运行Groovy脚本:线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / cli / ParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆