Rename file in gradle with passed parameter

2019-09-11 12:46发布

问题:

I'm packaging up my build artefacts from a gradle build. It's an Android Studio project.

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

I have the following gradle task:

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")
    }
}

And I'm calling it with gradle -PversionString="123" :sdk:renameArtifacts

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

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

I'm obviously going wrong somewhere, but where?

Full gradle file:

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() << {
    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)

回答1:

rename is a method on CopyProcessingSpec, that configures the task to perform some renamings while operating. If you wrap it in doLast, the copying has already happened, and no rename will be performed. Furthermore, rename takes only file names, not relative or absolute file paths. This should work:

project.ext.versionString = versionString
task renameArtifacts (type: Copy) {
    from ('build/')
    include 'my.zip'
    destinationDir file('build/')
    rename 'my.zip', "my-${project.versionString}.zip"
}

Edit: $versionString is not accessible in tasks. Using extra project property is the suggested way how to pass these in tasks (see here).