简体   繁体   English

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

[英]How to convert gradle ant java task to kotlin

I have a gradle ant task that starts a H2 database. 我有一个gradle ant任务,可以启动H2数据库。 The build script looks like this: 构建脚本如下所示:

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}")
            }
        }
    }
}

Given that Gradle are now supporting Kotlin, I decided to try and convert this build.gradle into a build.gradle.kts file. 鉴于Gradle现在支持Kotlin,我决定尝试将build.gradle转换为build.gradle.kts文件。

I'm struggling to find documentation on how to do this in Kotlin. 我正努力在Kotlin中找到有关如何执行此操作的文档。 I've found examples of other ant tasks, but nothing with args like above. 我已经找到了其他蚂蚁任务的示例,但是上面的args没有。 I have got as far as this: 我到目前为止:

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)
            }
        }
    }
}

How do I configure the args and the classpath? 如何配置args和classpath? Is there any extra documentation other than what's listed here: https://docs.gradle.org/current/userguide/ant.html ? 除了此处列出的内容以外,是否还有其他文档: https : //docs.gradle.org/current/userguide/ant.html

You may check more examples in the Gradle Kotlin DSL repository, eg https://github.com/gradle/kotlin-dsl/blob/master/samples/ant/build.gradle.kts 您可以在Gradle Kotlin DSL存储库中查看更多示例,例如https://github.com/gradle/kotlin-dsl/blob/master/samples/ant/build.gradle.kts

So your Ant call may look like 所以您的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
            )
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM