如何使用 build.gradle.kts 中的任务修改源代码?

How to modify source code using task in build.gradle.kts?

我有一个脚本可以在生成的源代码中找到必要的行时打印,但我也必须删除这些行,我想知道我该怎么做? 此任务似乎适用于 groovy,但仅打印找到的 Kotlin 值。

tasks.register<Copy>("filter") {
    from("src/generate-swagger/java") {
        filter { line ->
            if (line.contains("com.magazine.report.exception")) {
                println("found import")
                return@filter null
            }
            if (line.contains("throw new ReportException")) {
                println("found exception")
                return@filter line.replace("throw new ReportException", "throw new RuntimeException")
            }
            return@filter line
        }
    }
    into("generated/java")
    dependsOn("generate")
}

我能够通过以下操作修改内容:

tasks.register<Copy>("filter") {
    from("src/generate-swagger/java") {
        filter { line ->
            if (line.contains("com.magazine.report.exception")) {
                return@filter ""
            }
            if (line.contains("throw new ReportException")) {
                return@filter line.replace("throw new ReportException", "throw new RuntimeException")
            }
            return@filter line
        }
    }
    into("generated/java")
    dependsOn("generate")
}