如何在使用Gradle复制文件时跳过空行? [英] How to skip empty lines when copying files with Gradle?

查看:232
本文介绍了如何在使用Gradle复制文件时跳过空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gradle中复制一些文件,生成的文件不应该包含任何空白行,即空白行不会被复制。我假设可以使用 filter(...),也许使用来自ant的 TokenFilter 来完成。

解决方案

/ div>

Gradle使用 Ant进行过滤,因为它的强大实施。例如,您可以使用 LineContainsRegExp Ant过滤器可过滤出任何只有空格或空格的行。



适当的正则表达式可以是 [^ \\\
\t\ r] +



您可以直接从Gradle使用Ant,如下所示:

  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:'[^ \\\
\t\r] +')
}
}
}
}

Gradle CopySpec 的过滤方法:

 任务copyGradlefied(type:Copy){
def regexp = new org.apache.tools.ant.types.RegularExpression )
regexp.pattern ='[^ \\\
\t\r] +'

from(projectDir){
include'input.txt'
过滤器(org.apache.tools.ant.filters.LineContainsRegExp,regexps:[regexp])
}
intooutputDir
}


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

这篇关于如何在使用Gradle复制文件时跳过空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆