如何将Gradle Ant Java任务转换为Kotlin

时间:2019-03-06 18:43:11

标签: java gradle kotlin ant

我有一个gradle ant任务,可以启动H2数据库。构建脚本如下所示:

apply plugin: 'java'

repositories {
    mavenCentral()
}

dependencies {
    runtime 'com.h2database:h2:1.3.168'
}

task startH2Db {
    group = 'database'
    description='Starts the H2 TCP database server on port 9092 and web admin on port 8082'
    doLast{
        ant.java( fork:true, spawn:true, classname:'org.h2.tools.Server', dir:projectDir){
            arg(value: "-tcp")
            arg(value: "-web")
            arg(value: "-tcpPort")
            arg(value: "9092")
            arg(value: "-webPort")
            arg(value: "8082")
            arg(value: "-webAllowOthers")
            classpath {
                pathelement(path:"${sourceSets.main.runtimeClasspath.asPath}")
            }
        }
    }
}

鉴于Gradle现在支持Kotlin,我决定尝试将此build.gradle转换为build.gradle.kts文件。

我正努力在Kotlin中找到有关如何执行此操作的文档。我已经找到了其他蚂蚁任务的示例,但是上面的args没有。我已经知道了:

plugins {
    java
}

repositories {
    mavenCentral()
}

dependencies {
    runtime ("com.h2database:h2:1.3.168")
}

tasks {
    register("startH2Database") {
        group = "database"
        description = "Starts the H2 TCP database server on port 9092 and web admin on port 8082"
        doLast {
            ant.withGroovyBuilder {
            "java"("fork" to true, "spawn" to true, "classname" to "org.h2.tools.Server", "dir" to projectDir)
            }
        }
    }
}

如何配置args和classpath?除了这里列出的内容之外,还有其他文档吗?https://docs.gradle.org/current/userguide/ant.html

1 个答案:

答案 0 :(得分:2)

您可以在Gradle Kotlin DSL存储库中查看更多示例,例如 https://github.com/gradle/kotlin-dsl/blob/master/samples/ant/build.gradle.kts

所以您的Ant呼叫可能看起来像

ant.withGroovyBuilder {
  "java"( 
     "fork" to true, 
     "spawn" to true, 
     "classname" to "org.h2.tools.Server", 
     "dir" to projectDir
   ){
      "arg"("value" to "-tcp")
      "arg"("value" to "-web")
      "arg"("value" to "-tcpPort")
      "arg"("value" to "9092")
      "arg"("value" to "-webPort")
      "arg"("value" to "8082")
      "arg"("value" to "-webAllowOthers")
      "classpath" {
        "pathelement"(
                "path" to configurations["runtime"].asPath
            )
      }
   }
}