how to put folder into a tar file with gradle - gradle

I am new to gradle.
I want to create a filename.tar file which includes (filename.war and data/config/*.properties). The war file stays inside build/libs. The requirement is: inside tar file, we need to have folder data/config and all properties files under that folder. The war file stays in the same level with data folder.
Here is what i have,
task tarz(type: Tar) {
archiveName = 'filename'
from 'build/libs','data/config'
include '*.war','*.properties'
destinationDir = file('build/tar')
extension 'tar'
compression = Compression.GZIP
}
this above script is only copy properties files into tar, not the folder.
And the name of the file is 'filename', not 'filename.tar'
Please help me.

here is what i have done
task tarz(type: Tar) {
archiveName = 'aaa.tar'
into ('/'){
from 'build/libs'
include '*.war'
}
into ('data/config'){
from 'data/config'
include '*.properties.*'
}
destinationDir file('build/tar')
extension 'tar'
compression = Compression.GZIP
}

This is my example for Spring Boot App
task tgzTask(type: Tar) {
into('app/') {
from 'build/libs'
include '*.jar'
}
into('app/') {
from 'build/resources/main/banner.txt'
}
into('app/config') {
from 'build/resources/main/config'
from 'build/resources/main/banner.txt'
}
into('app/config/sql') {
from 'build/resources/main/sql'
}
destinationDirectory = file('build/distributions')
extension 'tgz'
compression = Compression.GZIP
}
for more info Tar manual

Related

Gradle - Copy folder into jar

In my gradle.build I have the following code in order to copy the content of a folder into the jar during the build process
sourceSets {
main {
resources {
srcDirs = ["build/swagger-ui-myapp/"]
}
output.resourcesDir = "build/resources/main/swagger-ui-myapp"
}
}
The content of the folder ends up in the BOOT-INF/classes/ folder in the jar. But I don't want the content of the folder in that directory, I want the files to be inside the original folder, so I want them inside BOOT-INF/classes/swagger-ui-myapp.
How can I achieve this?
I wouldn't change the resources to do that. instead, I would just configure the bootJar task:
bootJar {
dependsOn(taskWhichGeneratesTheBuildSwaggerUiMyapp)
bootInf {
into("classes/swagger-ui-myapp") {
from("$buildDir/swagger-ui-myapp")
}
}
}

Create a tar.gz for each folder in Gradle with Tar Task

I have a project that has a folder structure like this
blue/folder1
blue/folder2
red/folder3
red/folder4
and in Gradle I would like to tar up these folders individually so that the output looks like:
build/
tar/
blue/
folder1.tar.gz
folder2.tar.gz
red/
folder3.tar.gz
folder4.tar.gz
Is that possible? How would I do this?
apply plugin: 'base'
['blue', 'red'].each { colour ->
file(colour).listFiles().each { File folder ->
if (folder.directory) {
Task tarTask = tasks.create(name: "tar${colour.capitalize()}${folder.capitalize()}", type: Tar) {
from folder
destinationDir = "${buildDir}/${colour}"
archiveName = "${folder}.tar"
}
assemble.dependOn tarTask
}
}
}

How to change destination folder in zip for gradle distribution plugin [duplicate]

I created a simple Gradle build that exports the contents of ./src/main/groovy to a zip file. The zip file contains a folder with the exact same name as the zip file. I cannot figure out how to get the files into the root of the zip file using the distribution plugin.
i.e. gradlew clean distZip produces:
helloDistribution-1.0.zip -> helloDistribution-1.0 -> files
what I would like:
helloDistribution-1.0.zip -> files
My build.gradle file:
apply plugin: 'groovy'
apply plugin: 'distribution'
version = '1.0'
distributions {
main {
contents {
from {
'src/main/groovy'
}
}
}
}
I have attempted to fix the problem by adding into { 'dir' } but to no avail.
Using into '/' seems to do the trick:
contents {
from {
'src/main/groovy'
}
into '/'
}
Unfortunately, penfold's answer did not work for me. Here is the solution I came up with:
task Package(type: Zip) {
from {
def rootScriptFiles = [] // collection of script files at the root of the src folder
new File('src/main/groovy/').eachFile { if (it.name.endsWith('.groovy')) rootScriptFiles.add(it) }
['build/libs/', // build binaries
'src/res/', // resources
rootScriptFiles, // groovy root source files
]
}
baseName = pluginName
}
To copy files into the root of helloDistribution-1.0.zip -> helloDistribution-1.0 -> use
contents {
from {
'some/file'
}
into ''
}

Gradle Zip Task: Replace Entire File Content While Being Copied

I currently have a set of files that contain a DSL that needs to be parsed and converted to XML before being copied over to the destination build directory.
I'm using the eachFile hook to accomplish this, but when I replace the content of the file the source file is being changed as well:
task build(type: Zip) {
with {
archiveName = "${project.name}-${project.version}.${extension}"
destinationDir = buildDir
}
from('workflow/dsl') {
eachFile { fileDetails ->
String xml = new OozieDslParser().parse(fileDetails.getFile())
fileDetails.setName(fileDetails.getName().replaceFirst(~/\.[^\.]+$/, '.xml')
fileDetails.getFile().text = xml //This changes the source file as well.
}
}
from('workflow/resources')
}
What is the best way to approach this problem?
Unfortunately, the 'expand' and 'filter' options don't seem to work as the former just expands properties and the latter only feeds me one line at a time.
Thanks!
I used a custom FilterReader to solve this problem:
class OozieDslFilter extends FilterReader {
OozieDslFilter(Reader input) {
super(new StringReader(new OozieDslParser().parse(input.text)))
}
}
task build(type: Zip) {
with {
archiveName = "${project.name}-${project.version}.${extension}"
destinationDir = buildDir
}
from('workflow/resources')
from('workflow/dsl') {
rename { it - ~/\.[^\.]+$/ + '.xml' }
filter(OozieDslFilter)
}
}

Gradle Distribution Task Output Files Not at Root of ZIP

I created a simple Gradle build that exports the contents of ./src/main/groovy to a zip file. The zip file contains a folder with the exact same name as the zip file. I cannot figure out how to get the files into the root of the zip file using the distribution plugin.
i.e. gradlew clean distZip produces:
helloDistribution-1.0.zip -> helloDistribution-1.0 -> files
what I would like:
helloDistribution-1.0.zip -> files
My build.gradle file:
apply plugin: 'groovy'
apply plugin: 'distribution'
version = '1.0'
distributions {
main {
contents {
from {
'src/main/groovy'
}
}
}
}
I have attempted to fix the problem by adding into { 'dir' } but to no avail.
Using into '/' seems to do the trick:
contents {
from {
'src/main/groovy'
}
into '/'
}
Unfortunately, penfold's answer did not work for me. Here is the solution I came up with:
task Package(type: Zip) {
from {
def rootScriptFiles = [] // collection of script files at the root of the src folder
new File('src/main/groovy/').eachFile { if (it.name.endsWith('.groovy')) rootScriptFiles.add(it) }
['build/libs/', // build binaries
'src/res/', // resources
rootScriptFiles, // groovy root source files
]
}
baseName = pluginName
}
To copy files into the root of helloDistribution-1.0.zip -> helloDistribution-1.0 -> use
contents {
from {
'some/file'
}
into ''
}

Resources