问题描述
我正在尝试静态编译groovy脚本以加快其执行速度,但是如果使用命令行参数,则无法使其正常工作.我的实际脚本更长,但是我用于此问题的单行脚本完美地再现了我的错误.
I am trying to statically compile a groovy script to speed up it's execution, but am not able to get it to work if command line arguments are used. My actual script is much longer, but the one-line script I use for this question perfectly reproduces my error.
使用以下脚本( test.groovy )
println(args.length)
这可以使用命令 groovyc test.groovy
进行编译,并通过Java命令 java -cp.;%GROOVY_HOME%\ lib \ * test
运行,并且只需打印使用的命令行参数的数量.
This can be compiled with the command groovyc test.groovy
and ran by the java command java -cp .;%GROOVY_HOME%\lib\* test
and will simply print the number of command line arguments used.
现在,如果我提供脚本( config.groovy )
Now, if I provide the script (config.groovy)
withConfig(configuration) {
ast(groovy.transform.CompileStatic)
}
并使用 groovyc -configscript config.groovy test.groovy
进行编译,我得到一个错误
and compile with groovyc -configscript config.groovy test.groovy
, I get an error
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
testing.groovy: 1: [Static type checking] - The variable [args] is undeclared.
@ line 1, column 9.
println(args.length)
^
1 error
此错误仅在我尝试静态编译时发生.我可以通过将脚本包装在一个类中并将代码放在main方法中来使其正常工作(当然,这是编译器对脚本执行的操作),但是当我尝试仅使用脚本时(这不是我更喜欢做什么).出于某种原因,变量 args 在静态编译时是未知的.我已经尝试过 this.args
,但仍然收到错误消息.如果我尝试声明args的类型( String [] args
),它将不再接收命令行参数.
This error only occurs when I attempt to compile statically. I can get it to work by wrapping the script in a class and putting my code in a main method (which, of course, is what the compiler does with a script), but not when I try to just use the script (which is what I prefer to do). For some reason, the variable args is unknown when compiled statically. I've tried this.args
but still receive the error. If I try to declare a type for args (String[] args
), it no longer receives the command line arguments.
以这种方式静态编译脚本时,是否还有办法获取命令行参数?
Is there a way to still get the command line arguments when a script is compiled statically this way?
我正在使用Java 8的Windows 7上使用Groovy版本2.4.10.
I am using Groovy version 2.4.10 on Windows 7 with Java 8.
推荐答案
在执行Groovy类和运行简单脚本方面有所不同.编译器只是将脚本包装在main方法中是不正确的,脚本主体将被复制到 run
方法中.
There's difference in executing Groovy class and running simple script. It's not correct that compiler simply wraps your script in main method, the body of the script will be copied into a run
method.
示例:
println(args.length)
将转换为
import org.codehaus.groovy.runtime.InvokerHelper
class Main extends Script {
def run() {
println(args.length)
}
static void main(String[] args) {
InvokerHelper.runScript(Main, args)
}
}
由于动态类型,它可以很好地编译.现在,如果我们向该类添加 @CompileStatic
批注,则会得到未声明变量的错误.
This compiles fine due to dynamic types.Now, if we add @CompileStatic
annotation to that class, we'll get the error of undeclared variable.
因此,您必须将代码包装在类中才能使用静态编译.
So, you have to wrap your code in class in order to use static compiling.
您可以在文档中了解有关脚本与类的更多信息.
You can read more about Scripts versus classes in documentation.
这篇关于使用命令行参数静态编译groovy脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!