Use Gradle task functionality in another task

Viewed 28

I have a Gradle Tar task that creates an archive file with my project sources.

Now I need to duplicate this functionality but to add a file into the archive.

Is there a way to extend/inherit the original task functionality without duplicating the code?

Thanks.

1 Answers

Yes, use the with method. The API may look a bit weird as it says it takes a CopySpec as input, but a Tar task is actually also a CopySpec.

Example (Groovy DSL):

task myDefaultTar(type: Tar) {
    archiveBaseName = "default"
    from("$buildDir/default.txt")
}

task myOtherTar(type: Tar) {
    archiveBaseName = "other"
    with(myDefaultTar)
    from("$buildDir/additional_file.txt")
}

Related