Gradle build - Resolve dependencies from downloaded archive

710 views Asked by At

I'm fairly new to Gradle. I have a multi-project build that uses some dependencies currently packaged within the project (using repositories and flatDir), as they're not available in an artifactory. I want to remove this local folder and download a couple of archives holding these dependencies, unpack them and proceed with the build as regular. I will use https://plugins.gradle.org/plugin/de.undercouch.download for downloading, but I don't know how to this before any dependency resolution (and ideally, download if not already done). Currently, the build fails in the configuration phase as far as I can tell:

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

EDIT: Downloading the files works. Still struggling with unzipping the archives:

task unzipBirt(dependsOn: downloadPackages, type: Copy) {
    println 'Unpacking archiveA.zip'
    from zipTree("${projectDir}/lib/archiveA.zip")     
    include "ReportEngine/lib"
    into "${projectDir}/new_libs"
}

How do I make this run in the configuration phase ?

2

There are 2 answers

4
lance-java On

See Project.files(Object...) which states

You can pass any of the following types to this method:

...

A Task. Converted to the task's output files. The task is executed if the file collection is used as an input to another task.

So you can do:

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) 
}

Note: if there's lots of jars in the zip you can do

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

I ended up using copy to force the unzip in the configuration phase

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