Gant 如何能够通过名称引用目标,并且不会出现丢失属性异常?
在此处提供的代码中:Gant 文件,有以下代码:
target(dist: 'Create release artefacts') {
depends(test)
depends(jar)
}
target(run: 'Run the distributed jar') {
depends(dist)
Ant.java(jar: distributedJarPath, fork: 'true')
}
target(hello: 'Hello World') {
println("Hello World")
}
setDefaultTarget(dist)
我对它的原理感兴趣能够编写 setDefaultTarget(dist)
,并且不会收到有关 dist
作为缺失属性的异常。此代码片段是否在某个对象的上下文中进行评估,并且目标创建实例变量?
In the code presented here: Gant file, there is the following code:
target(dist: 'Create release artefacts') {
depends(test)
depends(jar)
}
target(run: 'Run the distributed jar') {
depends(dist)
Ant.java(jar: distributedJarPath, fork: 'true')
}
target(hello: 'Hello World') {
println("Hello World")
}
setDefaultTarget(dist)
I'm interested in how it's able to write setDefaultTarget(dist)
, and not receive an exception about dist
as a missing property. Is this snippet evaluated in the context of some object, and the targets create instance variables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要考虑两项:脚本的绑定以及目标的声明方式。
首先是绑定。 Groovy 脚本与 Groovy/Java 类有点不同。 Groovy 脚本允许未绑定的变量。这意味着编译脚本时不需要声明变量。当编译器看到这些未声明的变量引用之一时,它会将对变量的引用转换为对 binding.getVariable(variable name) (或类似内容)的调用。因此,脚本的最后一行在功能上等同于
第二行,即目标的声明。当声明目标时,Groovy 编译器实际上会看到一个方法调用:target(Map args, Closureclosure)(或类似的方法)。当此方法执行时,它会检查参数,提取目标的名称,并在绑定中按该名称存储对闭包的一些包装引用。近似值是......
当然,上面的代码是近似值,而不是真正的甘特代码。
所以本质上...
There are two items you need to consider, the Binding of the script, and how a target is declared.
First there is the binding. A Groovy script is a bit different from a Groovy/Java class. Groovy scripts allow for unbound variables. Meaning when you compile the script the variable need not be declared. When the compiler sees one of these undeclared variable references, it will convert the refrence to the variable under the covers to a call to binding.getVariable(variable name) (or something thereabout). So the last line of the script is functionally equivilant to
Second there is the is the declaration of the target. When the target is declared the Groovy compiler is actually seeing a method call: target(Map args, Closure closure) (or thereabout). When this method executes it examines the args, extracts the name of the target, and stores some wrapped reference to the closure by that name in the binding. An appropximation would be..
Of course the above code is an approximation and not the real gant code.
so in essence...