【发布时间】:2018-11-15 03:56:10
【问题描述】:
我准备了一个非常简单的脚本,它说明了我在使用 Gradle 1.7 时遇到的问题(需要坚持使用它,因为一些插件还不支持新版本)。
我正在尝试动态创建任务,每个任务对应于项目目录中的一个文件。这很好用,但我创建的任务在我分配它们类型“复制”后就永远不会执行。
这是我的问题build.gradle:
file('templates').listFiles().each { File f ->
// THIS LINE DOES NOT WORK
task "myDist-${f.name}" (type: Copy) {
// NEXT LINE WORKS
//task "myDist-${f.name}" {
doLast {
println "MYDIST-" + f.name
}
}
}
task distAll(dependsOn: tasks.matching { Task task -> task.name.startsWith("myDist")}) {
println "MYDISTALL"
}
defaultTasks 'distAll'
这样,当我简单地调用默认任务调用gradle时,我的任务不会被执行:
MYDISTALL
:myDist-template1 UP-TO-DATE
:myDist-template2 UP-TO-DATE
:distAll UP-TO-DATE
BUILD SUCCESSFUL
如果我从我的动态任务中删除类型 Copy(取消注释上面的行),我的任务就会被执行:
MYDISTALL
:myDist-template1
MYDIST-template1
:myDist-template2
MYDIST-template2
:distAll
BUILD SUCCESSFUL
(您需要在build.gradle所在的同一目录中创建一个文件夹名称templates,并在其中放入几个空文件才能运行测试)
根据调试输出:
跳过任务 ':myDist-template1',因为它没有源文件。
跳过任务 ':myDist-template2',因为它没有源文件。
那么我怎样才能指定源文件并让我的Copy 任务执行呢?
我试过添加
from( '/absolute/path/to/existing/file' ) {
into 'myfolder'
}
对于任务主体,我尝试分配任务的inputs.source file('/my/existing/file'),但没有成功。
您能否就如何修改我的简单脚本留下动态任务创建并保持Copy 类型的动态任务提出建议?
谢谢!
编辑: 好的,这样任务就被调用了:
file('templates').listFiles().each { File f ->
task "myDist-${f.name}" (type: Copy) {
from f
into 'dist'
doLast {
println "MYDIST-" + f.name
}
}
}
但看起来我必须始终指定from/into。在 doLast{} 正文中这样做是不够的。
【问题讨论】:
标签: gradle build.gradle