sver
sver

Reputation: 942

Copy Unzipped files in a folder having subfolders that are named dynamically

I have a folder copyAggregatedLibs. In this folder, I have 2 libs > a-libs.zip,b-libs.zip. These libs have a structure >> a-libs.zip--jar and b-libs.zip--jar Now I want to unzip these files in another folder unzippedLiraries such that the structure is intact. It should look like : unzippedLibraries--a-libs--jar and b-libs--jar. Now the thing is , I am getting the names of the libraries dynamically inside an array called libNames. libNames ={a-lib.zip,b-lib.zip} Now, I write a task to unzip the files from copyAggregatedLibs to unzippedLibraries

task unzipLibrary(type:Copy){
libNames.each { fullComponentName ->
   println('fullname'+fullComponentName)
   def zipFile = []
    zipFile = file('build/tmp/libs/copyAggregatedLibraries/'+fullComponentName)
   from zipTree(zipFile)
   println('Zips'+zipFile)
   into 'build/tmp/unpacked/unzippedLibraries/'+fullComponentName
   }

   }

Now, zip prints the names of both the files but I dont get the folder structure. All I get is the name of the second library i.e b-lib an jar folder inside that. It seems into is not run each time unlike from. could anyone suggest me how to get this working.

I tried destinationDir as well . Its not working Also tried using two tasks, executing one inside other, that also doesnt work. Says no source files

Upvotes: 0

Views: 84

Answers (1)

Axel von Engel
Axel von Engel

Reputation: 339

Why it doesn't work: The Copy task's from method can be called multiple times, each time adding another source file/directory to copy. The into method however acts like a setter for the destination dir.

You should be able to achieve your goal like this:

task unzipLibrary(type:Copy){
    libNames.each { fullComponentName ->
       println('fullname'+fullComponentName)
       def zipFile = file('build/tmp/libs/copyAggregatedLibraries/'+fullComponentName)
       println('Zips'+zipFile)
       into project.buildDir
       // the following into calls will be relative to the one above
       from(zipTree(zipFile)) {
           into 'tmp/unpacked/unzippedLibraries/'+fullComponentName // notice the missing leading 'build/'
       }
   }
}

The from calls here use the supplied closure to configure a child CopySpec. That's also why the directory specified inside the closure is relative to the task's target directory.

Upvotes: 0

Related Questions