下面是常规DSL中的build.gradle
,我希望将其转换为脚本语法:
plugins {
id 'application'
id 'java'
id 'com.github.johnrengelman.shadow' version '4.0.1'
}
allprojects {
apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'
repositories {
mavenCentral()
}
ext {
vertxVersion = '3.7.0'
commitTimestamp = {
return "git log -1 --pretty=format:%cd --date=format:%Y%m%d%H%M%S".execute().text.trim()
}
commitId = {
return "git rev-parse --short HEAD".execute().text.trim()
}
buildId = {
if (System.getenv("BUILD_ID") != null) return ".${System.getenv("BUILD_ID")}"
else return ""
}
}
group = 'com.pluralsight.docker-production-aws'
version = System.getenv("APP_VERSION") ?: "${commitTimestamp()}.${commitId()}${buildId()}"
sourceCompatibility = '1.8'
mainClassName = 'io.vertx.core.Launcher'
dependencies {
compile "io.vertx:vertx-core:$vertxVersion"
compile "io.vertx:vertx-hazelcast:$vertxVersion"
compile "io.vertx:vertx-service-discovery:$vertxVersion"
compile "io.vertx:vertx-dropwizard-metrics:$vertxVersion"
compile "com.typesafe:config:1.3.0"
compile "com.hazelcast:hazelcast-cloud:3.6.5"
testCompile "io.vertx:vertx-unit:$vertxVersion"
testCompile "junit:junit:4.12"
testCompile "org.assertj:assertj-core:3.5.2"
testCompile "com.jayway.awaitility:awaitility:1.7.0"
}
task copyDeps(type: Copy) {
from (configurations.runtime + configurations.testRuntime) exclude '*'
into '/tmp'
}
test {
testLogging {
events "passed", "skipped", "failed"
}
reports {
junitXml.enabled = true
junitXml.destination = file("${rootProject.projectDir}/build/test-results/junit")
html.enabled = false
}
}
}
task testReport(type: TestReport) {
destinationDir = file("${rootProject.projectDir}/build/test-results/html")
reportOn subprojects*.test
}
test {
dependsOn testReport
}
configure(subprojects - project(':microtrader-common')) {
shadowJar {
destinationDir = file("${rootProject.projectDir}/build/jars")
classifier = 'fat'
mergeServiceFiles {
include 'META-INF/services/io.vertx.core.spi.VerticleFactory'
}
}
}
task wrapper(type: Wrapper) {
gradleVersion = '4.10.2'
}
下面是groovy DSL中从上方开始的代码片段:
plugins {
id("com.github.johnrengelman.shadow") version "5.2.0"
}
我很难理解,如果下面是相应的脚本语法:
plugins({
id(
{
"com.github.johnrengelman.shadow",
version("5.2.0")
}
)
})
如何将DSL语法转换为脚本语法?因为脚本化语法对我来说更具可读性。.
id(String)
块中的plugins方法返回具有方法PluginDependencySpecImpl和version(String)
的apply(boolean)
。所以你只需要这样写:
plugins ({
id("com.github.johnrengelman.shadow").version("5.2.0")
})
工作中的模式称为Command Chain。
Groovy可让您省略方法调用参数周围的括号用于顶级语句。 “命令链”功能将其扩展为允许我们链接此类无括号的方法调用,要求既不要在参数周围加上括号,也不要在链接之间使用点电话。一般的想法是,像b c d这样的电话实际上是相当于a(b).c(d)。这也适用于多个参数,闭包参数,甚至是命名参数。
MrHaki有一个很好的解释,为什么它会起作用here。