Setup Zip task in a dynamic way

(Florian Oeser) #1

Hey guys,

I would like to setup the input (files to compress) and the archive name for the Zip task in a dynamic way (in the execution phase). As far as I’ve experienced and what I’ve read here this is not possible. The Zip task must be setup during configuration. Why? Whats the concept about it? :slightly_smiling:

(if it’s just because of the up to date check then a switch to disable this behavior would be nice.)

(Lance Java) #2

The file tasks are convenience wrappers around project methods. For instance the Copy task invokes Project.copy(…). You can invoke methods directly on the Project instance


  • Project.copy(…)
  • Project.zipTree(…)
  • Project.copySpec(…)
  • Project.delete(…)
  • etc, etc

If you’re invoking these manually I suggest you configure the task inputs/outputs accordingly

(Florian Oeser) #3

Thanks for your reply!

Well I did not find a project method which compresses (zip’s) a file(s). Project.zipTree() is not what I’m looking for…

(Florian Oeser) #4

Any help would be much appreciated :wink:

(Lance Java) #5

Yes, you’re right. Unlike unzipping, there’s no convenient way to zip other than using the zip task.

You could use either a doFirst {…} block or another task (and dependsOn) to dynamically initialize a folder to zip. Then the folder location can be static but the contents dynamic.

Perhaps a bit more info on what you are doing and the timings would be helpful to come to a solution to achieve the dynamic behaviour you require but still setting archiveName / source at configuration time.

I’m assuming you realise you can do something like:

["one", "two", "three"].each {
   task "zip$it"(type: Zip) {
      from "src/$it" 
      archiveName "${it}.zip"
   assemble.dependsOn "zip$it" 


def sources = ["one", "two", "three"]
task myZip(type: Zip) {
   sources.each {
      from "src/$it" 
   archiveName "${sources.join()}.zip"

(Florian Oeser) #6

Thanks for your reply!

I don’t have a static folder with dynamic content. I have a .txt file (written at runtime) containing the paths of files to compress. This file is read as a collection of files which then serves as input (from property) for the Zip task as an FileCollection.

So what I try to achieve is doing a Zip out of files which are defined at runtime. The Zip archiveName is read from a file which itself is created during the build (execution time) too.

task doRelease() {
	doFirst {
	     // 'file' is written through build process
	     project.ext["version"] = (file =~ regex)[0][1]				
        // zipSources is from type: Zip
	finalizedBy zipSources

In ‘zipSources’ if want to set this version as the archiveName which is not possible because at configuration time the version property is not available. Putting the setup of the Zip properties (like archiveName) in a doFirst{} etc. in the ‘zipSources’ task result always in an up-to-date for this task.

The same problem with the archiveName arises with the dynamic setup of the FileCollection I described at the beginning.

To summarize it, why it’s not possible to configure the Zip task during execution phase in a straight manner?

Thanks in advice,

(Lance Java) #7

You could always use ant’s zip task. Example here

(V. Bors) #8

I used something like this and it work:

task myZip(type: Zip, dependsOn: ['generateVersion']) {
 archiveName = 'myZip_' + proejct.version + '.zip'
  doFirst {
    //update archiveName, because the version was increased in runtime by release task
    archiveName = 'myZip_' + getGeneratedVersion() + '.zip'

where getGeneratedVersion is a custom method that return a generated version by another previous task generateVersion