How can I skip blank lines when copying files using Gradle?

I would like to copy some files to Gradle and the resulting files should not contain blank lines, i.e. empty lines are not copied. I guess it can be done with filter(...)

and maybe with TokenFilter

from ant. However, I'm not sure what the syntax looks like.

Thank.

+3
filter copy gradle


source to share


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







All Articles
Loading...
X
Show
Funny
Dev
Pics