java - 如何将 DSL 语法转换为脚本语法

标签 java gradle groovy dsl

下面是 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/

相关文章:

java - macOS High Sierra 上的 Gradle - Java 9

java - 在 groovy 脚本 (soapui) 中导入 jar 时出错

java - 从 Android 中的 apk 文件中读取 Activity 名称

java - 在几次 next() 调用后从 ResultSet 获取 RowCount

java - Gradle - 如果项目仍然具有 SNAPSHOT 依赖项,则抛出异常

java - 如何在 IntelliJ IDEA 中修复 "unsupported class file major version 60"?

testing - 如何在jmeter的循环 Controller 中使用jsr223变量

java - PowerMock:模拟具有不同参数的同一方法的多次调用表现异常

java - 如何在 Java 而不是 XML 中以编程方式设置 View 的重力和布局重力?

gradle - 如何管理 gradle 模块项目和应用程序之间的复合构建