Gradle Distribution Task Output Files Not at Root of ZIP - gradle

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 ''
}

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")
}
}
}

gradle - unpack zip in distribution

My gradle build is creating a distribution for me:
apply plugin: 'distribution'
distributions {
main {
contents {
from ('src/resources/test.zip')
// other stuff
}
}
}
This results with test.zip and all the other stuff inside my created distribution. My question is how is it possible to unpack test.zip into the new distribution?
Use a zipTree to get the contents of the zip file:
contents {
from (zipTree('src/resources/test.zip'))
// other stuff
}

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 put folder into a tar file with 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

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 ''
}

Resources