Failing to unpack dependencies in a Sync task

Hi!

I am trying to find a nice way of unpacking my dependencies from zip files to a folder in my project directory. I want to use a Sync task or similar to do it, because I would rather that Gradle handles it.

I am on Gradle 1.5 using the gradle wrapper.

This is what I hope the relevant portions of my build.gradle file:

apply plugin: 'base'
apply plugin: 'maven'
apply plugin: 'artifactory'
  configurations {
 compile
}
  dependencies {
 compile group: 'com.anoto', name: 'PatternLibrary-C', version: '0.1.0.0', ext: 'zip'
 compile group: 'com.anoto', name: 'PPSSWLibrary-C', version: '0.1.0.1', ext: 'zip'
}
  [snip]
  // Unpack dependencies
task unpackDependencies << {
 delete new File( "deps" )
 configurations.compile.resolvedConfiguration.resolvedArtifacts.each{
   artifact ->
      project.copy {
          into new File( "deps/" + artifact.name )
          from( zipTree(artifact.file) )
      }
   }
 }
  // Unpack dependencies2
task unpackDependencies2(type: Sync, dependsOn: configurations.compile) {
 configurations.compile.resolvedConfiguration.resolvedArtifacts.each{
  artifact ->
      from zipTree(artifact.file)
     into "deps/" + artifact.name
  }
 }

The task unpackDependencies works just fine. unpackDependencies2 gives me the error message

C:\gitlab\penfirmware-zeus>gradlew unpackDependencies2
-i
Starting Build
Settings evaluated using empty settings script.
Projects loaded. Root project using build file 'C:\gitlab\penfirmware-zeus\build.gradle'.
Included projects: [root project 'penfirmware-zeus']
Evaluating root project 'penfirmware-zeus' using build file 'C:\gitlab\penfirmware-zeus\build.gradle'.
Compiling build file 'C:\gitlab\penfirmware-zeus\build.gradle' using BuildScriptClasspathScriptTransformer.
Compiling build file 'C:\gitlab\penfirmware-zeus\build.gradle' using BuildScriptTransformer.
  FAILURE: Build failed with an exception.
  * Where:
Build file 'C:\gitlab\penfirmware-zeus\build.gradle' line: 101
  * What went wrong:
A problem occurred evaluating root project 'penfirmware-zeus'.
> Could not resolve all dependencies for configuration ':compile'.
   > Could not find com.anoto:PatternLibrary-C:0.1.0.0.
     Required by:
         com.anoto:penfirmware-zeus:unspecified
   > Could not find com.anoto:PPSSWLibrary-C:0.1.0.1.
     Required by:
         com.anoto:penfirmware-zeus:unspecified

What am I doing wrong in the second version?

Kind regards,

Torkel

not completely sure yet, but I can’t see a repository definition in your snippet. Do you just cut it out when pasting it here?

Yes, I did cut it out of there.

Here is a more complete one

apply plugin: 'base'
apply plugin: 'maven'
apply plugin: 'artifactory'
  configurations {
 compile
}
  dependencies {
 compile group: 'com.anoto', name: 'PatternLibrary-C', version: '0.1.0.0', ext: 'zip'
 compile group: 'com.anoto', name: 'PPSSWLibrary-C', version: '0.1.0.1', ext: 'zip'
}
  project.ext.fwversionswitch = "" // this is set automatically
buildDir = 'output'
  gradle.taskGraph.whenReady {graph ->
 if (System.env.JENKINS_GENERATED_VERSION != null) {
  version = System.env.JENKINS_GENERATED_VERSION
  project.ext.fwversionswitch = "--fwversion=${->version}"
 }
}
  task wrapper(type: Wrapper) {
    gradleVersion '1.5'
    jarFile 'build/gradle/wrapper/gradle-wrapper.jar'
}
  // Artifactory connection
buildscript {
    repositories {
        maven {
            url 'http://[censored]:8081/artifactory/plugins-release'
        }
    }
    dependencies {
        classpath(group: 'org.jfrog.buildinfo', name: 'build-info-extractor-gradle', version: '2.0.9')
    }
}
  artifactory {
    contextUrl = "${artifactory_contextUrl}"
 //The base Artifactory URL if not overridden by the publisher/resolver
    resolve {
        repository {
            repoKey = 'repo'
            maven = true
        }
    }
}
  // Unpack dependencies
task unpackDependencies << {
 delete new File( "deps" )
 configurations.compile.resolvedConfiguration.resolvedArtifacts.each{
   artifact ->
      project.copy {
          into new File( "deps/" + artifact.name )
          from( zipTree(artifact.file) )
      }
   }
 }
  // Unpack dependencies
task unpackDependencies2(type: Sync, dependsOn: configurations.compile) {
 //delete new File( "deps" )
 configurations.compile.resolvedConfiguration.resolvedArtifacts.each{
  artifact ->
      from zipTree(artifact.file)
     into "deps2"
  }
 }

‘unpackDependencies2’ resolves the ‘compile’ configuration in the configuration phase, rather than the execution phase. This will cause several problems. If you can live with a shared ‘into’, ‘from { configurations.compile.collect { zipTree(it) }}’ is an elegant solution. If you need separates 'into’s, I can’t think of an easy solution, and you may have to defer configuration with ‘gradle.taskGraph.whenReady { graph -> if (graph.hasTask(unpackDependencies2)) { … } }’. (Deferring configuration with ‘unpackDependencies2.doFirst { … }’ won’t work for a ‘Sync’ task; it will always be UP-TO-DATE.)

PS: ‘configurations.compile.resolvedConfiguration.resolvedArtifacts.each’ can be shortened to ‘configurations.compile.each’.

Thanks Peter!

I will probably keep my old approach of synching by deleting the folder for now. I am only dealing with a few files in each dependency and they are small.

It would be really nice to have a more gradle-y version of it that could unpack dependencies in a separate folder, sort of like the unpackDependencies2 in my example. I can however live without it for now.

Torkel