How to make Gradle copy source directories in certain order? - gradle

I need resources from "myOtherResourcesDir" to be copied to output directory before "src/test/resources".
It looks like FileCollection does not obey the order:
sourceSets.test.resources.srcDirs 'myOtherResourcesDir', 'src/test/resources'
UPDATE:
Can I configure processTestResources task to skip existing files?

I ended up configuring processTestResources task with two "from". Files from 'src/test/resources' are copied last, so that they overwrite files from myOtherResourcesDir.
processTestResources {
from 'myOtherResourcesDir'
from 'src/test/resources'
}

Can you elaborate a bit what you're trying to achieve?
the files are copied in the processTestResourcestask. At the moment there is no way to specify the order when copying multiple directories
I assume the reason you need the ordering is, that a file in src/test/ressources overwrites a file copied from myOtherResourcesDir?. One option is to exclude that file specific file from 'myOtherResourcesDir' programmatically using the eachFile hook
processTestResources{
eachFile{fileDetails ->
if(fileDetails.file.absolutePath.contains("myOtherResourcesDir/fileToExclude.properties")){
fileDetails.exclude()
}
}
}

Related

zip a folder from resources using gradle

I want to make a gradle task to zip a subdirectory called pack from my resources folder, I am not very familiar with gradle, so sorry if this seems like something easy, so far I have this:
task genResourcePack(type: Zip) {
from sourceSets.main.resources
include '**/*'
archiveName 'pack.zip'
}
this zips my entire resources folder, which I do not want, I also tried doing this as well:
task genResourcePack(type: Zip) {
from sourceSets.main.resources.include('pack/')
include '**/*'
archiveName 'pack.zip'
}
which is a little better, but then the zip has a folder called pack at the top, and I want to flatten this folder, but not all the way, basically I want the from to target the subdirectory of pack directly.
I actually just found a solution to my problem, but it isn't particularly elegant so I'll keep this open in case someone has a better way. I replaced my from with the following line:
from sourceSets.main.resources.getSrcDirs()[0].toPath().resolve('pack')

How to modify the file on the fly while copying from one location to other in gradle

I want to modify the contents of file while making the .jar out of it. To do so i am trying to modify the "processResources" task like below -
processResources{
println 'process resources..'
from('./dist'){
into('static')
}
}
Here i have some html files under "dist" folder which I want to modify while copying it into .jar
I have copied the file but didn't get any solution to modify the file while copying.
This is spring boot project along with gradle build tool.
Any help much appreciated!!
Gradle offers out of the box a number of options for changing files during a Copy operation.
I recommend having a look at the relevant documentation and in particular the filter {} block, which can look at files content line by line.
Found the nice documentation which covers the file operations in gradle in great details-
https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch01.html
the solution to my problem is like this >>
processResources{
println 'process resources..'
from('./dist'){
into('static')
filter{
line -> line.replace("old-string","new-string")
}
}
}

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

trying to understand the "from" in the "into" closure about a gradle sample

When learning from the tutorial, I encountered a sample below:
task dist(type: Zip) {
dependsOn spiJar
from 'src/dist'
into('libs') {
from spiJar.archivePath // what's meaning
from configurations.runtime // what's meaning
}
}
artifacts {
archives dist
}
As a newbie to gradle, how to understand this into(...){ from ...}?
In this particular case:
from spiJar.archivePath
is probably (since I don't know what exactly spiJar is) resolved to the output of spiJar task - namely a jar archive - particular file.
When it comes to the second question configurations is (simplifying) is a map that matches given name - runtime in this case - with a group of dependencies (jar files in this case).
When:
from configurations.runtime
is used is copies all the dependencies from runtime configuration into given destination.
In addition to the previews answer, to, possibly, clarify a little. Due to dsl reference, Zip task provide the into(destPath, configureClosure) method, which:
Creates and configures a child CopySpec with a destination directory inside the archive for the files.
This means, that it could create an additional directory with the some content in it. In your case, script creates a libs directory within archive and specifies the resources, which should be copied into this directory. This resources could be out of the src/dist directory, which will be fully zipped into the archive's root.
Here is a dsl reference for CopySpec task, which is configured by the into method of the Zip task. As you can see, from just:
Specifies source files or directories for a copy.

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