How to configure the processResources task in a Gradle Kotlin build
Asked Answered
P

3

15

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

}
Playboy answered 17/10, 2016 at 21:31 Comment(0)
T
10

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()
}
Telmatelo answered 18/10, 2016 at 6:29 Comment(3)
That looks like it should work but I get an error. I think it could be a bug. Logged it here: discuss.gradle.org/t/…Playboy
See the answer by @Heartthrob for a cleaner answer https://mcmap.net/q/757960/-how-to-configure-the-processresources-task-in-a-gradle-kotlin-buildStarwort
It was helpful. I'd like someone to make an groovy to kotlin-dsl converter!Costive
H
29

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.

Heartthrob answered 13/1, 2018 at 16:11 Comment(2)
This should honestly be the correct answer. You would only need "processResources"(ProcessResources::class) if you weren't in a context where the Java plugin is applied.Starwort
@Starwort also note what often escapes attention that in configures all tasks with such type for all sourceSets (e.g. also for test and any custom one). Which is a common mistake when people do compileJava {} it only affects JavaCompile task for main sourceSet, so it always should be withType<...> for any type of tasks which has their instances run for different sourceSetsHeartthrob
T
10

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()
}
Telmatelo answered 18/10, 2016 at 6:29 Comment(3)
That looks like it should work but I get an error. I think it could be a bug. Logged it here: discuss.gradle.org/t/…Playboy
See the answer by @Heartthrob for a cleaner answer https://mcmap.net/q/757960/-how-to-configure-the-processresources-task-in-a-gradle-kotlin-buildStarwort
It was helpful. I'd like someone to make an groovy to kotlin-dsl converter!Costive
M
9

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)
  }
}
Mustachio answered 9/9, 2017 at 1:58 Comment(1)
This is also IMO anti-pattern to get reference to TaskProvider<..> when you are not using that reference. aslo with "newer release" (after time of your anserwer) you can just do tasks.processResources {} or tasks { processResources {} } generated accessors are "lazy" so it has all the benefits and do downsides.Heartthrob

© 2022 - 2024 — McMap. All rights reserved.