# Right way to copy contents from dependency archives?

**URL:** https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449
**Category:** Old Forum Archive
**Created:** [April 12, 2012, 1:37pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449 "2012-04-12T13:37:00Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![netmikey](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/netmikey/32/158_2.png) [@netmikey](https://discuss.gradle.org/u/netmikey)
#### Post date: [April 12, 2012, 1:37pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/1 "2012-04-12T13:37:00Z")

</div>

Hi,

I have a configuration that contains some dependencies. I want a task that extracts all resolved artifacts into a directory, but I’m getting stuck with Gradle’s Lifecycle I think…

Here’s what I want:

```gradle
configurations {
 api
}
  dependencies {
 api ( group: 'somegroup', name: 'someArtifact', version: 'someVersion' )
}
  task extractApi(type: Copy) {
 configurations.api.asFileTree.each {
  from( zipTree(it) )
 }
 into file("${project.buildDir}/api/")
}

```

But I’m getting: \> \* What went wrong: \> A problem occurred evaluating project ‘:myproject’. \> \> You can’t change a configuration which is not in unresolved state!

… which makes sense: I’m trying to configure a copy task (gradle configuration phase) with configuration dependencies, which would have to be resolved first, but they may not even be readily configured, so they can’t…

The following doesn’t work as well:

```gradle
task extractApi(type: Copy) {
 into file("${project.buildDir}/api/")
 doFirst {
  configurations.api.asFileTree.each {
   from( zipTree(it) )
  }
 }
}

```

… the problem here is, that gradle tells me the task is up-to-date, which, again, seems plausible since no input has been configured during its configuration phase.

The following SEEMS to work, but seems _very_ wrong :S

```gradle
task extractApi(type: Copy) {
 dependsOn configurations.api
 from {
  configurations.api.asFileTree.each {
   from( zipTree(it) )
  }
  // Don't include the actual archives themselves
  null
 }
 into file("${project.buildDir}/api/")
}

```

Any advice? 😉

Thanks in advance!, Mike

---

<div class="post-metadata">

### Author: ![netmikey](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/netmikey/32/158_2.png) [@netmikey](https://discuss.gradle.org/u/netmikey)
#### Post date: [April 13, 2012, 8:41am UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/2 "2012-04-13T08:41:00Z")

</div>

Thanks, Peter, that works perfectly! 🙂

I’m curious: I don’t see an obvious implementation of “from” taking a single closure as argument in [http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html](http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html) How comes this works? What am I missing?

Best regards, Mike

---

<div class="post-metadata">

### Author: ![netmikey](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/netmikey/32/158_2.png) [@netmikey](https://discuss.gradle.org/u/netmikey)
#### Post date: [April 13, 2012, 1:17pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/3 "2012-04-13T13:17:00Z")

</div>

Just for the record: The specified solution needs a

```gradle
dependsOn configurations.api

```

Because if it’s not present and if the configuration depends on another project in a multi-project build, that project doesn’t get build - resulting in missing dependency artifacts in the copy operation.

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [April 13, 2012, 8:45pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/4 "2012-04-13T20:45:00Z")

</div>

Correct. There are some cases (like this one) where Gradle cannot infer the task dependencies automatically.

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [April 13, 2012, 8:47pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/5 "2012-04-13T20:47:00Z")

</div>

It’s this one: [http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:from(java.lang.Object...)](http://gradle.org/docs/current/dsl/org.gradle.api.tasks.Copy.html#org.gradle.api.tasks.Copy:from(java.lang.Object...))

Note the sentence: “The given paths are evaluated as for Project.files()”. This is a common pattern in Gradle.

---

<div class="post-metadata">

### Author: ![Uyen\_Vu](https://avatars.discourse-cdn.com/v4/letter/u/7bcc69/32.png) [@Uyen\_Vu](https://discuss.gradle.org/u/Uyen_Vu)
#### Post date: [February 13, 2013, 7:20pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/6 "2013-02-13T19:20:00Z")

</div>

Peter,

Your code works for me but it only extracts one file at a time. If I have 7 native jars from dependecy archives that I would like to extract to their own directories. Do you know how to that in a loop?

dependencies {

runtime ‘net.java.dev.jogl:jogl-linux-amd64:1.1.1’,

‘net.java.dev.jogl:jogl-linux-i586:1.1.1’,

‘net.java.dev.jogl:jogl-macosx-ppc:1.1.1’,

‘net.java.dev.jogl:jogl-macosx-universal:1.1.1’,

‘net.java.dev.jogl:jogl-solaris-amd64:1.1.1’,

‘net.java.dev.jogl:jogl-solaris-i586:1.1.1’,

‘net.java.dev.jogl:jogl-solaris-sparc:1.1.1’,

‘net.java.dev.jogl:jogl-solaris-sparcv9:1.1.1’,

‘net.java.dev.jogl:jogl-windows-amd64:1.1.1’,

‘net.java.dev.jogl:jogl-windows-i586:1.1.1’

}

Thanks for your help in advance.

---

<div class="post-metadata">

### Author: ![cmcginty](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/cmcginty/32/2209_2.png) [@cmcginty](https://discuss.gradle.org/u/cmcginty)
#### Post date: [December 9, 2013, 10:58pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/7 "2013-12-09T22:58:00Z")

</div>

A side affect of this code, is that Gradle will resolve and download dependencies even if the task is not in the taskgraph. Is there a way to move the dependency resolution to task execution step?

---

<div class="post-metadata">

### Author: ![cmcginty](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/cmcginty/32/2209_2.png) [@cmcginty](https://discuss.gradle.org/u/cmcginty)
#### Post date: [December 12, 2013, 11:34pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/8 "2013-12-12T23:34:00Z")

</div>

I don’t know if this used to work, but on 1.9 the dependency is resolved even when the task is not executed. Can you update this snippet?

---

<div class="post-metadata">

### Author: ![cmcginty](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/cmcginty/32/2209_2.png) [@cmcginty](https://discuss.gradle.org/u/cmcginty)
#### Post date: [April 3, 2014, 11:24pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/9 "2014-04-03T23:24:00Z")

</div>

Still a bug? Can you verify this actually does what your comment says it will?

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [April 4, 2014, 4:22am UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/10 "2014-04-04T04:22:00Z")

</div>

Works fine for me, both with 1.9 and 1.11. The configuration only gets resolved if Gradle decides that ‘extractApi’ will get executed. In that case, ‘from’ (and therefore also resolution of the configuration) gets triggered when the task execution graph is built, i.e. between configuration and execution phase.

---

<div class="post-metadata">

### Author: ![cmcginty](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/cmcginty/32/2209_2.png) [@cmcginty](https://discuss.gradle.org/u/cmcginty)
#### Post date: [April 4, 2014, 8:32pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/11 "2014-04-04T20:32:00Z")

</div>

Yes, i see it does work, just not with ‘gradle tasks’. Thank You.

---

<div class="post-metadata">

### Author: ![Martin4](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@Martin4](https://discuss.gradle.org/u/Martin4)
#### Post date: [September 15, 2014, 2:10pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/12 "2014-09-15T14:10:00Z")

</div>

Hi, where should this dependsOn be located? Who should depend on the configuration? Can you provide me with some more context please. THanks. martin

---

<div class="post-metadata">

### Author: ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)
#### Post date: [April 12, 2012, 5:06pm UTC](https://discuss.gradle.org/t/right-way-to-copy-contents-from-dependency-archives/7449/13 "2012-04-12T17:06:00Z")

</div>

An idiomatic solution would look like this:

```gradle
configurations {
    api
}
  dependencies {
    api 'somegroup:someArtifact:someVersion'
}
  task extractApi(type: Sync) {
    dependsOn configurations.api

    from { // use of closure defers evaluation until execution time
        configurations.api.collect { zipTree(it) }
    }
    into "$buildDir/api/"
}

```
