Gradle Copy Task: How can I avoid creating subdirectories while copying subdirectories content - gradle

I am trying to copy files from a parent directory which contain multiple sub directories. The required file Tree structure is below:
MachineLogs/XXXXX/*_CORE.txt
MachineLogs/YYYYY/*_CORE.txt
I am using the following code for selecting and copying required files:
from "$localLogsDir/CoreLogsUos1"
include '*/*_CORE_*.*'
into new File(analysisChainDir, 'CORE')
includeEmptyDirs = false
exclude { details -> details.file.isDirectory()}
The above snippet is copying the CORE files correctly but it is also copying the directory in which they are present. I can not name the sub-directories as they are created dynamically according to present date.

You can try in the following way (mind the regex, it does not match at the moment):
task copyLogs(type: Copy) {
from fileTree('logs').filter { it.isFile() && it.name.matches('.*_CORE.*') }
into 'dist'
includeEmptyDirs = false
}
Here you can find a demo.

Related

Gradle copies all the files if at least one is not up-to-date

For instance, I've got the following task:
task testCopy(type: Copy) {
from ("folder/copy_from")
into ("folder/copy_to")
eachFile {println it.name}
}
Unless the inner files of the folder copy_from are touched, task works fine. As soon as I change, let's say one file in the folder copy_from, then Gradle begins to copy all the files from copy_from folder into copy_to instead of copying only one changed/added file.
Is this behaviour expected? Is there a way to make Gradle copy only changed/added file?
Yes based on this github issue and gradle discuss:
The build is incremental in the sense that the copy task only executes
when things have changed but it isn’t incremental itself, in that it
only copies inputs that have been modified.
I couldn't find a propper solution, but one solution is just splitting your task into smaller ones with specific types.
task copy1(type: Copy) {
into 'build/out'
from ('src') {
include 'docs/*.txt'
}
eachFile {println it.name}
}
task copy2(type: Copy) {
into 'build/out'
from ('src') {
include 'docs/*.md'
}
eachFile {println it.name}
}
task copy3 {
dependsOn copy1, copy2
}
It's not exactly what you want but it improves the performance by reducing files to copy.
when you change a text file and run gradle copy3 it just copies the text files not md files.
UPDATE:
Ant copy task doesn't have this problem
from it's documentation:
By default, files are only copied if the source file is newer than the destination file, or when the destination file does not exist. However, you can explicitly overwrite files with the overwrite attribute
So you can use ant copy task instead, as we can use ant tasks from gradle:
task copyFromAnt {
doLast {
ant.copy(todir: 'build/out') {
fileset(dir: 'src')
}
}
}
ant logs the files it copies so you can check the log with help of gradle -d and grep:
gradle copyFromAnt -d | grep "\[ant:copy\]"
and to see just the files it copies with out up-to-dat and etc. you can use the below command:
gradle copyFromAnt -d | grep "\[ant:copy\] Copying"

How to exclude files from only one srcDir in a sourceSet?

I have a java project with two resource directories, res and raw. I've set this up in my build.gradle like so:
sourceSets.main.resources.srcDirs = ['res', 'raw']
Both of these directories contain a subdirectory called graphics. The issue is I want to exclude the graphics directory from raw, but not from res.
I have tried:
sourceSets.main.resources.srcDirs = ['res', 'raw']
sourceSets.main.resources.exclude 'raw/**/graphics'
but this does not work, because the exclude pattern is relative to the srcDirs.
How do I set an exclude pattern only for the raw directory?
The reason that didn't work is because the expression: sourceSets.main.resources.exclude 'raw/**/graphics', creates a new live view of the files, where the excluded files do not exist.
Simply put, it does not affect sourceSets.main.resources.
To make it "permanent", you have to use the groovy DSL syntax:
sourceSets.main {
resources {
srcDirs 'res', 'raw'
exclude 'raw/**/graphics'
}
}

include single directory in an archive with gradle Zip task

I have a directory A under my project's rootDir. Directory A has subidrectories B,C,D,E,etc.
I want to create archive of only B and ignore others. I have written a gradle task to zip the directory B.
task ZipOnlyB(type: Zip){
archiveName = "ArchiveOfB.zip"
destinationDir = file ("${rootDir}/toSendToArtifactory")
from "${rootDir}/A/B"
}
The above code works and creates an archive with name ArchiveOfB.zip. But this archive doesn't contain the directory B. The archive only contains the contents of directory B.
Expected content of zip file : A/B/contents-of-directory-B
Actual content of zip file : A/contents-of-directory-B
How do I go about getting the expected result. Any guidance in this regard is highly apprecited. i have looked up the "Working with fiels" section of Gradle documnetaion,but that doesn't help my cause. I was expecting some "include" property for including diretcories, but the include works only with files and no directory example was given.
You can use into method (see https://docs.gradle.org/current/dsl/org.gradle.api.tasks.bundling.Zip.html#org.gradle.api.tasks.bundling.Zip:into(java.lang.Object) ) to specify the destination directory inside the archive
In you case you could do something like:
task ZipOnlyB(type: Zip){
archiveName = "ArchiveOfB.zip"
destinationDir = file ("${rootDir}/toSendToArtifactory")
from fileTree('A/B')
into ('A/B')
}
This will create the archive with first level directory "A" containing the "B" directory and its content. If you don't want this root directory "A" but just directory "B", then use into ('B')

how to exclude path information in gradle zip task

Apologies in advance for this simple question I am relatively new to Gradle.
I would like to run a Zip task during my Build process and create a .zip file in my "archive" directory but without the source file's directory structure. I managed to get the task working but its retaining the directory structure. I read that for linux Zip there is the option -j or -r which flattens the directory but I am not sure if this is available to me via the Gradle task.
My input file structures looks similar to the below,
./libs/
./libs/file1.jar
./scripts/script1.sh
./scripts/script2.sh
but I would like to end up with a Zip file with directory structure as follows,
./
./file1.jar
./script1.sh
./script2.sh
My current Zip task is as follows,
task makeZipForDeploy(type: Zip) {
from 'build/'
include 'libs/*' //to include all files in libs folder
include 'scripts/*' //to include all files in the scripts folder
archiveName("${archiveFileName}")
destinationDir file("${archivePath}")
}
the solution was even more trivial than I thought. The below will flatten the structure
task makeZipForDeploy(type: Zip) {
from 'build/libs' //to include all files in libs folder
from 'build/scripts' //to include all files in the scripts folder
archiveName("${archiveFileName}")
destinationDir file("${archivePath}")
}

Gradle copy task not working

I am attempting to convert my JavaFX build from Maven to Gradle. One thing that I need to do is copy my files from a non standard location to one that my javafx-gradle-plugin can use.
For some reason, gradle isn't copying the files, but I am not getting any errors.
This is my task:
task copyRequiredRuntimeConfiguration(type: Copy) {
logger.error('***************************************************Source Folder is')
FileTree tree = fileTree(dir: 'properties')
tree.each {File file ->
println file
}
from 'properties'
into '{project.buildDir}/additionalResources/properties'
include '**/*.*'
logger.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Destination Folder is')
FileTree tree2 = fileTree(dir: '{project.buildDir}/additionalResources/properties')
tree2.each {File file ->
println file
}
}
The output I am getting is:
***************************************************Source Folder is
C:\workspace\GRADLE-POC\master-module\app\properties\log4j.xml
C:\workspace\GRADLE-POC\master-module\app\properties\server.properties
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Destination Folder is
:javafx-framework:compileJava
For some reason the copy is never occurring and the folder isn't being created. I have tried creating the directory first (which created the structure) but the copy into that location didn't work either.
I am really new to gradle, so this might be really easy - I just can't seem to determine the issue. However I can see the listing of the source destination.
I think your only problem is a syntax one, just replace the curly braces and the single quotes in your script, with a dollar sign and double quotes so your script will look like this.
task copyRequiredRuntimeConfiguration(type: Copy) {
logger.error('***************************************************Source Folder is')
FileTree tree = fileTree(dir: 'properties')
tree.each {File file ->
println file
}
from 'properties'
into "$projectDir/additionalResources/properties"
include '**/*.*'
logger.error('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Destination Folder is')
FileTree tree2 = fileTree(dir: "$projectDir/additionalResources/properties")
tree2.each {File file ->
println file
}
}
It will work!

Resources