【问题标题】:Fileset ant, include from external source?文件集蚂蚁,包括来自外部来源?
【发布时间】:2012-01-02 16:16:25
【问题描述】:

当文件集 ant 任务从外部提供一组文件时,例如 java 程序或作为 xml 文件,我如何才能在它们中包含一组文件?

通常我们使用包含和排除正则表达式进行这些自定义,

<fileset ...>
<include name="**/*Test.java" />
<exclude name="**/*.txt" />

但我对要包含的文件一无所知,对它们施加条件,因为在这种情况下,我们已经知道我们应该包含以 test 结尾的 java 文件并忽略 txt 文件..

要包含和排除的文件将从外部来源(如 java 程序或 xml 格式)提供。在这种情况下,我如何将它们包含在文件集中?

【问题讨论】:

    标签: ant


    【解决方案1】:

    想出了一个方法来做到这一点..在 ant 脚本中包含一个 groovy 任务来完成这个..假设我有一个像这样的input.xml

    <include>       
        <targetclass name="com.samples.ClassToBeIncluded1" />               
        <targetclass name="com.samples.ClassToBeIncluded2" />
        <targetclass name="com.samples.ClassToBeIncluded3" />               
        <targetclass name="com.samples.ClassToBeIncluded4" />
    </include>
    

    提供有关要包含的类的详细信息。我使用简单的 groovy 解析器解析文件,并在 ant 中配置一个新属性,指定要包含的类集。

    新属性是:

    <property name="instrumentedclasses" value=""/>
    

    读取外部输入并设置属性的 groovy 脚本是:

    <groovy>
        def xmlParser = new XmlParser()
        def includeStr = ""
        def eachFile  = new File('input.xml')
        def includeRootNode = xmlParser.parse(eachFile)         
        includeRootNode.targetclass.each{ targetClassNode ->
            def className = targetClassNode.attribute("name")
            className = className.replace(".","/")
            includeStr = includeStr +className+".class "
    
        }
        properties["instrumentedclasses"] = includeStr
    
    </groovy>
    

    现在新设置的属性被下面的 ant 脚本用来包含必要的类:

    <fileset dir="${classes.dir}" includes="${instrumentedclasses}" />
    

    【讨论】:

      猜你喜欢
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多