6

In Groovy, I overwrite a task like this:

task jar(overwrite: true) {
    ...
}

How do I do that with Kotlin-dsl? I know that I can create a task like this:

tasks {
     val jar by creating {
          ...
     }
}

but I can't find the equivalent way to declare it as overwrite, this leads to an error

xeruf
  • 2,602
  • 1
  • 25
  • 48

1 Answers1

6

By opening an issue on the kotlin-dsl github I found the correct syntax:

tasks.replace("jar") {
    ...
}

However, this is the old way and does not work within a tasks { } block, so this issue will be further tracked here

xeruf
  • 2,602
  • 1
  • 25
  • 48
  • The issue links to https://github.com/gradle/kotlin-dsl-samples/pull/1000 which has been merged, so it should work, but I haven't tried yet. – xeruf May 30 '20 at 12:14
  • As of Gradle 7.3.3, this doesn't seem to work. I get this error: `Type mismatch: inferred type is () -> Unit but Class was expected` with an arrow pointing to the the open curly brace. If I change it to `tasks.replace("build").doLast { ... }` that particular error goes away, but then I get another error: `Cannot call Task.dependsOn(Object...) on task ':build' after task has started execution.` – Dave Yarwood Jan 17 '22 at 21:17