How to configure the processResources task in a Gradle Kotlin build

安稳与你 提交于 2020-12-08 15:00:21

问题


I have the following in a groovy-based build script. How do I do the same in a kotlin-based script?

processResources {

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

}

回答1:


I think task should look like:

Edit: According this comment in gradle/kotlin-dsl repository. Task configuration should work this way:

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

apply {
    plugin("java")
}

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

Which is pretty ugly. So i suggest following utility function for this purpose, until one upstream done:

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:


Why not to just use "withType" ? I just mean (IMHO)

tasks {
  withType<ProcessResources> {
.. 
}

looks much better than

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

So,

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

EDIT:

With newer release you can just do:

tasks.processResources {}

or

tasks { processResources {} }

generated accessors are "lazy" so it has all the benefits and no downsides.




回答3:


With updates to the APIs in newer release of the Kotlin DSL and Gradle, you can do something like:

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

plugins {
  java
}

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

And also:

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


来源:https://stackoverflow.com/questions/40096007/how-to-configure-the-processresources-task-in-a-gradle-kotlin-build

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!