如何在使用Gradle复制文件时跳过空行?

编程入门 行业动态 更新时间:2024-10-25 13:29:21
本文介绍了如何在使用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复制文件时跳过空行?

本文发布于:2023-05-25 10:56:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/226644.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空行   跳过   文件   如何在   Gradle

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!