How to Rename file in Tar task when moving it into a tar ball?

I have the following task which creates a tar.gz file, I am unable to figure out how to rename a file when I’m creating a tar ball, I tried multiple options including renameTo but I must be doing something wrong.

Any thoughts on how i could rename the jar file when its being copied into tar.gz ?

task distTargz( type: Tar ) {

compression = Compression.GZIP

baseName =

baseFolder = “projectC”

extension = ‘tar.gz’

into("${baseFolder}/lib") {

from { configurations.runtime }


into( baseFolder ) {

from { “${buildDir}/VERSION” }

from { “${rootDir}/hiverc” }


into("${baseFolder}/build") {

from("${buildDir}/distributions") {

include “*.jar”


// Want to rename the Jar file here, When its copied into ${baseFolder}/build

} }

Your “from” closure is a CopySpec, so the CopySpec “rename” method should work. For example:

from("${buildDir}/distributions") {
          include "*.jar"
          // strips off anything after the last "-"
          rename '(.*)-.*.jar','$1.jar'

Thanks! That works. I was trying renameTo option, did not try rename!