如何在Gradle Kotlin构建中配置processResources任务

时间:2016-10-17 21:31:51

标签: gradle kotlin gradle-kotlin-dsl

我在基于groovy的构建脚本中有以下内容。如何在基于kotlin的脚本中执行相同的操作?

processResources {

    filesMatching('application.properties'){
        expand(project.properties)
    }

}

3 个答案:

答案 0 :(得分:6)

在较新版本的Kotlin DSL和Gradle中更新API,您可以执行以下操作:

import org.gradle.language.jvm.tasks.ProcessResources

plugins {
  java
}

tasks {
  "processResources"(ProcessResources::class) {
    filesMatching("application.properties") {
      expand(project.properties)
    }
  }
}

还有:

val processResources by tasks.getting(ProcessResources::class) {
  filesMatching("application.properties") {
    expand(project.properties)
  }
}

答案 1 :(得分:4)

我认为任务应该如下:

修改:根据comment存储库中的gradle/kotlin-dsl。任务配置应该以这种方式工作:

import org.gradle.language.jvm.tasks.ProcessResources

apply {
    plugin("java")
}

(tasks.getByName("processResources") as ProcessResources).apply {
    filesMatching("application.properties") {
        expand(project.properties)
    }
}

哪个很难看。所以我建议为此目的使用以下效用函数,直到上游完成:

configure<ProcessResources>("processResources") {
    filesMatching("application.properties") {
        expand(project.properties)
    }
}

inline fun <reified C> Project.configure(name: String, configuration: C.() -> Unit) {
    (this.tasks.getByName(name) as C).configuration()
}

答案 2 :(得分:1)

为什么不使用&#34; withType&#34; ? 我只是说(恕我直言)

tasks {
  "processResources"(ProcessResources::class) {
.. 
}

看起来比

好多了
tasks.withType<ProcessResources> {
    //from("${project.projectDir}src/main/resources")
    //into("${project.buildDir}/whatever/")
    filesMatching("*.cfg") {
        expand(project.properties)
    }
}

所以,

  @IBAction func btnplayClicked(sender: AnyObject) {
let url = ModuleServices.getFullRequestUrlForStream(playerClass, playerCode: sessionInfo!.code, fileName: "\(currentIndex).mp4")
     let path = url
    let url = NSURL(string: path)
    // Create an AVPlayer, passing it the HTTP Live Streaming URL.
    let player = AVPlayer(URL: url!)

    // Create a new AVPlayerViewController and pass it a reference to the player.
    let controller = AVPlayerViewController()
    controller.player = player

    // Modally present the player and call the player's play() method when complete.
    self.presentViewController(controller, animated: true)
    {
        player.play()
    }
   }