【问题标题】:How to over-write the property in Ant?如何覆盖 Ant 中的属性?
【发布时间】:2009-12-08 12:44:05
【问题描述】:

有没有办法为 Ant property 任务重新分配值?或者是否有其他任务可用于此目的?

【问题讨论】:

    标签: ant properties task


    【解决方案1】:

    ant-contrib'sVariable任务可以这样做:

    <property name="x" value="6"/>
    <echo>${x}</echo>   <!-- will print 6 -->
    <var name="x" unset="true"/>
    <property name="x" value="12"/>
    <echo>${x}</echo>   <!-- will print 12 -->
    

    不推荐,但是,如果您的 Ant 脚本的某些部分假定不可变的属性值,而其他部分打破了这个假设,这可能会导致奇怪的副作用。

    【讨论】:

    • var 任务对于“局部变量”特别好,例如在 for 循环中(也是优秀 ant-contrib 的一项任务)。不过,一个缺点是 var 任务不支持“位置”属性。
    【解决方案2】:

    为了公正起见,有一个 hack 允许在没有任何额外库的情况下更改 ant 的不可变属性(从 java 6 开始):

    <scriptdef name="propertyreset" language="javascript"
        description="Allows to assign @{property} new value">
        <attribute name="name"/>
        <attribute name="value"/>
            project.setProperty(attributes.get("name"), attributes.get("value"));
    </scriptdef>
    

    用法:

        <property name="x" value="10"/>
        <propertyreset name="x" value="11"/>
        <echo>${x}</echo>   <!-- will print 11 -->
    

    正如其他人所提到的,在所有规范方法都被证明不适合之后,应谨慎使用。

    【讨论】:

      【解决方案3】:

      属性在 ant 中是不可变的。

      你可能对ant-contribvar task感兴趣。

      <var name="my_var" value="${my_property}" />
      
      <echo>Addressed in the same way: ${my_var} and ${my_property}</echo>
      

      【讨论】:

        【解决方案4】:

        根据你想如何使用修改后的属性,你可以使用macrodefs。

        例如,不要写如下:

        <target name="foo">
           <echo message="${my_property}"/>
        </target>
        

        并且无法通过另一条消息致电ant foo,您可以写:

        <macrodef name="myecho">
            <attribute name="msg"/>
            <sequential>
                <echo message="@{msg}"/>
            </sequential>
        </macrodef>
        
        <target name="foo">
           <myecho msg="${my_property}"/>
           <property name="my_property2" value="..."/>
           <myecho msg="${my_property2}"/>
        </target>
        

        【讨论】:

          【解决方案5】:

          从 Ant 1.8 开始,您可以使用“本地”任务来更改目标中属性的值。请注意,这不会更改具有相同名称的全局属性的值,但这是解决某些问题的一种方法。

          http://ant.apache.org/manual/Tasks/local.html

          【讨论】:

            【解决方案6】:

            您不能在 Ant 中更改属性的值。

            如果您有一些 Ant 任务想要重复运行并传入不同的值,我推荐 macrodef task,因为您可以重复运行相同的宏并传入不同的属性。

            例如:

            <macrodef name="copythings">
              <attribute name="todir"/>
              <sequential>
                <copy todir="@{todir}">
                  <fileset dir="${src}">
                    <exclude name='**/*svn' />
                  </fileset>
                </copy>
              </sequential>
            </macrodef>
            
            <copythings todir="/path/to/target1"/>
            <copythings todir="/path/to/target2"/>
            

            请注意,${property} 用于引用属性,@{attribute} 用于引用传递给&lt;macrodef&gt; 任务的属性。

            【讨论】:

              【解决方案7】:

              属性在 ant 中是不可变的。但这并不像看起来那么可怕。有一整类编程语言,其中(大多数)变量是常量,但它们可以完成工作——这被称为“函数式编程”。

              您可以通过从旧属性派生新的、已更改的属性或在使用subantantcall 任务调用任务时更改参数来“​​更改”不同任务使用的值。如果您有创造力,通常可以找到解决问题的方法。

              【讨论】:

              • 举个例子,多解释,很有帮助。
              【解决方案8】:

              这是一个使用 local 和 basename 命令的示例。 Var-unset 对我不起作用。

              <for param="db-patches">
                     <path>
                          <fileset dir="${undeployed-files}" includes="**/ddl*.zip"/>
                      </path>
                      <sequential>
                            <local name="inpfile" />
                             <basename property="inpfile" file="@{db-patches}" suffix=".zip" />
                             <!-- unzip the patch  -->
                             <unzip src="${undeployed-files}/${inpfile}.zip" 
                                 dest="${unzipped-patches}/${inpfile}" />
                         <move file="${undeployed-files}/${inpfile}.zip" tofile="${deployed-files}/${inpfile}.zip"/>
                      </sequential>   </for>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2011-09-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多