Rename file in gradle with passed parameter
<p>I'm packaging up my build artefacts from a gradle build. It's an Android Studio project.</p>

<p>I have tasks that successfully create a zip file containing two jars. Let's say the zip file is called '<code>my.zip</code>'.</p>

<p>I have the following gradle task:</p>

<pre><code>task renameArtifacts (type: Copy) {
    from ('build/')
    include 'my.zip'
    destinationDir file('build/')
    doLast {
        println "my-${versionString}.zip"
        rename('build/my.zip', "build/my-${versionString}.zip")
    }
}
</code></pre>

<p>And I'm calling it with <code>gradle -PversionString="123" :sdk:renameArtifacts</code></p>

<p>I have a <code>println</code> in the <code>doLast</code> closure and can see the my string interpolation is working correctly as it outputs <code>my-123.zip</code>.</p>

<p>However, <code>'my.zip'</code> is not renamed to <code>'my-123.zip'</code>. It remains <code>'my.zip'</code> and in fact results in a file with a size of zero bytes and is no longer openable as a zip file.</p>

<p>I'm obviously going wrong somewhere, but where?</p>

<p>Full gradle file:</p>

<pre><code>apply plugin: 'com.android.library'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.3"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 14
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:support-v4:22.2.0'
    compile files('libs/urbanairship-lib-3.1.0.jar')
    compile files('libs/jackson-annotations-2.2.2.jar')
    compile files('libs/jackson-core-2.2.2.jar')
    compile files('libs/jackson-databind-2.2.2.jar')
}

task updateVersionNumber() &lt;&lt; {
    ant.replace(file: 'src/main/java/com/my/Version.java', token: '{{VERSION}}', value: versionString)
}

task javadoc(type: Javadoc) {
    source = android.sourceSets.main.java.srcDirs
    classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
    destinationDir = file("build/javadoc")
    failOnError false
}

task createJavaDocJar(type: Jar) {
    from ('build/javadoc')
    destinationDir file('build')
    baseName = 'my_doc'
}
createJavaDocJar.dependsOn(javadoc)

task packageMyJarAndDependencies(type: Jar) {
    from (zipTree('libs/jackson-annotations-2.2.2.jar')) {
        exclude 'META-INF/maven/'
        exclude 'META-INF/services/'
    }
    from (zipTree('libs/jackson-core-2.2.2.jar')) {
        exclude 'META-INF/maven/'
        exclude 'META-INF/services/'
    }
    from (zipTree('libs/jackson-databind-2.2.2.jar')) {
        exclude 'META-INF/maven/'
        exclude 'META-INF/services/'
    }
    from (zipTree('libs/urbanairship-lib-3.1.0.jar'))
    from (zipTree('build/intermediates/bundles/release/classes.jar'))
}

task createFinalJar(type: Copy) {
    from('build/libs/')
    into('build')
    include('sdk.jar')
    rename('sdk.jar', 'my.jar')
}
createFinalJar.dependsOn(clean, build, packageMyJarAndDependencies)
packageMyJarAndDependencies.mustRunAfter build
build.mustRunAfter clean

task zipArtifacts(type: Zip) {
    from ('build/')
    from ('build/libs')
    include 'my_doc.jar'
    include 'my.jar'
    baseName = 'my_lib'
    destinationDir file('build')
}
zipArtifacts.dependsOn(createFinalJar, createJavaDocJar)

task renameArtifacts (type: Copy) {
    from ('build/')
    into('build')
    include 'my_lib.zip'
    doLast {
        println "my_lib-${versionString}.zip"
        rename "my_lib.zip", "my_lib-${versionString}.zip"
    }
}
renameArtifacts.dependsOn(zipArtifacts)
</code></pre>