下面是 groovy DSL 中的代码片段:
plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
}
<小时/>
这对我来说很难理解,如果下面是相应的脚本语法:
plugins({
id(
{
"com.github.johnrengelman.shadow",
version("5.2.0")
}
)
})
<小时/>
如何将 DSL 语法转换为脚本语法?因为脚本化语法对我来说更易读。
最佳答案
plugins 中的 id(String)
方法 block 返回 PluginDependencySpecImpl它具有方法 version(String)
和 apply(boolean)
。所以你只需要像这样写:
plugins ({
id("com.github.johnrengelman.shadow").version("5.2.0")
})
工作中的模式称为 Command Chain .
Groovy lets you omit parentheses around the arguments of a method call for top-level statements. "command chain" feature extends this by allowing us to chain such parentheses-free method calls, requiring neither parentheses around arguments, nor dots between the chained calls. The general idea is that a call like a b c d will actually be equivalent to a(b).c(d). This also works with multiple arguments, closure arguments, and even named arguments.
Haki 先生很好地解释了为什么它有效 here .
关于java - 如何将 DSL 语法转换为脚本语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60749533/