Getting a resource from the correct classloader within task

I’ve a resource in buildSrc/src/main/resources which I want to load with getResourceAsStream(...)

I’m currently doing the following in my kts-file:

fun registerApplyPatchToRepoTask(patch: String) =
    tasks.registering {
        group = "migration (internal)"
        doLast {
            val patchInputStream = CleanJrxmlsTask::class.java.getResourceAsStream(patch)
            val patchPath = layout.buildDirectory.file("tmp/repo.patch").get().asFile.toPath()
            Files.copy(patchInputStream, patchPath, StandardCopyOption.REPLACE_EXISTING)

            exec {
                commandLine("git", "am", patchPath)
                workingDir(layout.buildDirectory.dir("repo"))
            }
        }
    }

Where CleanJrxmlsTask is a class located in buildSrc/src/main/kotlin.

My issue: As the task has nothing to do with CleanJrxmlsTask, CleanJrxmlsTask::class.java.getResourceAsStream(patch) doesn’t feel clean. But I cannot use java.getResourceAsStream(patch), as this would refer to org.gradle.api.DefaultTask_Decorated, so the code would try to load the resource from the wrong module.

So is there another object / class I could use?

I guess the clean solution would be to not use an ad-hoc task, but move your task to a proper class.
You could then also inject FileSystemOperations and use its copy { ... } or sync { ... } function.