How to skip empty lines when copying files with Gradle?

痴心易碎 提交于 2019-12-10 15:38:43

问题


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.


回答1:


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"
}


来源:https://stackoverflow.com/questions/14952955/how-to-skip-empty-lines-when-copying-files-with-gradle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!