使用传递的参数在gradle中重命名文件

时间:2016-07-06 11:16:51

标签: android gradle android-gradle build.gradle

我正在从gradle构建中打包我的构建文物。这是一个Android Studio项目。

我有成功创建包含两个jar的zip文件的任务。假设zip文件名为“my.zip”。

我有以下gradle任务:

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

我用gradle -PversionString="123" :sdk:renameArtifacts

来称呼它

我在println闭包中有一个doLast,可以看到我的字符串插值在输出my-123.zip时正常工作。

但是,'my.zip'未重命名为'my-123.zip'。它仍为'my.zip',实际上会生成一个大小为零字节的文件,并且不再可以作为zip文件打开。

我显然在某处出错,但在哪里?

完整的gradle文件:

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 个答案:

答案 0 :(得分:1)

renameCopyProcessingSpec上的一种方法,用于将任务配置为在操作时执行某些重命名。如果将其包装在doLast中,则复制已经发生,并且不会执行重命名。此外,重命名仅包含文件名,而不是相对或绝对文件路径。这应该有效:

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

编辑:$ versionString在任务中无法访问。使用额外的项目属性是如何在任务中传递这些属性的建议方法(参见here)。