如何搜索文件中的字符串用Ant(确保某些字符串不是在源文件中找到) [英] How to search for a string in files with Ant (make sure certain string isn't found in source files)

查看:509
本文介绍了如何搜索文件中的字符串用Ant(确保某些字符串不是在源文件中找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索的使用Ant我的源文件中的字符串。 (我想我的生成的情况下,某些字符串失败是我的源文件中找到)。

I'd like to search for a string within my source files with Ant. (I'd like my build to fail in case certain string is found within my source files).

所以,我应该能够递归搜索文件集中的某个字符串。

So, I should be able to recursively search for a certain string within a file set.

我已经发现,我可以使用的loadFile任务检查的whether字符串模式是文件中找到。但是,这似乎是工作和放大器;明智的只用单一的文件

I already found that I can use loadfile task to check whether a string pattern is found within one file. But that seems to be working & sensible only with a single file.

在另一方面,更换任务将提供递归搜索和替换。我想我可以做到这一点之前构建和一些会打破建立取代我的字符串,但我不知道是否有一些清晰的解决方案?

On the other hand, replace task would provide recursively search-and-replace. I guess I could do that before build and replace my string with something that would break the build but I wonder if there is some cleaner solution?

BR,Touko

推荐答案

您可能会考虑使用文件集选择做到这一点。选择器允许您根据内容,大小,可编辑性等方面来选择文件。您可以结合使用选择基于域名包括和排除,或patternsets。

You might consider using fileset selectors to do this. Selectors allow you to choose files based on content, size, editability and so on. You can combine selectors with name-based includes and excludes, or patternsets.

下面是一个例子。所述第二文件集从第一衍生,具有选择器,简单地对文件内容的匹配。对于更复杂的匹配还有就是 containsregexp 选择。其结果是只包含匹配字符串文件的文件集。与 resourcecount 条件是那么用于构建失败,除非该文件集是空的。

Below is an example. The second fileset is derived from the first, with a selector that simply matches on file content. For more sophisticated matching there is the containsregexp selector. The result is a fileset containing only files matching the string. A fail task with a resourcecount condition is then used to fail the build, unless that fileset is empty.

<property name="src.dir" value="src" />
<property name="search.string" value="BAD" />

<fileset id="existing" dir="${src.dir}">
    <patternset id="files">
        <!-- includes/excludes for your source here -->
    </patternset>
</fileset>

<fileset id="matches" dir="${src.dir}">
    <patternset refid="files" />
    <contains text="${search.string}" />
</fileset>

<fail message="Found '${search.string}' in one or more files in '${src.dir}'">
    <condition>
        <resourcecount when="greater" count="0" refid="matches" />
    </condition>
</fail>

旧的答案的):如果调整或重复文件集可能是有问题的,这里的一个例子的相对的简单的替代

(Old answer): If adjusting or reusing filesets might be problematic, here's an illustration of a relatively simple alternative.

我们的想法是,使文件的副本,
然后更换你想搜索的字符串
在复制的文件中的某些标志值。
这将更新任何匹配文件的最后修改时间。
UPTODATE 任务可以被用来寻找对于受影响的文件。
最后,除非没有文件匹配,可以 失败 构建。

The idea is to make a copy of the files, then replace the string you wish to search for with some flag value in the copied files. This will update the last modified time on any matching file. The uptodate task can then be used to look for affected files. Finally, unless no files matched, you can fail the build.

<property name="src.dir" value="src" />
<property name="work.dir" value="work" />
<property name="search.string" value="BAD" />

<delete dir="${work.dir}" />
<mkdir dir="${work.dir}" />
<fileset dir="${src.dir}" id="src.files">
    <include name="*.txt" />
</fileset>
<copy todir="${work.dir}" preservelastmodified="true">
    <fileset refid="src.files" />
</copy>
<fileset dir="${work.dir}" id="work.files">
    <include name="*.txt" />
</fileset>
<replaceregexp match="${search.string}"
               replace="FOUND_${search.string}">
    <fileset refid="work.files" />
</replaceregexp>
<uptodate property="files.clean">
    <srcfiles refid="work.files" />
    <regexpmapper from="(.*)" to="${basedir}/${src.dir}/\1" />
</uptodate>
<fail message="Found '${search.string}' in one or more files in dir '${src.dir}'"
      unless="files.clean" />

这篇关于如何搜索文件中的字符串用Ant(确保某些字符串不是在源文件中找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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