如果在Apache Ant文件集中遇到条件,请包含文件

时间:2014-09-08 21:58:54

标签: java ant conditional-statements

根据Apache Ant(1.8.3)中的条件,是否有办法includeexclude某些文件?例如,我有一个macrodef来获取属性。如果属性的值与include匹配,我想xyz某些文件:

<macrodef name="pkgmacro">
    <attribute name="myattr" />
    <sequential>
         <zip destfile="${dist}/@{myattr}.war">
             <fileset dir="${dist}/webapp" >     
                 <include name="**/@{myattr}/**" />
                 <exclude name="WEB-INF/config/**" /> 
                 <!-- if @{myattr} = "xyz", then
                 <include name="PATH/TO/file.xml" />
                 -->                    
             </fileset>
             <zipfileset dir="${ear}/@{myattr}/WEB-INF/" includes="*.xml" prefix="WEB-INF/" />
         </zip>
    </sequential>
</macrodef>

例如,如果myattr值为xyz,我想include上面部分的注释文件。

2 个答案:

答案 0 :(得分:3)

文件集可以使用nested include/exlucde patternsets if / unless 属性。设置或不设置命名属性时包含/排除模式,因此不需要任何ant插件。
一些片段:

<project>

 <!-- property that triggers your include/exclude
      maybe set via condition in some other target .. -->
 <property name="foo" value="bar"/>

 <macrodef name="pkgmacro">
  <attribute name="myattr" />
   <sequential>
    <fileset dir="C:/whatever" id="foobar">
     <!-- alternatively
     <include name="*.bat" unless="@{myattr}"/>
     -->
     <include name="*.bat" if="@{myattr}"/>
    </fileset>
    <!-- print fileset contents -->
    <echo>${toString:foobar}</echo>
   </sequential>
 </macrodef>

 <pkgmacro myattr="foo"/>

</project>

- 评论后编辑 -

if/unless attribute之后include name/exclude name检查给定值是否是设置的属性(当使用if =&#34; ..&#34;)或未设置时(使用时除非= &#34; ..&#34;)在蚂蚁项目范围内 - 它不会检查特定值。
<include name="*.xml" unless="foo"/>
表示包含仅在项目中设置了名为foo的 no 属性时才有效 <include name="*.xml" if="foo"/>
表示只有在项目中设置了名为foo的属性

时才包含include


对我来说很好,使用Ant 1.7.1,现在没有Ant 1.8.x:

<project>

 <echo>$${ant.version} => ${ant.version}</echo>

 <macrodef name="pkgmacro">
   <attribute name="myattr"/>
   <sequential>
     <condition property="pass">
      <equals arg1="@{myattr}" arg2="xyz" />
     </condition>
    <fileset dir="C:/whatever" id="foobar">
     <include name="*.bat" if="pass" />
    </fileset>
    <echo>${toString:foobar}</echo>
   </sequential>   
 </macrodef>

<pkgmacro myattr="xyz"/>

</project>

输出:

[echo] ${ant.version} => Apache Ant version 1.7.1 compiled on June 27 2008
[echo] switchant.bat;foobar.bat;foo.bat

- 评论后编辑 -
使用几个包含模式的工作正常:

...
<fileset dir="C:/whatever" id="foobar">
 <include name="*.xml" if="pass" />
 <include name="*.bat" if="pass"/>
 <include name="**/*.txt" if="pass"/>
</fileset>
...

也许您正在使用错误的模式?

- 评论后编辑 -

使用当前范围内的属性所需的

Here is the reference<local>任务,在本例中为<sequential>

 <macrodef name="pkgmacro">
   <attribute name="myattr"/>
   <sequential>
     <!-- make property pass mutable -->
     <local name="pass"/>
     <condition property="pass">
      <equals arg1="@{myattr}" arg2="xyz" />
     </condition>
    <fileset dir="C:/whatever" id="foobar">
     <include name="*.xml" if="pass" />
      <include name="*.bat" if="pass" />
      <include name="**/*.txt" if="pass" />
    </fileset>
    <!-- print value of property pass and fileset contents -->
    <echo>$${pass} = ${pass}${line.separator}${toString:foobar}</echo>
   </sequential>   
 </macrodef>

答案 1 :(得分:2)

如果你可以使用ant-contrib,那么试试这个:

<contrib:if>
    <equals arg1="${myattr}" arg2="xyz" />
    <then>
        <include name="PATH/TO/file.xml" />
    </then>
</contrib:if>    

如果你不能使用ant-contrib,试试这个:

http://jaysonlorenzen.wordpress.com/2010/03/10/apache-ant-if-else-condition-without-ant-contrib/