使用根路径到文件而不是zipTree的子项目路径

时间:2016-09-02 22:46:10

标签: gradle

我有这个项目结构

journey\
    applications\
    desktop\
    core\

我有一个下载.zip存档的任务。

task download_redis(type: Download) {
    src 'https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip'
    dest new File("applications/redis/install", 'Redis-x64-3.2.100.zip')
    onlyIfNewer true
}

zip个文件保存到applications\redis\install。 然后,我还有另一项任务,即将zip文件的内容提取到applications/redis

task install_redis(dependsOn: download_redis, type: Copy) {
    from zipTree(download_redis.dest) 
    into 'applications/redis'
}

但是我收到了这个错误

Cannot expand ZIP 'L:\journey\desktop\applications\redis\install\Redis-x64-3.2.100.zip' as it does not exist.

似乎尝试从zip项目解决desktop的路径,尽管它的级别更高 - root

如何设置zipTree使用root的路径而不是子项目的路径? (尽管这两项任务都在desktop子项目中)

1 个答案:

答案 0 :(得分:1)

您可以访问各种路径:

println project(":applications").projectDir
println project(":desktop").projectDir
// root
println project(":").projectDir

以下是使用欺骗性zip下载的示例:desktop:build.gradle文件。请注意applicationsDir变量。 (我假设您使用的是与指定插件相同的插件。):

plugins {
    id "de.undercouch.download" version "1.2"
}

apply plugin: 'de.undercouch.download'

import de.undercouch.gradle.tasks.download.Download

def applicationsDir = project(":applications").projectDir

task download_redis(type: Download) {
    src 'https://github.com/codetojoy/Git_Sandbox/raw/master/tmp/example.zip'
    dest new File("${applicationsDir}/redis/install", 'Redis-x64-3.2.100.zip')
    onlyIfNewer true
}

task install_redis(dependsOn: download_redis, type: Copy) {
    from zipTree(download_redis.dest) 
    into "${applicationsDir}/redis"
}