gradle - Gradle 不执行任务
问题描述
所以,这是我的毕业任务。我想将一些 sql 文件从 src/main/sql 复制到 build/sql,同时根据 .properties 文件过滤一些数据。
import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
def db_names = ['oracle', 'postgresql', 'h2']
db_names.each{db ->
copy {
from('src/main/sql')
include '*sql'
def properties = new Properties()
file('src/main/sql/' + db + '_token.properties').withInputStream{
properties.load(it);
}
filter(ReplaceTokens, tokens: properties)
rename 'vorlage', db
into 'build/sql'
}
}
}
基本上这个任务在我运行时工作正常gradle buildSQL
。当我运行gradle clean buildSQL
任务时不会执行。
我该怎么做才能运行任务“buildSQL”?
解决方案
该任务可能永远不会执行,因为它没有任何输入。唯一运行的是copy
构建配置时的方法。
- 当您只运行
buildSQL
任务时,该copy
方法会在配置阶段运行,然后构建完成,因为没有输入任务。 - 当您运行时
clean buildSQL
,该copy
方法会在配置阶段运行,并且构建clean
会在执行阶段执行任务——这会删除刚刚创建的副本。
如果我的解释没有立即意义,我建议阅读Gradle 的构建阶段。也许跑步gradle --console=verbose clean buildSQL
也会对正在发生的事情有更多的了解。
抛开理论不谈,以下是修复配置的方法:
import org.apache.tools.ant.filters.ReplaceTokens
task buildSQL(type: Copy) {
def db_names = ['oracle', 'postgresql', 'h2']
db_names.each { db ->
def properties = new Properties()
file("src/main/sql/${db}_token.properties").withInputStream {
properties.load(it)
}
from('src/main/sql') {
include '*sql'
filter(ReplaceTokens, tokens: properties)
rename 'vorlage', db
}
}
into 'build/sql'
}
换句话说,只需删除该copy
方法,但保留它CopySpec
的Copy
任务。
推荐阅读
- sorting - 表格排序时Vue v-for循环中的重复键
- asp.net-core-webapi - .NET Core 2.2 与 Identity Server 4 SameSite Cookie 更改问题
- c# - ToString 覆盖以返回数组
- python - 在 selenium 中为 firefox 启用 webdriver 日志记录
- python - 将 multiprocessing.RawArray 传递给 C++ 函数
- c - 结构内的枚举,这些枚举在这段代码中完成了什么?
- javascript - 使用 Google 跟踪代码管理器跟踪英雄横幅中的轮播事件
- javascript - 在反应的同一组件中单击并鼠标向上?
- ios - 到达边缘时如何使 SwiftUI 文本字段垂直延伸?
- sql - 添加 number + varchar 以获得字符串连接