Gradle task to delete contents in a directory but exclude jar file - gradle

I still trying to learn Gradle Task authoring.
I found the following solution to help with deleting the contents of a directory while leaving the directory untouched.
However can I please have help modifying it to exclude specific files such as the .jar file?
How to delete an empty directory (or the directory with all contents recursively) in gradle?
The solution was given by Heri
task deleteGraphicsAssets(type: Delete) {
def dirName = "src"
file( dirName ).list().each{
f -> delete "${dirName}/${f}"
}
}

task deleteGraphicsAssets(type: Delete) {
doLast {
file('src').eachFileRecurse(groovy.io.FileType.FILES) { File file ->
//Delete all files except *.jar
if (!file.name.endsWith('.jar')) {
delete file
}
}
}
}
Another option, is using Filetree for more advanced filter, for example:
task deleteGraphicsAssets(type: Delete) {
delete fileTree(dir: "tempDir", exclude: "dontDelete.jar")
}

Related

Gradle Copy task issue with duplicate file

In build.gradle I have the following code:
task copyModifiedSources(type:Copy) {
from ('ApiClient.java.modified') {
rename 'ApiClient.java.modified', 'ApiClient.java'
}
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
into 'build/generated/src/main/java/com/client/invoker'
}
I am trying to copy ApiClient.java.modified (renamed to ApiClient.java before copy) file into build/generated/src/main/java/com/client/invoker folder which already has ApiClient.java file.
I am using a duplicate strategy to override APIClient.java from /invoker folder with ApiClient.java.modified.
If I rename the copying file to the file name not in /invoker folder, then it works but does not work with the duplicate file name. The file is not copied successfully.
Could you please help me to figure out what is going wrong?
Gradle -> 6.5.1
Java -> 11
the rename is same level as from not under from.
below example final file in build/gen content is 'override'
task setupFiles() {
doLast {
mkdir 'build/override'
mkdir 'build/gen'
new File('build/gen/ApiClient.java').text = "orig"
new File('build/override/ApiClient.java.modified').text = "override"
}
}
task copyModifiedSources(type: Copy, dependsOn: setupFiles) {
from 'build/override'
include '*.modified'
rename '(.*).modified', '$1'
setDuplicatesStrategy(DuplicatesStrategy.INCLUDE)
into 'build/gen'
}
run gradle clean copyModifiedSources

Gradle zip: how to include and rename one file easily?

Create a zip adding file "hello/world.xml" under directory "foo/bar" as "hello/universe.xml"
task myZip(type: Zip) {
from ("foo/bar") {
include "hello/world.xml"
filesMatching("hello/*") {
it.path = "hello/universe.xml"
}
}
}
filesMatching(...) will impact performance obviously.
What is a better way? like:
task myZip(type: Zip) {
from ("foo/bar") {
include ("hello/world.xml") {
rename "hello/universe.xml"
}
}
}
But rename is not supported with include.
I don't get why you are using filesMatching at all. You are only including one single file in your child CopySpec. Simply rename it and everything is fine:
task myZip(type: Zip) {
from ('foo/bar') {
include 'hello/world.xml'
rename { 'hello/universe.xml' }
}
}
If you want to include multiple files (or even copy all), but only want to rename one of them, specify which file(s) to rename with a regular expression as first argument:
task myZip(type: Zip) {
from 'foo/bar'
rename 'hello/world.xml' 'hello/universe.xml'
}
If the last one did not work, try:
rename ('a.java', 'b.java')

Adding file to zip in gradle

I'm currently building a zip in gradle from file referenced in a configuration using the following code:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
exclude { it.file in configurations.common.files }
Additionally I want to add an image file (logo.png), located in the same directory of the build.gradle .
How can I achieve this?
You can simply add it with one more from method call like:
task makeZip(type: Zip) {
baseName 'libsZip'
from configurations.compile
from ('logo.png')
exclude { it.file in configurations.common.files }
}

How to delete an empty directory (or the directory with all contents recursively) in gradle?

I can't figure out how to delete all contents of a directory.
For cleaning out a directory, I want to remove all files and directories inside it: I want to wipe everything there is inside (files and directories).
I tried this with the delete task, but I can't figure out to make it also remove directories and not just files. I've tried different ways to specify the directories, but nothing works.
task deleteGraphicsAssets(type:Delete) {
delete fileTree('src').include('**/*')
}
.
task deleteGraphicsAssets(type:Delete) {
delete fileTree('src').include('/')
}
.
task deleteGraphicsAssets(type:Delete) {
delete fileTree('src').include('*')
}
Any help appreciated!
Edit:
This works - yet it seems a bit like a hack.
task deleteGraphicsAssets(type: Delete) {
def dirName = "src"
delete dirName
doLast {
file(dirName).mkdirs()
}
}
I was looking for something like:
task deleteGraphicsAssets(type: Delete) {
deleteContentsOfDirectory "src"
}
or
task deleteGraphicsAssets(type: Delete) {
delete {dir : "src", keepRoot : true }
}
To delete the src directory and all its contents:
task deleteGraphicsAssets(type: Delete) {
delete "src"
}
At the risk of resurrecting an answered topic, there's a relatively easy way to do this.
This task will delete all files and directories under 'src' without traversing the file tree and without removing to 'src' dir
task deleteGraphicsAssets(type:Delete) {
delete file('src').listFiles()
}
Groovy enhances the File class with several methods. You can delete a directory with all it's subdirectories and files using deleteDir() method.
task deletebin << {
def binDir = new File('bin')
binDir.deleteDir()
}
Following will delete all content from the src folder but leaves the folder itself untouched:
task deleteGraphicsAssets(type: Delete) {
def dirName = "src"
file( dirName ).list().each{
f ->
delete "${dirName}/${f}"
}
}
clean {
delete += fileTree('src').include('**/*')
}
This 'clean' task's configuration seems to work.
Found using FileTree#visit worked.
ConfigurableFileTree ft = fileTree('someDir')
ft.include("xxx")
ft.exclude("yyy")
task delteFilesOnly() {
doLast {
//// for test
//ft.each { File file ->
// println "===== " + file.absolutePath
//}
delete ft
}
}
task deleteFilesAndDirs(){
doLast {
ft.visit { FileVisitDetails fvd ->
//// for test
//println "----- " + file.absolutePath
delete fvd.file
}
}
}

Remove a directory when unzipping

I am unpacking a zip file into a directory. The zip file has an extra top level directory that I don't want in the unzipped destination.
task unpackDojoSource(type: Copy) {
new File("build/dojo/src").mkdirs()
from(zipTree(dojoSource)) {
eachFile { details -> details.path =
details.path.substring(details.relativePath.segments[0].length()) }
} into "build/dojo/src"
}
The task produces the following output
/dijit
/dojo
/dojo-release-1.7.2
/dijit
/dojo
/dojox
/util
/dojox
/util
Is there a way I can prevent the dojo-release directory from being created?
Ref:
http://gradle.markmail.org/thread/x6gmbrhhen63rybe#query:+page:1+mid:lws7nlqcncjumnvs+state:results
I just stumble upon the same problem. Only thing you need to do is to add includeEmptyDirs = false into your copy spec like this:
task unpackDojoSource(type: Copy) {
new File("build/dojo/src").mkdirs()
from(zipTree(dojoSource)) {
eachFile { details -> details.path =
details.path.substring(details.relativePath.segments[0].length()) }
} into "build/dojo/src"
includeEmptyDirs = false
}
The resulting structure will not contain the empty directories left by flattening.
I've just ran across this myself. I worked around it by doing a delete afterwards. Less than ideal but still effective.
delete "dojo-release-1.7.2"

Resources