简体   繁体   English

Groovy DSL“运行”的 Gradle Kotlin DSL 等价物?

[英]Gradle Kotlin DSL equivalent for Groovy DSL 'run'?

I am trying to build a simple JavaFX 11 program with Kotlin and Java 11, using Gradle, following the instructions here .我正在尝试按照此处的说明使用 Gradle 使用 Kotlin 和 Java 11 构建一个简单的 JavaFX 11 程序。 However, this page uses Gradle's Groovy DSL, and I am trying to use the Kotlin DSL.但是,此页面使用 Gradle 的 Groovy DSL,而我正在尝试使用 Kotlin DSL。 Surprisingly, my Google searches have not turned up a document that maps each Groovy construct to its equivalent Kotlin construct or explains in general how to convert Groovy DSL code to equivalent Kotlin DSL code.令人惊讶的是,我的 Google 搜索并没有找到将每个 Groovy 构造映射到其等效 Kotlin 构造的文档,也没有大致解释如何将 Groovy DSL 代码转换为等效的 Kotlin DSL 代码。 (This seems like a big oversight in the Gradle documentation!). (这似乎是 Gradle 文档中的一个大疏忽!)。

In particular, this document contains the following Groovy code:特别是,本文档包含以下 Groovy 代码:

compileJava {
    doFirst {
        options.compilerArgs = [
            '--module-path', classpath.asPath,
            '--add-modules', 'javafx.controls'
        ]
    }
}

run {
     doFirst {
         jvmArgs = [
             '--module-path', classpath.asPath,
             '--add-modules', 'javafx.controls'
         ]
    }
}

As far as I can tell, the Kotlin equivalent to the first part appears to be:据我所知,相当于第一部分的 Kotlin 似乎是:

tasks.withType<JavaCompile> {
    options.compilerArgs.addAll(arrayOf(
        "--module-path", classpath.asPath,
        "--add-modules", "javafx.controls"
    ))
}

However, I have not been able to figure out what the Kotlin DSL equivalent to the second part is.但是,我一直无法弄清楚与第二部分等效的 Kotlin DSL 是什么。 Note that 'run' is a standard function extension in Kotlin's standard library, so it does not appear that the Kotlin version of this code can use the name 'run' for the same purpose in the Kotlin DSL.请注意,'run' 是 Kotlin 标准库中的标准函数扩展,因此该代码的 Kotlin 版本似乎无法在 Kotlin DSL 中出于相同目的使用名称 'run'。

(Note: I considered trying to use a plugin for the JavaFX support (as described by this page, for instance), but the plugin seems quite complicated to use, and I already am having enough problems with the number of complications in this project that I am hesitant to introduce a very-lightly-documented open-source plugin into the mix. I really am trying to produce the simplest possible "Hello, World" program in JavaFX/Gradle at the moment, and this has so far seemed surprisingly difficult.). (注:我认为试图使用JavaFX的支持(如由一个插件页面,例如),但该插件似乎相当复杂的使用,我已经感到有在这个项目中,与并发症的数量不够的问题我很犹豫要不要在混合中引入一个非常简单的开源插件。目前我真的想在 JavaFX/Gradle 中生成最简单的“Hello, World”程序,到目前为止,这似乎非常困难.)

Any help would be appreciated.任何帮助,将不胜感激。

Using the configuration avoidance APIs, the equivalent to the second block is:使用配置避免 API,相当于第二个块是:

tasks.named<JavaExec>("run") {
    doFirst {
        jvmArgs = listOf("--module-path", classpath.asPath,"--add-modules", "javafx.controls")
    }
}

The key is that run has the JavaExec type, which like any task's type can be discovered by creating a task to print the class of the task that you then run:关键是 run 有JavaExec类型,它像任何任务的类型一样可以通过创建一个任务来发现你然后运行的任务的类:

tasks.register("getName") {
    doFirst {
        print("Class name: ${tasks["run"].javaClass}")
    }
}

Note that as your JavaFX application grows, you will need to specify additional modules like this:请注意,随着 JavaFX 应用程序的增长,您将需要指定其他模块,如下所示:

tasks.named<JavaExec>("run") {
    doFirst {
        jvmArgs = listOf("--module-path", classpath.asPath,
            "--add-modules", "javafx.base,javafx.controls,javafx.graphics")
    }
}

Surprisingly, my Google searches have not turned up a document that maps each Groovy construct to its equivalent Kotlin construct or explains in general how to convert Groovy DSL code to equivalent Kotlin DSL code.令人惊讶的是,我的 Google 搜索并没有找到将每个 Groovy 构造映射到其等效 Kotlin 构造的文档,也没有大致解释如何将 Groovy DSL 代码转换为等效的 Kotlin DSL 代码。

Please have a look at https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/ and esp.请查看https://guides.gradle.org/migrating-build-logic-from-groovy-to-kotlin/和 esp。 the Configuring tasks section. 配置任务部分。 According to that, I'd say the Kotlin DSL equivalent is据此,我会说 Kotlin DSL 等价物是

tasks.named<JavaExec>("run").doFirst {
    jvmArgs = listOf('--module-path', classpath.asPath, '--add-modules', 'javafx.controls')
}

With Gradle 5.0 and kotlin-dsl 1.0, tasks that are registered or created by plugins can be statically accessed through the tasks container ( TaskContainer . There is this example provided in the release notes :使用 Gradle 5.0 和kotlin-dsl 1.0,可以通过tasks容器( TaskContainer静态访问由插件注册或创建的tasks发行说明中提供了此示例:

 plugins { java } tasks { named<Test>("test") { testLogging.showStacktraces = true } }

you can now write:你现在可以写:

 plugins { java } tasks { test { testLogging.showStacktraces = true } }

For your example, you are most likely using the application plugin, which registers the run task so you can configure it in a similar matter.对于您的示例,您最有可能使用application插件,它会注册run任务,以便您可以在类似的情况下对其进行配置。 One issue to be aware of is that run clashes with the Kotlin stdlib run method so you need to apply some workaround to make sure it gets invoked (see gradle/kotlin-dsl/issues/1175 )要注意的一个问题是run与 Kotlin stdlib run方法发生冲突,因此您需要应用一些解决方法以确保它被调用(请参阅gradle/kotlin-dsl/issues/1175

tasks {
  compileJava {
    doFirst {
      jvmArgs = listOf("--module-path", classpath.asPath,
          "--add-modules", "javafx.base,javafx.controls,javafx.graphics")
    }
  }

  (run) {
    doFirst {
      jvmArgs = listOf(
        "--module-path", classpath.asPath,
        "--add-modules", "javafx.controls"
      )
    }
  }
}

The other answers show how you can use the name, type, or combination to query the container for specific tasks.其他答案显示了如何使用名称、类型或组合来查询特定任务的容器。

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

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