Gradle构建-解决下载档案中的依赖关系

时间:2018-11-05 18:05:11

标签: gradle download task multi-project external-dependencies

我是Gradle的新手。我有一个多项目构建,它使用项目中当前打包的某些依赖项(使用存储库和flatDir),因为它们在工件中不可用。 我想删除此本地文件夹,并下载几个包含这些依赖项的档案,解压缩它们,然后按常规进行构建。我将使用https://plugins.gradle.org/plugin/de.undercouch.download进行下载,但是在进行任何依赖项解析之前,我不知道如何进行下载(理想情况下,如果尚未完成,则下载)。目前,据我所知,构建在配置阶段失败:

C++11

编辑:下载文件有效。仍在努力解压缩档案:

  `A problem occurred configuring project ':sub-project-A'.
  > Could not resolve all files for configuration ':sub-project-A:compileCopy'.
    Could not find :<some-dependency>:.

如何在配置阶段运行它?

2 个答案:

答案 0 :(得分:0)

请参见Project.files(Object...),其中指出

  

您可以将以下任何一种类型传递给此方法:

     

...

     

任务。转换为任务的输出文件。如果将文件集合用作其他任务的输入,则执行该任务。

因此您可以这样做:

task download(type: Download) {
    ... 
    into "$buildDir/download" // I'm guessing the config here
}
task unzip {
    dependsOn download
    inputs.dir "$buildDir/download"
    outputs.dir "$buildDir/unzip"
    doLast {
        // use project.copy here instead of Copy task to delay the zipTree(...)
        copy {
            from zipTree("$buildDir/download/archive.zip")
            into "$buildDir/unzip"
        }
    }
}
task dependency1 {
    dependsOn unzip
    outputs.file "$buildDir/unzip/dependency1.jar" 
}
task dependency2 {
    dependsOn unzip
    outputs.file "$buildDir/unzip/dependency2.jar" 
}
dependencies {
    compile files(dependency1)
    testCompile files(dependency2) 
}

注意:如果拉链中有很多罐子,就可以

['dependency1', 'dependency2', ..., 'dependencyN'].each {
    tasks.create(it) {
        dependsOn unzip
        outputs.file "$buildDir/unzip/${it}.jar" 
    }
}

答案 1 :(得分:0)

我最终在配置阶段使用copy强制解压缩

copy {
     ..
     from zipTree(zipFile)
     into outputDir
     ..  
   }