Execute a CopySpec in a project

Project has multiple APIs to create a CopySpec but I can’t seem to find an API on Project to execute it.

My project has a task with doLast-blocks, and in there, I want to execute a CopySpec I declared earlier. I did see the with-method to configure a Copy task with a CopySpec, but I can’t change the type of my task to Copy. How can I execute the CopySpec in a task that’s not a member of the AbstractCopyTask hierarchy?

I believe the Project.copy method is what you want. For example:

def mySpec = project.copySpec {
    ...
}
task myCopy {
    doLast {
        project.copy {
            with mySpec
        }
    }
}