Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure the processResources task in a Gradle Kotlin build

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

}
like image 560
KevinS Avatar asked Oct 17 '16 21:10

KevinS


2 Answers

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.

like image 192
SoBeRich Avatar answered Sep 28 '22 03:09

SoBeRich


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()
}
like image 25
Ruslan Avatar answered Sep 28 '22 02:09

Ruslan