我正在尝试根据task
编写适用于文件的gradle-watch-plugin
。 (虽然它现在没有。它应该在我开始threadWatch
任务时假设运行
但是我没有得到如何传递并在任务中传递文件名。
apply plugin: 'java'
apply plugin: 'application'
apply plugin: 'groovy'
apply plugin: 'com.bluepapa32.watch'
watch {
rules {
files fileTree(dir: '/src/main/resources', include: '*.xls')
tasks 'validateRules'
}
}
....
dependencies {
compile "org.codehaus.groovy:groovy-all:2.0.5"
compile ("org.drools:drools-core:${droolsVersion}")
compile ("org.drools:drools-compiler:${droolsVersion}")
compile ("org.drools:drools-decisiontables:${droolsVersion}")
}
import org.drools.builder.*
import org.drools.io.ResourceFactory
task validateRules(type: DefaultTask) {
ext.ruleValidator = {
xls_file_name ->
try {
def tConfig = KnowledgeBuilderFactory.newDecisionTableConfiguration().with {
tConfig.inputType = DecisionTableInputType.XLS
def builder = KnowledgeBuilderFactory.newKnowledgeBuilder().with {
builder.add(
ResourceFactory.newInputStreamResource(new FileInputStream(xls_file_name)),
ResourceType.DTABLE,
tConfig
)
if (builder.hasErrors()) {
println 'Invalid ${xls_file_name} rule table!'
}
}
table.add rFactory
}
} catch (exc) {
println exc.getMessage()
}
}
doLast {
ruleValidator()
}
}
task watchThread() << {
Thread.start {
project.tasks.watch.execute()
}
}
根据Gradle Watch Plugin,我需要使用所需的任务和文件列表定义watch
配置。
然而,它没有描述任务应该如何以及如何在那里传递参数 - &gt;所以我创建了基于DefaultTask
的任务,需要获取文件列表。
但是我不知道是不是因为我不明白watch
插件是如何通过那里的。完全没有。
答案 0 :(得分:1)
首先,由于Gradle 2.5存在连续构建模式(命令行上的-t),这意味着在大多数情况下你可以放弃使用watch插件。如果可能,您应该考虑将构建版本升级到至少2.5。
现在关于代码本身,有一些事情你可以做得更好。
[1]您似乎将任务配置与任务操作混合在一起。
task ValidateRules << {
// Given that you are assuming a single XLS file you can do
// (It will throw exception if there is more than one)
def xls_file_name = inputs.files.singleFile
// ... rest of your code inside the try block can follow here
// (I would not catch the exception, but rather let Gradle handle it)
}
[2]现在将自定义任务配置为根据输入的变化进行操作
ValidateRules {
inputs.files fileTree(dir: '/src/main/resources', include: '*.xls')
}