Delete directory with all files in it using gradle task - gradle

In my project's root I have a directory as follows:
build/exploded-project/WEB-INF/classes
I want to delete all the files in the classes directory using a gradle task. I tried the of the following combinations but none of them worked:
task deleteBuild(type: Delete) {
project.delete 'build/exploded-project/WEB-INF/classes/'
}
task deleteBuild(type: Delete) {
delete 'build/exploded-project/WEB-INF/classes/'
}
task deleteBuild(type: Delete) {
delete '$rootProject.projectDir/build/exploded-project/WEB-INF/classes/'
}
task deleteBuild(type: Delete) {
delete fileTree('build/exploded-project/WEB-INF/classes').matching {
include '**/*.class'
}
}

Your second variant is correct and works fine here.
Though I'd recommend not hardcoding the path.
Use $buildDir instead of build, or if the path is the output path of another task use the respective property of that task.
If it doesn't work for you, run with -i or -d to get more information about what is going on and possibly going wrong.

As suggested a better approach is to use Gradle Variables.
Try:
task deleteBuild(type: Delete) {
delete "$buildDir/exploded-project/WEB-INF/classes/"
}
Pay attention to replace the single quote with the double one.
Regards,
S.

Related

Copy files and rename - Gradle

I know there are already a couple of StackOverflow posts regarding copying and renaming for Gradle but sadly none seem to be applicable to my issue...
I'm trying to copy and rename files from multiple directories, here's my approach:
task copyReportsForDocs(type: Copy) {
from rootProject.files("path1",
"path2",
"path3",
"path4")
into genDir
rename '(.*)_$d_(.*)', '$1$2'
}
The files which should be renamed look something like this:
captured_0_foobar_request.adoc
captured_0_foobar_response.adoc
captured_1_fuubar_request.adoc
captured_1_fuubar_response.adoc
I just need the front portion to be deleted so it looks something like this:
foobar_request.adoc
foobar_response.adoc
fuubar_request.adoc
fuubar_response.adoc
Any help is appreciated, thanks!
This worked for me:
task copyReportsForDocs(type: Sync) {
from(rootProject.file("path")) {
include '**/*foo*'
rename 'captured_\\d+_(.+)', '$1'
}
}

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"

Regex Directory matching in Groovy

As part of Gradle delete task, I would like to delete all tar files beginning with CSW and ending with .tar.gz from a directory. How can I achieve it with Groovy?
I tried something like below:
delete (file('delivery/').eachDirMatch(~/^CSW$.*.gz/))
But it doesn't work. How do I employ the regex in Groovy. As compared to shell it is something like rm -rf CSW*.tar.gz
I will answer with a gradle solution as you refer to gradle in your question. Something along the lines of:
myTask(type: Delete) {
delete fileTree(dir: 'delivery' , include: '**/CSW*.tar.gz')
}
where the delete method call is configuration time and configures what will be deleted when the task eventually runs. For details it might be worth looking through the gradle docs on the fileTree method.
If you need to stay pure groovy you could do something along the lines of:
new AntBuilder().fileScanner {
fileset(dir: 'delivery', includes: '**/CSW*.tar.gz')
}.each { File f ->
f.delete()
}
If this code lives in a gradle script I would recommend sticking with option one as it retains the gradle up-to-date checking and fits well within the gradle configuration vs execution time pattern.
If you really want to go regex as opposed to the above style patterns you can certainly do that, though I would personally not see much of a point given your requirements.

How to remove an element from gradle task outputs?

is it possible to exclude an element from the output files of a Task in order to not consider it for the up-to-date check? In my case I have a copy task that automatically set the destination directory in outputs variable, but I'd like to remove it and set only some of the copied files.
Or, as alternative, is it possible to overwrite the entire outputs variable?
Thanks,
Michele.
Incremental tasks create snapshots from input and output files of a task. If these snapshots are the same for two task executions (based on the hash code of file content), then Gradle assumes that task is up-to-date.
You are not able to remove some files from output and expect Gradle to forget about them, simply because the hash codes will be different.
There is an option that allows you to manually define the logic of up-to-date checks.
You should use a method upToDateWhen(Closure upToDateClosure) in TaskOutputs class.
task myTask {
outputs.dir files('/home/user/test')
outputs.upToDateWhen {
// your logic here
return true; // always up-to-date
}
}
I've found the solution:
task reduceZip(type: Copy) {
outputs.files.setFrom(file("C:/temp/unzip/test.properties"))
outputs.file(file("C:/temp/unzip/test.txt"))
from zipTree("C:/temp/temp.zip")
into "C:/temp/unzip"
}
Outputs.files list could be modified only register new elements, not removing (for what I know). So I need to reset the list and then eventually add other files. The outputs.files.setFrom method reset the outputs.files list so it is possible add other file. In the example above I reduce the up-to-date check only to the test.properties and test.txt files.

Rename files in assemble task in gradle

I need to write a simple task to create a zip from the source code. I need to include a dir called 1-dir or 2-dir depending on a system property. But the name of the directory in the resulting zip should always be dir. So basically, I want to include a dir in a zip (conditionally) and rename it.
I tried the rename method but that does not work.
Any pointers?
It will be:
task zipDir(type: Zip) {
def fromDir = project.hasProperty('from') ? project.from : 'dir1'
from(fromDir)
into('dir')
}
It can be run in the following way gradle zipDir -Pfrom=dir2. If no from property is passed dir1 will be zipped.
If you need system property instead of gradle property, pass -Dfrom=dir2 and use System.properties['from'] instead of project.from.

Resources