Unzip files in a specific folder

Hello,

I have a zip file containing a bunch of files at the root. I need to add these files in the output jar in a specific location (let’s say “/folder/inside/jar”). In order to use implicit dependencies between tasks, I tried this:

val unzipFilesTask = tasks.register<Copy>("unzipFiles") {
    from(zipTree(configurations["confForMyZipFile"].singleFile))
    into(layout.buildDirectory.dir("extractedFiles/folder/inside/jar"))
}

sourceSets {
    main {
        resources {
            srcDir(unzipFilesTask )
        }
    }
}

Obviously, in the output jar, the files are at the root since the sourceset uses the folder of the “into” method as an input. I found a way to achieve what I want by using explicit dependencies between tasks, but I would prefer to use implicit dependencies if possible:

val unzipFilesTask = tasks.register<Copy>("unzipFiles") {
    from(zipTree(configurations["confForMyZipFile"].singleFile))
    into(layout.buildDirectory.dir("extractedFiles/folder/inside/jar"))
}

sourceSets {
    main {
        resources {
            srcDir((layout.buildDirectory.dir("extractedFiles"))
        }
    }
}

tasks.named("processResources") {
    dependsOn("unzipFiles")
}
tasks.named("sourcesJar") {
    dependsOn("unzipFiles")
}

Is it possible?

You don’t really need to worry about the sourceSets block for resources that are created as part of your build. Just actually link up the tasks with the implicit build dependency. You need the root of the unzipFilesTask to be the extractedFiles folder, then unzip to the specific directory. Then include the task as part of the resources to be processed. Untested, but something like this should work:

val unzipFilesTask = tasks.register<Copy>("unzipFiles") {
    into(layout.buildDirectory.dir("extractedFiles"))
    from(zipTree(configurations["confForMyZipFile"].singleFile)) {
        into("folder/inside/jar")
    }
}

tasks.named("processResources") {
    from(unzipFilesTask)
}

Thanks for your help! It works fine by defining the root folder and the specific folder this way.
Compared to your solution, I just had to add the processResources type in the task configuration to make it work:

tasks.named<ProcessResources>("processResources") {
    from(unzipLicensesTask)
}
1 Like