Exclude files from FileTree in Gradle - filter

I want to exclude src\main and src\test files from src
FileCollection files =
project.fileTree(/src/).minus(project.fileTree(/src\main/)).minus(project.fileTree(/src\test/))
How can I exclude this directories without double minus usage?

The idiomatic way to exclude subdirectories from a FileTree is:
def files = fileTree("src").matching {
exclude "main", "test" // relative to the file tree's root directory
}
PS: Instead of .minus, you can use -.

Related

Using gradle typed tasks how can we exclude different types of files?

Using Gradle typed task how can we exclude file copy for file names starting with as well as ending with some strings?
def contentSpec = copySpec {
exclude {
it.file.name.startsWith('img')
it.file.name.endsWith('gif')
}
from 'src'
}
task copyImages (type: Copy) {
with contentSpec
into 'Dest'
}
On running gradle copyImages, it excludes files ending with gif, but does not exclude files starting with img.
Is there a way to achieve both?
You forgot an or (||) between your two conditions:
exclude { it.file.name.startsWith('img') || it.file.name.endsWith('gif') }
The value of a closure is the value of its last expression. Since the last expression, in your code, is it.file.name.endsWith('gif'), that's the value of the closure, and the file is thus excluded when it.file.name.endsWith('gif') is true.
Of course, you could also use two exclusions:
exclude {
it.file.name.startsWith('img')
}
exclude {
it.file.name.endsWith('gif')
}

Gradle Copy format to include several files

I would like to use the Gradle Copy task, and to specify several files to include from the given directory.
I have seen the syntax with several include directives:
task myTask (type: Copy) {
from: myDir {
include "file1.txt"
include "file2.csv"
include "file3.xml"
}
into: dest
}
But I would like to do that in one line, so that I can use a variable I receive as argument of the include directive.
The syntax is simply to pass an array of Strings:
task myTask (type: Copy) {
from: myDir {
include ["file1.txt", "file2.csv", "file3.xml", "**/otherfile", "*.java"]
}
into: dest
}

What's the most elegant way to copy just one file with gradle

What's the most concise and most elegant and the shortest way to copy just one file AND rename it with gradle?
So far I could think of just this:
copy {
from projectDir
into projectDir
include '.env.template'
rename '.env.template', '.env'
}
You can simplify your CopySpec:
copy {
from file('.env.template')
into projectDir
rename '.*', '.env'
}
The from method accepts single File objects and, since only this one file is copied, the rename pattern can match any copied file.
This way is simple and clean, but to follow the Gradle concept, you should consider using a Copy task, to maintain a clean cut between configuration and execution phase.
Edit:
I just learned, that one can provide a closure for the rename method, so you could also use:
copy {
// ...
rename { '.env' }
}
task copySingleFileInGradle {
doFirst {
def src = new File("sourcefile") // this must exist in top-level project dir
def dst = new File("destinationFile") // this will get created when task is run
dst.write(src.text)
}
}

Get the names of all the files/folders in an array

Basically I have a folder in that I have 4 zip files. I want to get the names of these zip files in an array.
Requirement :
I have a folder AggregatedComponetLibraries: I have my lib zips inside it. a.zip,b.zip,c.zip,d.zip. I want to get the name of the zips insde an array componentNames in gradle that means my array should contain : a,b,c,d
You can get it with FileTree so:
def names = []
fileTree(dir: 'AggregatedComponetLibraries', include: '**/*.zip').visit {
FileVisitDetails details ->
names << details.file.name
}
task printNames << {
println names
}
Here is the names array defined and then created a FileTree instance, for directory named "AggregatedComponetLibraries", this tree includes all the files with zip extention. After that, script traverses over the tree elements and add the element's names into the array.
Task printNames here is just to show the result.

Gradle Zip DuplicatesStrategy not working correctly

Suppose you have something like:
task zip(type: Zip) {
archiveName = "out.zip"
duplicatesStrategy = 'exclude'
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
from("$rootDir/customizations/folder2")
}
}
According to http://www.gradle.org/docs/current/javadoc/org/gradle/api/file/DuplicatesStrategy.html Exclude means
Do not allow duplicates by ignoring subsequent items to be created at the same path.
So if you have the same filename in folder1 & folder2 only the file from folder1 should end up in the zip. If you change the two "from" lines in the buildfile, only the file from folder2 should end up there. This seems not to be whats happening (gradle 1.10). Instead always the same file is used. Seems like nested "from"s do not preserve their order.
The only solution I've found is to split up the conflicting parts:
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder1")
}
into('TARGET_FOLDER_IN_ZIP') {
from("$rootDir/customizations/folder2")
}
now the order is preserved and the output is deterministic

Resources