I'm looking for some mechanism that would allow me to execute an action over each file in a directory that matches a certain pattern.
I'm currently trying to make fileTree work this way.
def srcDir = 'myDir'
def includePattern = '*'
def tree = fileTree(srcDir) {
include includePattern
}
tree.visit { d ->
logger.info(d.file)
}
My directory looks like this:
myDir/file1
myDir/file2
myDir/subDir/file3
What I would like to have as output is:
/../myDir/file1
/../myDir/file2
But of course subDir also matches the * include pattern. So it gets included in the result.
How can I only visit files?
If you do not want to do a recursive scan but just scan a single directory, you can do something like eachFileMatch:
import static groovy.io.FileType.*
new File('myDir').eachFileMatch FILES, ~/.*/, { f ->
println f.name
}
which would print:
─➤ groovy solution.groovy 1 ↵
file2
file1
where f is a java.io.File and ~/.*/ is a regular expression matching on file names. .* means any character, zero or more times. To match say a .txt extension you would do something like ~/.*\.txt/.
Consider:
def srcDir = 'myDir'
task go() {
doFirst {
new File(srcDir).eachFileRecurse { f ->
if (f.isFile()) {
println f.absolutePath.replaceAll("${projectDir}", "/..")
}
}
}
}
where this is invoked with gradle go.
The best thing I could come up with that will still work with an include pattern (and exclude if needed) is to add an if in the visit closure.
def srcDir = 'myDir'
def includePattern = '*'
def tree = fileTree(srcDir) {
include includePattern
}
tree.visit { d ->
if (!d.file.isDirectory()) {
logger.info(d.file)
}
}
Related
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 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)
}
}
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
Very similar to this question: Gradle Zip task to do multiple sub-trees? Which I don't believe is fully answered, merely circumvented..
I have a project with child projects, built with gradle 1.6, and I need to assemble some results into multiple paths, but I too see the last path surviving.
task zip (type: Zip) {
from ('src/resources') {
into '/'
}
from ('web') {
into '/'
}
from project('A').reference { into ('A') }
from project('B').reference { into ('B') }
}
(Essentially the reference task creates a few directories which are named the same in A and B, so needs to prepend the project name)..
Obviously the references all end up into /B/** in the zip file. When I reverse the order of the two lines, they end up in /A/**.
The other two goes correctly into /. If I move the subproject up before the root resources, they would still go in either /A or /B depending on their order, but the normal resources end in / as assumed.
I would essentially like to include the subprojects dynamically, i.e.
project.subprojects.each {
def pname = it.name
from project(pname).reference {
into "$pname"
}
}
but all my attempts so far has been in vain.
Any pointers welcome
The syntax doesn't look right. It should be from(project('A').reference) { into ('A') }. (Same for B.) Does this make a difference?
PS: into "/" is redundant and can be omitted.
I would like to copy some files in Gradle and the resulting files should not contain any blank lines, i.e., the blank lines are not copied. I assume that can be done with filter(...) and maybe with the TokenFilter from ant. However, I am not sure how to the syntax would look like.
Thanks.
Gradle uses Ant for filtering, because of its powerful implementation. For example, you can use the LineContainsRegExp Ant filter to filter out any line that is only empty or whitespaces.
The appropriate regexp can be [^ \n\t\r]+
You can use Ant directly from Gradle like this:
task copyTheAntWay {
ant.copy(file:'input.txt', tofile:'output.txt', overwrite:true) {
filterchain {
filterreader(classname:'org.apache.tools.ant.filters.LineContainsRegExp') {
param(type:'regexp', value:'[^ \n\t\r]+')
}
}
}
}
or by using the Gradle CopySpec's filter method:
task copyGradlefied(type:Copy) {
def regexp = new org.apache.tools.ant.types.RegularExpression()
regexp.pattern = '[^ \n\t\r]+'
from(projectDir) {
include 'input.txt'
filter(org.apache.tools.ant.filters.LineContainsRegExp, regexps:[regexp])
}
into "outputDir"
}