2009-10-12 70 views
1

如何將目錄列表轉換爲.jar文件列表? 似乎使用Copy Task和Mappers是這樣做的正確方法,但我沒有找到任何嵌套元素從目錄中創建一個jar文件。如何使用Ant將目錄轉換爲.jar文件

當然,目錄列表將被計算出來,而不是在螞蟻腳本中進行硬編碼。

例如,foo目錄包含bar1,bar2和bar3目錄。該腳本將從bar1目錄創建bar1.jar,從bar2目錄創建bar2.jar並從bar3目錄創建bar3.jar目錄 腳本將從新添加的bar4目錄創建bar4.jar,而不會對腳本進行任何更改(無硬編碼列表)。提前對您有所幫助

回答

2

可以使用subant任務來完成。例如:

<project name="jars" default="jar" basedir="."> 
    <property name="dir.build" value="${basedir}/build"/> 
    <property name="dir.root" value="${basedir}/foo"/> 

    <target name="init"> 
     <tstamp/> 
     <mkdir dir="${dir.build}"/> 
    </target> 

    <target name="jar" depends="init"> 
     <!-- ${ant.file} is the name of the current build file --> 
     <subant genericantfile="${ant.file}" target="do-jar"> 

      <!-- Pass the needed properties to the subant call. You could also use 
       the inheritall attribute on the subant element above to pass all 
       properties. --> 
      <propertyset> 
       <propertyref name="dir.build"/> 
      </propertyset> 

      <!-- subant will call the "do-jar" target for every directory in the 
       ${dir.root} directory, making the subdirectory the basedir. --> 
      <dirset dir="${dir.root}" includes="*"/> 
     </subant> 
    </target> 

    <target name="do-jar"> 
     <!-- Get the basename of the basedir (bar1, bar2, etc.) --> 
     <basename file="${basedir}" property="jarname"/> 

     <jar jarfile="${dir.build}/${jarname}.jar" basedir="${basedir}"/> 
    </target> 
</project> 
0

感謝你可以使用適當的Jar task,與foreach螞蟻的contrib支線任務一起。

樣本:

<foreach list="${hmProjectsList}" delimiter="/," 
     target="cvsCheckOut" param="hmProjectDir" inheritall="true" /> 
+0

Jar在輸入單個輸出文件中佔用多個文件。我需要多個輸出文件。如果你知道如何做到這一點,你可以舉個例子嗎? – dilig0 2009-10-12 12:33:05

+0

對不起。我改編了我的答案。 – KLE 2009-10-12 12:55:17

+0

AFAIK jar任務可以將一個basedir作爲輸入並將其轉換爲目標文件 - 這看起來就像您需要的一樣。 – 2009-10-13 06:39:05

相關問題