How to easily zip files without using Zip task?

Hi,

because of some Zip & Copy tasks limitations, I need to zip files without using the Zip task.

I was wondering if it is somehow possible natively in Gradle (using FileTrees) in the same was as there is Copy task together with Project.copy() method.

Thanks

What limitations are you finding?

https://discuss.gradle.org/t/why-is-the-closure-in-from-method-of-copy-task-evaluated-in-config-phase?source_topic_id=23485

The easiest thing to do is to fall back to the ant.zip task: https://docs.gradle.org/current/userguide/ant.html#sec:using_ant_tasks

See the javadoc here which states that the files(...) methods can accept a Closure if you need to delay the evaluation. I’m guessing you can use this in a Zip or Copy task and it will only be evaluated just in time

Eg:

   FileCollection lazyFiles = files({
      FileTree allTree = files().asFileTree
      file('build/source.txt').readLines().each {
         allTree.add(fileTree(it))
      }
      return allTree
   })
   task myCopy(type: Copy, dependsOn: generateSources) {
      from lazyFiles
      into 'foo' 
   } 

Hi Lance, late response here. FYI, in my case I am defining the zip task during applying of my plugin. And I get to know the basename of the zip later, during execution of the buildscript. setBaseName only accepts a plain string, so I cannot set it in a closure. The workaround is to call setBaseName on the task in a doFirst block. Another option is to do everything in a doLast and use project.ant.zip.