How can I skip blank lines when copying files using Gradle?
1 answer
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 spaces.
The matching regexp could 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 using the Gradle Filter CopySpec 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"
}
+4
source to share