Adding file to zip in gradle - gradle

I'm currently building a zip in gradle from file referenced in a configuration using the following code:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
exclude { it.file in configurations.common.files }
Additionally I want to add an image file (logo.png), located in the same directory of the build.gradle .
How can I achieve this?

You can simply add it with one more from method call like:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
from ('logo.png')
exclude { it.file in configurations.common.files }
}

Related

Use gradle to build a jarfile from other jarfiles

I have what should be a simple problem: I have a directory full of jarfiles, and I want to use gradle (v4.4.1) to combine the contents of these jarfiles into a single jarfile (i.e., when I do jar tf big-jar.jar, I want to see a bunch of classes, not a bunch of jars). I tried the following:
task bigJar(type: Jar) {
inputs.dir "$distLibsJar"
outputs.file "$distDir/big-jar.jar"
destinationDir = file("$distDir")
baseName = "big-jar"
from("$distLibsDir")
}
but this produces a jarfile with other jars as its contents rather than the contents of those other jars as the contents of the combined jar.
Any ideas? Thanks...
In this case best way to proceed is with a shadow jar. for this you can have this kind of configuration in build.gradle .posting some sample
apply plugin: 'com.github.johnrengelman.shadow'
shadowJar {
zip64 true
//classifier "shadow" can be mentioned according to need
mergeServiceFiles()
artifacts {
shadow(tasks.shadowJar.archivePath) {
builtBy shadowJar
}
}
}
artifacts {
archives shadowJar
}
distZip {
dependsOn shadowJar
//appendix='software'
eachFile { file ->
String path = file.relativePath
file.setPath(path.substring(path.indexOf("/")+1,path.length()))
}
doLast{
//operation to perform
}
}
distributions {
main {
contents {
//from and to can be mentioned here
}
}
}
hope it helps

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

Gradle task to create a zip archive of a directory

I have a gradle task to create a zip archive of a directory.
The gradle task is:
task archiveReports(type: Zip) {
from '/projects/Reports/*'
archiveName 'Reports.zip'
}
When I am running the command 'gradle archiveReports', its showing the build is successful. However, no zip file is getting created.
Am I missing something here?
I figured out a way for this:
Its working for me now.
task myZip(type: Zip) {
from 'Reports/'
include '*'
include '*/*' //to include contents of a folder present inside Reports directory
archiveName 'Reports.zip'
destinationDir(file('/dir1/dir2/dir3/'))
}
With Gradle 6.7,
task packageDistribution(type: Zip) {
archiveFileName = "my-distribution.zip"
destinationDirectory = file("$buildDir/dist")
from "$buildDir/toArchive"
}
Note : archiveName is deprected.
With Kotlin DSL
tasks.register<Zip>("packageDistribution") {
archiveFileName.set("my-distribution.zip")
destinationDirectory.set(layout.buildDirectory.dir("dist"))
from(layout.buildDirectory.dir("toArchive"))
}
With Groovy
tasks.register('packageDistribution', Zip) {
archiveFileName = "my-distribution.zip"
destinationDirectory = layout.buildDirectory.dir('dist')
from layout.buildDirectory.dir("toArchive")
}
Taken from the official docs
Just in case anyone will come here to find out how to zip your project e.g. to use it as an AWS lambda zip, here you go:
tasks {
val zipTask by register("createZip", Zip::class) {
from(processResources)
from(compileKotlin)
archiveFileName.set("app.zip")
into("lib") {
from(configurations.runtimeClasspath)
}
}
build {
dependsOn(zipTask)
}
}

Change name of zip when creating archive with distribution plugin in gradle

Is there any way to configure distribution file name when creating archive using distribution plugin in gradle?
Here is my build.gradle (I'm using gradle 2.10) file:
apply plugin: 'distribution'
version '1.2'
distributions {
custom {
baseName = 'someName'
contents {...}
}
}
And calling the task customDistZip creates file: $buildDir/distributions/someName-1.2.zip I would like to have a file name configurd to: someName.zip (without version number).
My workaround for this is to create new task to rename the zip file after creation and use gradle finalizedBy feature:
task renameDist {
doLast {
new File("$buildDir/distributions/someName-${project.version}.zip")
.renameTo("$buildDir/distributions/someName.zip")
}
}
customDistZip.finalizedBy renameDist
But this don't look like elegant and nice solution.
You can either do:
customDistZip.setVersion('')
to set the version string used with baseName to empty or
customDistZip.setArchiveName('someName.zip')
to set the full archive filename, which skips basename alltogether.
Kotlin/Groovy:
distributions {
->distribution<- {
distributionBaseName.set(->name<-)
}
}

Extracting files downloaded as a dependency in Gradle

I've got a gradle script that goes something like the following:
apply plugin: 'java'
apply plugin: 'maven'
defaultTasks 'build'
ext.basedir = file('.').getAbsolutePath()
repositories{
maven { url "http://package.repo.com:8081/nexus/content/repository
}
configurations.all {
// check for updates every build
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}
dependencies {
compile group:'com.repo.wwpd', name:'kernel_utilities', version:'3.0.0', changing:true
}
task copy_dependencies(type: Copy) {
from configurations.compile
into basedir+'\\install'
include '*'
{ FileTree ioTree = fileTree(dir: "C:\\Users\\username\\install") }
ioTree.each { f ->
copy {
from tarTree(resources.gzip(f))
into "C:\\Users\\user\\test"
}
}
}
The goal is to get the dependencies, move them to the install folder, and extract them from the tar files to the test folder.
The problem appears to be that the task is executed before the dependencies are downloaded. So if the files already exist in install it works fine, but if the install folder is empty the result is an empty test folder but a full install folder.
[EDIT - updated with comments Peter N.]
This should be one way to solve your case ; note it contains two tasks, choose the one(s) that fulfill your needs: simple copy VS full extraction
def installDir = "${buildDir}/install"
def extractDir = "${buildDir}/extract"
// task to copy dependencies
task copyDependencies(type: Copy) {
from configurations.compile
into installDir
}
// task to extract dependencies
task extractDependencies(type: Copy) {
from configurations.compile.collect{tarTree (it)}
into extractDir
}

Resources