Gradle copy resources, processResources - gradle

I am trying to copy 2 folders from my project folder into the jar.
When I use following settings Gradle copies the files inside the folders, not the folders themself:
processResources {
from 'public'
from 'data'
}
What I need is to have folder "projectDir/data" copyied into "my.jar/data".

There is a method of 'from' that takes a closure with CopySpec.
I would recommend using this instead of either breaking up into separate tasks or restructuring your layout.
processResources {
from('data') {into 'data'}
from('publicFolder') {into 'publicFolder'}
}

The copy spec in Gradle copies contents of a folder if the specified path in from is a folder.
One solution is to specify the target folder in the jar using the into directive.
processResources {
from 'data'
from 'publicFolder'
}
task data(type: Copy) {
from 'data' into 'data'
}
task publicFolder(type: Copy) {
from 'public' into 'public'
}
This will put the contents or the original folders into the folder with the same name within the jar. But as you can see, you'll need to repeat the folder names within the into closure.
Another workaround is to have a parent folder for these resource folders and use this parent folder in the copy spec.
Here's one way you could structure the resource folders:
<project-folder>
|
--> res
|
--> public
--> data
Then in the copy spec of processResources, you can do the following:
processResources {
from 'res'
}
This will create folders public and data in the final jar file as you want.

Related

Copying a single file into .jar with a different name using the Gradle task "jar"

How can I copy a single file into .jar with a different name using the Gradle task "jar"?
For example
local:src/main/resources/mypkg_prod.properties → jar-file:/mypkg.properties
You can copy the file into the jar and then do a rename.
This is a link to some information on the rename method from the Gradle docs:
https://docs.gradle.org/current/userguide/working_with_files.html#sec:renaming_files
Example:
jar {
from 'my_file.txt'
rename 'my_file.txt', 'my_super_file.txt'
}
Note: If your file is already being included in the jar, just add the rename line to the jar task:
jar {
rename 'mypkg_prod.properties', 'mypkg.properties'
}

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

Need a gradle jar task that copies classes normally and custom files

In my Gradle build, after the classes are compiled, I need to create a jar containing the classes in the typical location, but I also need the jar to include a set of other plain text files from a specific subdirectory of the project, and going into a different named folder in the jar.
So, for instance, at the root of the jar, I'll have the "com" folder that starts the classes tree, but I'll also have a "META-INF" folder which contains a custom subfolder name, containing the files copied from the project.
Update:
I tried adding the following:
jar {
from ("src/main/resources/yang") {
into ("META-INF/yang")
}
}
This comes close to working properly. It does copy all the files from that "from" folder" to the "into" folder in the resulting jar. However, the resulting jar ALSO has a "yang" folder containing the same files from "src/main/resources/yang". So, how do I prevent the default copying of that folder?
I might end up doing this as part of a custom plugin, but for now I'd like to see if I can configure a simple "jar" task to do this.
Update:
Based on the solution, the following worked:
jar {
exclude "yang"
from ("src/main/resources/yang") {
into ("META-INF/yang")
}
}
As far as I understood build.gradle can be modified in the following way:
apply plugin: 'java'
jar {
from('other') {
into('META-INF/other')
}
}
Full demo can be found here
UPDATE
This should work:
apply plugin: 'java'
jar {
from('src/main/resources/other') {
into('META-INF/other')
}
exclude 'other/**'
}

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.

Gradle - Extracting a zip into a specific directory

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.

Resources