Is it possible to call tar from within a task?

If I want to copy files within a task, I can do something like this

task prepareDirectory() {
  copy {
    from 'jobs'
    into "$buildDir/tmp/foo"
  }
  copy {
    from configurations.compile
    into "$buildDir/tmp/foo/lib"
  }
}

Now I want to turn that foo directory into a tarball. Of course, I can do this

task assembleTar(type: Tar) {
    from "$buildDir/tmp"
    compression 'gzip'
    extension 'tar.gz'
    exclude '**/.keepdir'
}

However it is in a different task. Can I put it into the same task?

yes, you can put contents from task prepareDirectory into assembleTar.

task assembleTar(type: Tar) {
     into 'foo' {
        from 'jobs' 
     }
     into 'foo/lib' {
       from configurations.compile
    }
}
1 Like