Gradle - Extracting a zip into a specific directory - gradle

I have a zip file as a dependency in my gradle build script. I am able to extract and reference files from it, but it is getting extracted into a folder inside my build directory (I guess that's the default behavior). Is it possible to change this behavior and make gradle extract the zip into a directory of my choice? For ex., I would like to have the zip extracted into a folder directly under my project root.
Thank you in advance for your help.
This is the code snippet:
FileCollection ZipFiles = files(configurations.new_config.findAll
{ it.name.endsWith('.zip') })
FileCollection zipFileContents = files(ZipFiles.collect { zipTree(it) })
return files(zipFileContents.findAll { it.name.endsWith('.jar') })
Actually I have this inside a function. The last line returns only the jar files within the zip. The line before that does the extraction.

Related

How to create a Zip file using gradle 2.3

I have my archive at location myProject/unzip and need to:
Zip the contents within the unzip folder and create a zip file with some name at any location within the project.
Using gradle 2.3.
Can anyone help me for this?
I am not sure what you are doing differently than the examples in the documentation from the link I gave in the comments. But this works for me with Gradle 2.3:
task myZip(type: Zip){
from "$projectDir/unzip"
archiveName = "my-zip.zip"
destinationDir = buildDir
}
The input folder is called unzip and needs to present as a child in the project folder. It outputs a file my-zip.zip in the build folder.
Be sure that there actually are some resources located in the from path, or Gradle might skip it.

Gradle: Include dynamically generated file in build?

During my gradle build, I generate a temporary buildinfo.properties file containing things like Git commit info, build time, etc. I would like to include this file in my output *.jar / *.war files as a resource. However, I do not want to put this file in my project src/ folder (this would require fiddling with .gitignore and in general it just seems unnecessary to me). Ideally, the developer shouldn't even see this file at all, it should just be contained in the output archive.
How would you include a dynamically generated text file in a Gradle build?
Add that file in a jar task (Kotin DSL):
tasks {
val jar by getting(Jar::class) {
from("build/buildinfo.properties")
}
}
It will add build/buildinfo.properties file (assuming you generate it there with another taks) to the root of your JAR.
For dynamically generated file, standard gradle way to process resources is the gradle task called processResources. You can do something like this:
processResources {
dependsOn taskThatGeneratesYourBuildinfo
from("build/buildinfo.properties") {
into("desired/path/in/jar")
}
}

Copy to directory outside of project directory using Gradle 4.0.2

I have a gradle build which generates war file. I want to copy war file to my application servers' dropins directory which is somewhere outside of project directory. I have following copy task to do this.
task copyWarToDropins(type: Copy,dependsOn:[war]) {
from './build/libs/bds-service-token-1.0-SNAPSHOT.war'
into file('/apps/dropins') // want to copy to 'C:/apps/dropins' directory
rename { fileName -> 'bds-service-token.war' }
}
build.dependsOn copyWarToDropin
It evaluates /apps/dropins relative project directory and does copy there. I have tried many ways I can think of but could not make it copy to C:/apps/dropins directory.
Can someone please help?
First, please note that using into file(...) is redundant, as each call to into(...) will be evaluated via Project.file(...) anyhow.
As you can read in the documentation , file(...) handles strings in the following way:
A CharSequence, including String or GString. Interpreted relative to the project directory. A string that starts with file: is treated as a file URL.
So, one way to solve your problem could be using an absolute file URL.
However, if you continue to read the documentation, you will see that Java File objects are supported. So you could simply create such an object:
into new File('C:/your/absolute/path')

How do I copy a file into my WAR's META-INF directory?

I’m using Gradle 2.7. I would like to copy an environment-specific file (context.xml) into my WAR’s META-INF directory (which is at the same level as WEB-INF). I have this task set up in my build.gradle file
// Copy context.xml into the appropropriate directory.
war {
def env = project.hasProperty('env') ? project.env : 'dev'
from("${project.rootDir}/src/main/environment/$env") {
include('context.xml')
into('META-INF')
}
}
However, when I run “gradle build”, nothing gets copied. I have verified the file exists. What else do I need to do to get this file to copy properly?
It seems that your script is configured correctly. It's stupid question (it should've failed at the very beginning) but have you applied war plugin?
Here you have a demo to compare the configurations.
Run:
jar -tvf <lib>.war
to verify if war contains appropriate file.

Gradle replacing Maven assembly plugin

I'm fairly new to Gradle, and trying to port an existing Maven pom.xml that makes extensive use of maven-assembly-plugin to create various zip files.
In the example below, I'm getting files from various subdirectories (with particular extensions), and then mapping them in a flat structure to a ZIP file.
task batchZip(type: Zip) {
from fileTree('src/main/sas') {
include('**/*.sas')
include('**/*.ds')
}.files
}
This puts all the files in the root of the zip. What I ideally need though, is for the files to live under a particular path in the root of the zip, e.g. /shared/sas.
Is there a way to do this without first copying all the files to a local directory and then zipping that up?
task batchZip(type: Zip) {
into('shared/sas') {
from { fileTree('src/main/sas').files }
include('**/*.sas')
include('**/*.ds')
}
}
Have a look at the docs. It seems that if You specify appropriate into You'll get the result You're looking for.

Resources