Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

How to overwrite a task? #705

Closed
xeruf opened this issue Feb 3, 2018 · 9 comments
Closed

How to overwrite a task? #705

xeruf opened this issue Feb 3, 2018 · 9 comments

Comments

@xeruf
Copy link

xeruf commented Feb 3, 2018

I can't find any documentation or sample on how to overwrite a task.

@StefMa
Copy link
Contributor

StefMa commented Feb 3, 2018

I've tried it a little bit and came to this solution:

tasks("copy") {
  doLast {
    println("HelloWorld")
  }
}

tasks.replace("copy").doLast {
   println("Not Hello World")
}

This will print Not Hello World.

We have to use the TaskContainer#replace since there is no extension function yet in the Kotlin DSL:
https://github.com/gradle/kotlin-dsl/blob/6565d94a752d475b01b0afecdd4a3eb8b2155ce6/provider/src/main/kotlin/org/gradle/kotlin/dsl/ProjectExtensions.kt#L110-L140

I think we should add the extension functions to the ProjectExtensions class :)

@xeruf xeruf closed this as completed Feb 4, 2018
@xeruf xeruf reopened this Feb 4, 2018
@xeruf
Copy link
Author

xeruf commented Feb 4, 2018

Would be good to add a sample for this

@StefMa
Copy link
Contributor

StefMa commented Feb 4, 2018 via email

@eskatos
Copy link
Member

eskatos commented Feb 9, 2018

Closing as answered

@eskatos eskatos closed this as completed Feb 9, 2018
@StefMa
Copy link
Contributor

StefMa commented Feb 9, 2018 via email

@eskatos
Copy link
Member

eskatos commented Feb 12, 2018

The Gradle API provides TaskContainer#replace.

// TaskContainer.java
Task replace(String name);
<T extends Task> T replace(String name, Class<T> type);

There's no need for an extension on the Gradle Kotlin DSL side to make this usable.

@xeruf
Copy link
Author

xeruf commented Feb 12, 2018

But as it stands now, I can't find a way to override a task within a tasks block, but have to use a separate tasks.replace call, which is inconvenient

@eskatos
Copy link
Member

eskatos commented Feb 12, 2018

Good point @Xerus2000!
tasks {} block receiver is a NamedDomainObjectContainer<Task>.
The TaskContainer methods aren't available in there.

I opened #716 to account for the more general issue.

@elect86
Copy link
Contributor

elect86 commented May 29, 2020

But as it stands now, I can't find a way to override a task within a tasks block, but have to use a separate tasks.replace call, which is inconvenient

Any news on this?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants