I have a `copySpec` that only defines exclusions (...
# community-support
n
I have a
copySpec
that only defines exclusions (with
exclude
) and I'd like to reuse it in multiple tasks that have differing from clauses. Is there a way to do so? (more details in ๐Ÿงต )
โœ… 1
I tried using
with(reusableCopySpec)
on the root copy task, but that registers a new child spec without any from clause, so it doesn't apply to anything. I also tried:
Copy code
with(reusableCopySpec.from(<task-specific input>))
but that changes the state of the copySpec itself, leading to every task that reuses it to have all inputs from all tasks.
v
I don't think a
CopySpec
is what you should use there. Iirc the copy spec is self-contained, so defines some from and for those excludes and you can reuse it. If you want to reuse excludes like that, you should probably store the configuration block and reuse that in the other copyspecs where you use the
from
.
Like
Copy code
val foo: CopySpec.() -> Unit = {
    exclude("...")
}

tasks.jar {
    from("...") {
        foo()
    }
}
Or
Copy code
fun CopySpec.foo() {
    exclude("...")
}

tasks.jar {
    from("...") {
        foo()
    }
}
n
Works like a charm. Thank you! Through all the iterations I've gone through, I remember having had the
CopySpec.() -> Unit
signature at some point, but I must've done something wrong when applying it.
๐Ÿ‘Œ 1