2015-12-17 38 views
0

我是新來的螞蟻,所以我無法找到一種方法使我的buld文件更加優雅。我相信有一種方法可以將重複代碼塊替換爲我的版本。因此,這裏是構建文件:如何開發替代重複代碼塊的方法

<project basedir="../../../" name="do-report" default="zip-all"> 
    <xmlproperty keeproot="false" file="implementation/xml/ant/properties.xml"/> 
    <!-- --> 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties"> 
     <classpath> 
      <pathelement location="${infrastructure-base-dir}/apache-ant-1.9.6/lib/ant-contrib-0.3.jar"/> 
     </classpath> 
    </taskdef> 
    <!-- --> 
    <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"> 
     <foreach target="copy-text-path" param="file"> 
      <path> 
       <fileset dir="${dita-odt.path.text-xml-base-dir}"> 
        <include name="**/content.xml"/> 
       </fileset> 
      </path> 
     </foreach> 
    </target> 
    <!-- --> 
    <target name="copy-text-path" description="copy text-xml path relative to text-xml-base-dir"> 
     <dirname property="text-absolute-dir-path" file="${file}"/> 
     <property name="absolute-path-text-base-dir" location="${dita-odt.path.text-xml-base-dir}"/> 
     <pathconvert property="common-path" dirsep="/"> 
      <path location="${text-absolute-dir-path}"/> 
      <map from="${absolute-path-text-base-dir}/" to=""/> 
     </pathconvert> 
     <antcall target="copy-writing-unzipped"> 
      <param name="common-path" value="${common-path}"/> 
     </antcall> 
    </target> 
    <!-- --> 
    <target name="copy-writing-unzipped"> 
     <echo>${common-path}</echo> 
     <copy todir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}"> 
      <fileset dir="${dita-odt.path.writing-odt-unzipped}"> 
       <include name="**/*"/> 
      </fileset> 
     </copy> 
    </target> 
    <!-- --> 
    <target name="transform-all" depends="extract-common-paths"> 
     <foreach target="transform" param="file"> 
      <path> 
       <fileset dir="${dita-odt.path.text-xml-base-dir}"> 
        <include name="**/content.xml"/> 
       </fileset> 
      </path> 
     </foreach> 
    </target> 
    <!-- --> 
    <target name="transform"> 
     <basename property="file-base-name" file="${file}"/> 
     <dirname property="file-dir-absolute-path" file="${file}"/> 
     <property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/> 
     <pathconvert property="common-path" dirsep="/"> 
      <path location="${file-dir-absolute-path}"/> 
      <map from="${text-xml-base-dir-absolute-path}/" to=""/> 
     </pathconvert> 
     <!--Substitutes backslashes with forword slashes. Basedir is a reserved property that returns absolute path with separator symbols of the current OS.--> 
     <pathconvert dirsep="/" property="base-dir-unix"> 
      <path location="${basedir}"/> 
     </pathconvert> 
     <echo>TRANSFORM TO: ${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}</echo> 
     <xslt in="${file}" out="${dita-odt.path.odt-unzipped-base-dir}/${common-path}/${file-base-name}" style="${dita-odt.path.text-odt-xsl}" extension=".xml" force="true"> 
      <param name="dir-path-styles-xml" expression="${dita-odt.path.odt-unzipped-base-dir}/${common-path}"/> 
      <param name="project-base-dir-absolute-path" expression="${base-dir-unix}"/> 
      <classpath location="${infrastructure-base-dir}/${dita-odt.text-odt-xsl.processor}"/> 
     </xslt> 
    </target> 
    <!-- --> 
    <target name="zip-all" depends="transform-all" description="Turns all unzipped text folders into ODT files"> 
     <foreach target="zip-odt" param="file"> 
      <path> 
       <fileset dir="${dita-odt.path.odt-unzipped-base-dir}" includes="**/content.xml" excludes="writing/**"/> 
      </path> 
     </foreach> 
    </target> 
    <!-- --> 
    <target name="zip-odt"> 
     <basename property="file-base-name" file="${file}"/> 
     <dirname property="file-dir-absolute-path" file="${file}"/> 
     <!--This property will be used to provided name for the produced ODT file. The document will have the same name as the folder that contains it.--> 
     <basename property="odt-doc-name" file="${file-dir-absolute-path}.odt"/> 
     <property name="odt-unzipped-base-dir-absolute-path" location="${dita-odt.path.odt-unzipped-base-dir}"/> 
     <pathconvert property="common-path" dirsep="/"> 
      <path location="${file-dir-absolute-path}"/> 
      <map from="${odt-unzipped-base-dir-absolute-path}/" to=""/> 
     </pathconvert> 
     <echo>COMMON PATH: ${common-path}</echo> 
     <zip destfile="examples/intermediate/odt-files/${common-path}/${odt-doc-name}" basedir="${dita-odt.path.odt-unzipped-base-dir}/${common-path}" update="true"/> 
    </target> 
    <!-- --> 
</project> 

所以這部分腳本做幾乎相同,但幾乎所有的項目中的目標共享:

 <dirname property="file-dir-absolute-path" file="${file}"/> 
     <property name="text-xml-base-dir-absolute-path" location="${dita-odt.path.text-xml-base-dir}"/> 
     <pathconvert property="common-path" dirsep="/"> 
      <path location="${file-dir-absolute-path}"/> 
      <map from="${text-xml-base-dir-absolute-path}/" to=""/> 
     </pathconvert> 

這部分不做任何事,但獲得一部分路徑。例如,如果${file}代表/folder/subfolder1/subfolder2,則在/folder之後採取路徑,即subfolder1/subfolder2並將其分配給屬性。我這種情況下,該屬性被命名爲common-path,其中包含所有目標的相同路徑。我檢查了MacroDef Task,但據我瞭解,它不返回,只接受一些屬性形式的參數。無論如何,任何幫助將不勝感激。

回答

1

您在考慮<macrodef>以降低重複性代碼時處於正確的軌道。

儘管<macrodef>確實沒有返回任何東西,但<macrodef>可以被賦予要設置的屬性的名稱。例如...

<macrodef name="my-hello"> 
    <attribute name="person"/> 
    <attribute name="output-property"/> 
    <sequential> 
     <property name="@{output-property}" value="Hello, @{person}!"/> 
    </sequential> 
</macrodef> 

<my-hello person="Riko" output-property="say-hi-to-riko"/> 
<echo>my-hello said: ${say-hi-to-riko}</echo> 

... ...輸出

[echo] my-hello said: Hello, Riko! 

在這個例子中,<my-hello>來電告訴macrodef到 「迴歸」,其結果在say-hi-to-riko財產。

認識到這一點,在你的腳本幾個<target> S的可轉換爲<macrodef> s表示設置屬性...

<project name="ant-macrodef-pathconvert" default="extract-common-paths"> 
    <taskdef resource="net/sf/antcontrib/antlib.xml" /> 
    <property name="dita-odt.path.text-xml-base-dir" value="C:\temp\dita-odt"/> 
    <macrodef name="my-pathconvert"> 
     <attribute name="file"/> 
     <attribute name="common-path-property"/> 
     <sequential> 
      <!-- <local> allows multiple calls to a macrodef. --> 
      <local name="file-dir-absolute-path"/> 
      <echo>In my-pathconvert for @{file}</echo> 
      <dirname property="file-dir-absolute-path" file="@{file}"/> 
      <property name="text-xml-base-dir-absolute-path" 
       location="${dita-odt.path.text-xml-base-dir}"/> 
      <pathconvert property="@{common-path-property}" dirsep="/"> 
       <path location="${file-dir-absolute-path}"/> 
       <map from="${file-dir-absolute-path}/" to=""/> 
      </pathconvert> 
     </sequential> 
    </macrodef> 
    <macrodef name="copy-text-path" 
     description="copy text-xml path relative to text-xml-base-dir"> 
     <attribute name="file"/> 
     <sequential> 
      <local name="common-path"/> 
      <echo>In copy-text-path for @{file}</echo> 
      <my-pathconvert file="@{file}" common-path-property="common-path"/> 
      <copy-writing-unzipped common-path="${common-path}"/> 
     </sequential> 
    </macrodef> 
    <macrodef name="copy-writing-unzipped"> 
     <attribute name="common-path"/> 
     <sequential> 
      <echo>In copy-writing-unzipped for @{common-path}</echo> 
      <echo>copy task goes here.</echo> 
     </sequential> 
    </macrodef> 
    <target name="extract-common-paths"> 
     <for param="file"> 
      <path> 
       <fileset dir="${dita-odt.path.text-xml-base-dir}"> 
        <include name="**/content.xml"/> 
       </fileset> 
      </path> 
      <sequential> 
       <copy-text-path file="@{file}"/> 
      </sequential> 
     </for> 
    </target> 
</project> 

一般情況下,最好還是更喜歡直接調用<target>在叫<macrodef>秒。在上例中,<foreach>被替換爲<for>,因爲<for>讓我們撥打<macrodef> s。

輸出

[echo] In copy-text-path for C:\temp\dita-odt\dir1\content.xml 
[echo] In my-pathconvert for C:\temp\dita-odt\dir1\content.xml 
[echo] In copy-writing-unzipped for C:/temp/dita-odt/dir1 
[echo] copy task goes here. 
[echo] In copy-text-path for C:\temp\dita-odt\dir2\content.xml 
[echo] In my-pathconvert for C:\temp\dita-odt\dir2\content.xml 
[echo] In copy-writing-unzipped for C:/temp/dita-odt/dir2 
[echo] copy task goes here. 
+0

感謝你的回覆。不幸的是,我遇到了實施macrodef策略的一些問題。對我來說,它似乎是屬性元素(在macrodef中)作爲一個常量。一旦設置,永不改變。我減少了代碼,所以可以將您的技術調整到我的構建文件。這裏是我試驗的例子:[build.xml](https://github.com/RadoslavMarinov/stack-show/blob/f0e93bf509518dc570506610cf7740997362ef71/xml/new-text-odt.xml) 雖然我有兩個文件傳遞** FOR **任務'$ {common-path}'總是相同的。 – Hairi

+0

@Riko在你連接的build.xml文件中,在之前插入' 。每當''任務循環時,''將清除'common-path'屬性。 –