Unzipping and copying JAR dependency content in desired folder

I would like to unzip and move the content of a dependency when it is resolved to a specified folder. I have updated my gradle.build as follows:

configurations {

dependencies {
    nativeBundle 'sigar:sigar:1.7.0-proactive:native'

task extractApi(type: Copy) {
    print "FILE=" + configurations.nativeBundle.singleFile
    from zipTree(configurations.nativeBundle.singleFile)
    into 'nativeLibs'

Unfortunately, when I execute gradle clean compileJava no nativeLibs folder is created and it seems that nothing is unzipped. However, the path that is displayed for “FILE=” exists:


I am new with gradle and groovy. I cannot figure out what is wrong. Any help or suggestion is welcome.

The statement is printed because that occurs at configuration time, as in the task is configured for execution and sets up its inputs and outputs. You need to add a task dependency, e.g. compileJava.dependsOn(extractApi) so that your task is executed as part of the build step.

Thank you a lot Benjamin. It is now working but I still have some questions:

  1. Is it possible to execute my task for any build step with an exception for the clean one?
  2. Should I declare a new files dependency to put all extracted files into the classpath or is there a mean to configure it from the task definition?
  3. How can I configure the clean task to remove my generated folder?
  1. Just set up the desired task dependency. The new task will only make sense in certain conditions, which only you know.
  2. You have to add it to the classpath of tasks where appropriate. For example, I made a similar task to run jsr107’s tests by extracting the test jar and setting the test task’s testClassesDir.
  3. I extract within the project’s buildDir, which a clean annihilates.

Thank you again for these information. One last question, is it possible to force gradle to execute my extractApi task when dependencies are resolved (if possible, dependencies defined with nativeBundle configuration)?

Not that I know of. I think your best bet is to understand what the task graph looks like for your build and ensure that the extractApi task relationship is setup. You could probably use cross-cutting logic like filtering and applying the relationship to allprojects.tasks but that’s overkill.