使用传递的参数重命名gradle中的文件 [英] Rename file in gradle with passed parameter
问题描述
我正在从gradle版本中打包我的版本文物.这是一个Android Studio项目.
I'm packaging up my build artefacts from a gradle build. It's an Android Studio project.
我有一些任务可以成功创建一个包含两个jar的zip文件.假设zip文件名为"my.zip
".
I have tasks that successfully create a zip file containing two jars. Let's say the zip file is called 'my.zip
'.
我有以下gradle任务:
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")
}
}
我用gradle -PversionString="123" :sdk:renameArtifacts
我在doLast
闭包中有一个println
,可以看到我的字符串插值在输出my-123.zip
时正常工作.
I have a println
in the doLast
closure and can see the my string interpolation is working correctly as it outputs my-123.zip
.
但是,'my.zip'
不会重命名为'my-123.zip'
.它仍然是'my.zip'
,实际上导致文件大小为零字节,并且不能再作为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?
完整的gradle文件:
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)
推荐答案
rename
是CopyProcessingSpec
上的一种方法,该方法将任务配置为在操作时执行一些重命名.如果将其包装在doLast
中,则复制已经发生,并且将不执行重命名.此外,重命名仅使用文件名,而不使用相对或绝对文件路径.这应该起作用:
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"
}
$ versionString在任务中不可访问.建议使用额外的项目属性来在任务中传递这些属性(请参见这里).
$versionString is not accessible in tasks. Using extra project property is the suggested way how to pass these in tasks (see here).
这篇关于使用传递的参数重命名gradle中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!