【问题标题】:Include a File If a Condition Is Met Inside Apache Ant fileset如果在 Apache Ant 文件集中满足条件,则包含文件
【发布时间】:2023-03-10 07:46:01
【问题描述】:

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

<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 上面部分中的注释文件。

【问题讨论】:

    标签: java ant conditional-statements


    【解决方案1】:

    如果你可以使用 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/

    【讨论】:

    • 谢谢,但是我不能使用任何外部库。
    【解决方案2】:

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

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

    --评论后编辑--

    include name/exclude name 之后的 if/unless attribute 检查给定值是否是在 ant 项目范围内设置(使用 if=".." 时)或未设置(使用 unless=".." 时)的属性- 它不检查特定值。
    &lt;include name="*.xml" unless="foo"/&gt;
    表示仅当 no 在您的项目中设置了名为 foo 的属性时,include 才有效
    &lt;include name="*.xml" if="foo"/&gt;
    意味着 include 只有在您的项目中设置了名为 foo 的属性时才处于活动状态


    对我来说很好用,使用 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&lt;local&gt; 需要使用当前范围内的属性的任务,在本例中为 &lt;sequential&gt;

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

    【讨论】:

    • 我仍然不确定includeif(或unless)如何与@{myattr} 的值进行实际比较。你能详细说明一下吗?
    • 我试过&lt;sequential&gt;&lt;condition property="pass"&gt;&lt;equals arg1="@{myattr}" arg2="xyz" /&gt;&lt;/condition&gt;&lt;fileset dir="C:/whatever" id="foobar"&gt;&lt;include name="*.bat" if="pass"/&gt;&lt;/fileset&gt;&lt;/sequential&gt; 但是,似乎每个@{myattr} 都包括*.bat(如果使用unless,即使@{myattr} 确实是@987654343,它也会排除所有@!
    • 所以,在您最近更新之前,我已经尝试过后续解决方案(我在上面的评论),但我发现如果我连续调用 pkgmacro 几个值,即abcdefxyzzwu,前两个调用包含文件。但是,xyz 之后的任何内容,在本例中为 zwu,请务必包含该文件!为什么?
    • 你的意思是使用多个 模式并行? 工作正常。也许您使用了错误的模式?
    • 评论打印错误的模式语法,所以请参阅答案中的编辑
    猜你喜欢
    • 2016-10-29
    • 1970-01-01
    • 1970-01-01
    • 2013-08-08
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多