gradle - 如何使用 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")
}
推荐阅读
- php - Laravel 抛出 Route [/profileUpdate/update] 未定义错误
- websocket - 将 Cypress 与“模拟套接字”一起使用
- java - 为什么 `parallelStream` 比 `CompletableFuture` 实现更快?
- ms-access - 访问:如何在设计视图中将所有字段添加到查询
- python - 如何在不使用循环的情况下计算python列表中的所有项目
- javascript - 在 Passage.render() 中使用 _.template(): SyntaxError: missing ) 在参数列表之后
- html - 如何在顶部的导航栏中放置固定徽标?
- google-chrome-extension - 在 iframe 中使用 tabs.executeScript
- java - Akka:-为什么不能保证消息到达(发送后)?消息的失败率是多少?
- javascript - 显示解析的 URL 值