gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 03:50:01 26 4
gpt4 key购买 nike

我正在从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)

最佳答案

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)。

关于android - 使用传递的参数重命名gradle中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38222724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com