【问题标题】:How to modify source code using task in build.gradle.kts?如何使用 build.gradle.kts 中的任务修改源代码?
【发布时间】:2021-08-17 15:24:01
【问题描述】:

我有一个脚本可以在生成的源代码中找到必要的行时打印,但我也必须删除这些行,我想知道我该怎么做? 此任务似乎适用于 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")
}

【问题讨论】:

    标签: gradle build.gradle gradle-kotlin-dsl


    【解决方案1】:

    我能够通过以下方式修改内容:

    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")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 2017-06-11
      相关资源
      最近更新 更多