How can i call a task for unzip file when i am downloading dependencies with gradle

i wanna create a task to download dependencies and to execute my task for unzipping files at the same time , it means a task to download dependencies and to call other task.

Thank you

When I’ve needed to do things like this in the past, I was recommended to use dependency transforms:

https://docs.gradle.org/current/userguide/artifact_transforms.html

The basic idea was something like put an attribute on all of the dependencies in a configuration, create another configuration that inherits from the first using a different attribute, and then hook up a transform that mapped the first attribute to the second. In this way, you don’t force any dependency resolution or transforms until you actually need them.

It’s been over a year since I was playing with this, so my memory/description are probably a little rough, but hopefully this gets you pointed in the right direction.

2 Likes

If you want to sidestep the overhead of doing this properly as @bparkison mentioned, I’ve found this pattern useful as a poor man’s artifact transform:

configurations {
    myConf
}

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    myConf name: 'foo', ext: 'zip'
    myConf name: 'bar'
}

task unpack(type: Copy) {
    from files(configurations.myConf)
    into "$buildDir/unpacked"
    filesMatching '*.zip', { zipDetails ->
        copy {
            from zipTree(zipDetails.file)
            into destinationDir
        }
        zipDetails.exclude()
    }
}

Running ./gradlew unpack will unzip the foo.zip dependency into build/unpacked, and copy the bar.jar dependency as-is.

1 Like