【问题标题】:Macrodef attribute doesnt change its valueMacrodef 属性不会改变它的值
【发布时间】:2015-12-18 11:53:48
【问题描述】:

在我之前的post 中,我试图实现macrodef 以便用“类似函数”的任务替换重复的代码块。然后我遇到了一些问题。之后我减少了脚本,所以我可以试验我不习惯的任务。这是我编造的:

<project basedir="../../../" name="do-report" default="extract-common-paths">
    <xmlproperty keeproot="false" file="implementation/xml/ant/properties.xml"/>
    <!--    -->
    <taskdef resource="net/sf/antcontrib/antlib.xml">
        <classpath>
            <pathelement location="${infrastructure-base-dir}/apache-ant-1.9.6/lib/antcontrib.jar"/>
        </classpath>
    </taskdef>
    <!--    -->
    <macrodef name="get-common-path">
        <attribute name="common-path-property"/>
        <attribute name="file"/>
        <attribute name="file-base-dir"/>
        <sequential>
            <local name="file-dir-absolute-path"/>
            <dirname property="file-dir-absolute-path" file="@{file}"/>
            <property name="file-base-dir-absolute-path" location="@{file-base-dir}"/>
            <echo>MACRODEF FILE: ${file-dir-absolute-path}</echo>
            <echo>MACRODEF FILE-BASE-DIR: ${file-base-dir-absolute-path}</echo>
            <pathconvert property="@{common-path-property}" dirsep="/">
                <path location="${file-dir-absolute-path}"/>
                <map from="${file-base-dir-absolute-path}/" to=""/>
            </pathconvert>
        </sequential>
    </macrodef>
    <!--    -->
    <target name="clean">
        <delete dir="${dita-odt.path.odt-unzipped-base-dir}" includeemptydirs="true" failonerror="no"/>
        <delete dir="examples/intermediate/odt-files" includeemptydirs="true" failonerror="no"/>
    </target>
    <!--    -->
    <target name="unzip-writing-odt-file" depends="clean">
        <unzip src="${dita-odt.path.writing-odt}" dest="${dita-odt.path.writing-odt-unzipped}"/>
    </target>
    <!--    -->
    <target name="extract-common-paths" depends="unzip-writing-odt-file">
        <for param="file">
            <path>
                <fileset dir="${dita-odt.path.text-xml-base-dir}">
                    <include name="**/content.xml"/>
                </fileset>
            </path>
            <sequential>
                <get-common-path common-path-property="common-path" file="@{file}" file-base-dir="${dita-odt.path.text-xml-base-dir}"/>
                <echo>THIS IS THE PATH: ${common-path}</echo>
            </sequential>
        </for>
    </target>
</project>

FOR 任务遍历不同目录中的两个文件。实际上FOR 按预期工作。它应该通过${file}macrodef属性common-path-property先设置为第一个文件的转换路径(可以)。但是当第二个文件传递给get-common-path/@file 时,common-path-property 属性不会改变它的值,我再次收到这个:

[echo] 宏指令文件:C:\Users\rmrd001\git\xslt-framework\examples\text\t1\t1.1

[echo] MACRODEF FILE-BASE-DIR: C:\Users\rmrd001\git\xslt-framework\examples\text

[echo] 这是路径:t1/t1.1

[echo] 宏指令文件:C:\Users\rmrd001\git\xslt-framework\examples\text\t2\t2.1

[echo] MACRODEF FILE-BASE-DIR: C:\Users\rmrd001\git\xslt-framework\examples\text

[echo] 这是路径:t1/t1.1

我希望收到这个:

[echo] 宏指令文件:C:\Users\rmrd001\git\xslt-framework\examples\text\t1\t1.1

[echo] MACRODEF FILE-BASE-DIR: C:\Users\rmrd001\git\xslt-framework\examples\text

[echo] 这是路径:t1/t1.1

[echo] 宏指令文件:C:\Users\rmrd001\git\xslt-framework\examples\text\t2\t2.1

[echo] MACRODEF FILE-BASE-DIR: C:\Users\rmrd001\git\xslt-framework\examples\text

[echo] 这是路径:\t2\t2.1

我希望你明白我想要做什么。提前谢谢!

【问题讨论】:

    标签: java xml xslt ant ant-contrib


    【解决方案1】:

    尝试打印宏定义属性值而不是属性值。

    • @{attribute} - 可以随着宏定义的每次执行而改变
    • ${property} - 始终只在 Ant 中设置和修复一次,即使在宏定义中也是如此
      • 除非前面有 &lt;local&gt; 任务,在这种情况下,在该块的范围内使用本地设置的属性值。

    你想要的可能是由 Ant Local task 提供的。

    将本地属性添加到当前范围。属性范围存在于 Apache Ant 的各种“块”级别。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-03
      • 2014-06-07
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 2022-11-27
      • 2013-06-25
      相关资源
      最近更新 更多