Reputation: 1584
I have a gradle build script in which I'm using the 'Zip' task to generate a zip archive directly from a source directory.
In addition to copying over all the files inside the source directory structure into the zip archive, I'm looking for a way to include a dynamically generated file that's not in the source directory.
Do you guys know how I can do this?
Here's pseudo code of what I want to do:
task('archive', type: Zip) {
...
from 'source'
newFile('dynamically-generated.txt', 'dynamic file content')
}
And here are the source and destination structures:
[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
Upvotes: 6
Views: 2249
Reputation: 11275
I have managed to achieve it quite easyly on Gradle 6.6.1
by just creating the file in the from
section.
distributions {
main {
contents {
into("/") {
from {
if (!buildDir.exists()) {
buildDir.mkdir()
}
def f = new File("$buildDir/all")
f.text = "project_version: ${project.version}\n"
f
}
}
...
}
}
}
btw. I think $buildDir
is safer than /tmp
Upvotes: 1
Reputation: 1584
This is what I ended up doing:
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()
}
}
Thanks!
Upvotes: 1
Reputation: 23697
Combining my comments above and your getTemporaryDir comment:
task generateThenZip()<<{
def tempDir = getTemporaryDir()
newFile("$tempDir/dynamically-generated.txt", 'dynamic file content')
zip{
from 'source'
from tempDir
}
}
Upvotes: 1