Gradle Zip任务:添加动态生成的文件
我有一个gradle构建脚本,其中我使用“Zip”任务直接从源目录生成Zip存档 除了将源目录结构中的所有文件复制到zip存档中之外,我还在寻找一种方法来包含一个不在源目录中的动态生成的文件 你们知道我怎么做吗 下面是我想做的伪代码:Gradle Zip任务:添加动态生成的文件,gradle,build.gradle,Gradle,Build.gradle,我有一个gradle构建脚本,其中我使用“Zip”任务直接从源目录生成Zip存档 除了将源目录结构中的所有文件复制到zip存档中之外,我还在寻找一种方法来包含一个不在源目录中的动态生成的文件 你们知道我怎么做吗 下面是我想做的伪代码: task('archive', type: Zip) { ... from 'source' newFile('dynamically-generated.txt', 'dynamic file content') } 以下是源和目标结构: [i71
task('archive', type: Zip) {
...
from 'source'
newFile('dynamically-generated.txt', 'dynamic file content')
}
以下是源和目标结构:
[i71178@bddev01-shell01 ~]$ tree source/
source/
├── file1.txt
├── file2.txt
└── file3.txt
0 directories, 3 files
[i71178@bddev01-shell01 ~]$ unzip -l destination.zip
Archive: destination.zip
Length Date Time Name
--------- ---------- ----- ----
0 02-26-2016 16:56 source/
0 02-26-2016 16:56 source/file2.txt
0 02-26-2016 16:56 source/dynamically-generated.txt
0 02-26-2016 16:56 source/file1.txt
0 02-26-2016 16:56 source/file3.txt
--------- -------
0 5 files
结合我上面的评论和您的getTemporaryDir评论:
task generateThenZip()<<{
def tempDir = getTemporaryDir()
newFile("$tempDir/dynamically-generated.txt", 'dynamic file content')
zip{
from 'source'
from tempDir
}
}
task generateThenZip()这就是我最后要做的:
subprojects {
apply plugin: 'myPlugin'
//The 'build' task is set up by the plugin
build {
//Customization to keep consistent with artifacts being currently generated.
doFirst {
new File(getTemporaryDir(), 'artifact-fullname.txt').text = "${project.name}-${project.version}\n"
}
archiveName = "${project.name}.${project.build.extension}"
from getTemporaryDir()
}
}
谢谢 我在Gradle 6.6.1上非常轻松地实现了这一目标
只需在from
部分中创建文件
distributions {
main {
contents {
into("/") {
from {
if (!buildDir.exists()) {
buildDir.mkdir()
}
def f = new File("$buildDir/all")
f.text = "project_version: ${project.version}\n"
f
}
}
...
}
}
}
顺便说一句,我认为$buildDir
比/tmp
更安全,包括一个不在源目录中的动态生成的文件,那么它在哪里?内容将动态生成(通过代码)在构建/复制时,我希望在目标zip存档中创建一个新文件,并提供名称和内容。它与“源”的有什么不同?只需从
行添加另一个带有新目录的。因此,源文件中不存在此文件。当我将文件从“源”复制到“目标”时,我还想在“目标”中插入一个新文件,其中包含一些动态生成的内容。我将更新问题以澄清这一点。在其他任务中创建文件。您可以使用dependsOn
强制该任务在zip任务之前运行这看起来很有趣:我可以在那里创建它,只需从“mytempfile”执行一个命令。。。