2010-04-28 58 views
0

我有一個包含多個JavaScript文件的文件夾結構,每個文件的頂部需要一個標準的一段文字的元= // @包括「includes.js」生成使用Ant

每個文件夾都必須包含一個名爲include.js的文件,該文件在其目錄中爲每個文件都包含一個條目,並且在其父目錄中包含include文件的條目。

我試圖用螞蟻做到這一點,它不太好。到目前爲止,我擁有以下內容,它可以完成插入標題的工作,但不是沒有實際移動或複製文件。我聽到有人提到<replace>任務要做到這一點,但有點難住。

<?xml version="1.0" encoding="UTF-8"?> 

<project name="JavaContentAssist" default="start" basedir="."> 

    <taskdef resource="net/sf/antcontrib/antcontrib.properties"> 
     <classpath> 
     <pathelement location="C:/dr_workspaces/Maven Repository/.m2/repository/ant-contrib/ant-contrib/20020829/ant-contrib-20020829.jar"/> 
     </classpath> 
    </taskdef> 

    <target name="start"> 

     <foreach target="strip" param="file"> 

      <fileset dir="${basedir}"> 
       <include name="**/*.js"/> 
       <exclude name="**/includes.js"/> 
      </fileset> 

     </foreach> 

    </target> 

    <target name="strip"> 

     <move file="${file}" tofile="${a_location}" overwrite="true"> 

      <filterchain> 
       <striplinecomments> 
        <comment value="//@" /> 
       </striplinecomments> 
       <concatfilter prepend="${basedir}/header.txt"> 
       </concatfilter> 
      </filterchain> 
     </move> 


    </target> 

</project> 

至於在目錄中的包含文件的生成,我不知道從哪裏開始。如果有人能指出我正確的方向,我會很感激。

回答

1

這似乎滿足問題描述:

<project default="addincludes"> 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties"> 
    <classpath> 
     <pathelement location="ant-contrib/ant-contrib-1.0b3.jar"/> 
    </classpath> 
    </taskdef> 

    <target name="addincludes"> 
    <foreach target="perdir" param="dir"> 
     <path> 
     <dirset dir="src" includes="**"/> 
     </path> 
    </foreach> 
    </target> 

    <target name="perdir"> 
    <echo file="${dir}/includes.js">//@include "../includes.js"&#10;</echo> 
    <foreach target="perfile" param="file"> 
     <path> 
     <fileset dir="${dir}" includes="*.js" excludes="includes.js"/> 
     </path> 
    </foreach> 
    </target> 

    <target name="perfile"> 
    <basename property="basename" file="${file}"/> 
    <echo file="${dir}/includes.js" append="true">//@include "${basename}"&#10;</echo> 
    <move file="${file}" tofile="${file}.tmp" overwrite="true"> 
     <filterchain> 
     <striplinecomments> 
      <comment value="//@" /> 
     </striplinecomments> 
     <concatfilter prepend="header.txt"/> 
     </filterchain> 
    </move> 
    <move file="${file}.tmp" tofile="${file}" overwrite="true"/> 
    </target> 
</project>