更改复制任务目标

时间:2014-04-04 11:18:06

标签: gradle

我想根据所选的版本更改文件复制的目标目录。 这不起作用,因为任务图在执行阶段执行,但copyTask在配置阶段设置。

我怎样才能做到这一点?

gradle.taskGraph.whenReady { taskGraph ->
    println('taskGraph')
    if (taskGraph.hasTask(buildRelease)){
        File toDir=file('test/r')
        println('Copy to: ' + toDir.getName())
    }else if (taskGraph.hasTask(buildDevel)) {
        File toDir=file('test/d')
        println('Copy to: ' + toDir.getName())
    }

}

task buildDevel (dependsOn: ['copyTask']){}
task buildRelease (dependsOn: ['copyTask']){}

task copyTask(type: Copy) {
        from "test"
        into toDir
        include 'a.txt'
}

1 个答案:

答案 0 :(得分:0)

这可能会对您有所帮助(您需要在与a.txt所在级别相同的级别创建build.gradle文件:

gradle.taskGraph.whenReady { taskGraph ->

   def cp = project.copyTask
   if (taskGraph.hasTask(buildRelease)){        
      cp.into 'lol1'
   } else if (taskGraph.hasTask(buildDevel)) {
      cp.into 'lol2'
   }
}

task buildDevel (dependsOn: ['copyTask']){}
task buildRelease (dependsOn: ['copyTask']){}

task copyTask(type: Copy) {
    from file('.')
    include 'a.txt'
}

这也有效:

task buildDevel 
task buildRelease 

buildDevel.doFirst {
   cp('lol1')
}

buildRelease.doFirst {
   cp('lol2')
}

def cp(to) {
   copy {
    from file('.')
    into to
    include 'a.txt'
   }
}