2012-03-21 107 views
1

我有這個ant任務,創建一個webstart jnlp文件。螞蟻replacetoken與多個jar文件名

它取代像@標題和@這樣的令牌從一個模板文件:

<?xml version="1.0" encoding="UTF-8"?> 
<jnlp spec="1.0+" codebase="@[email protected]"> 
<information> 
    <title>@[email protected]</title> 
</information> 
<resources> 
    @[email protected] 
</resources> 
<application-desc main-class="@[email protected]"/> 
</jnlp> 

的問題是,我在我的lib /目錄許多罐子: Log4J.jar,xpp.jar,資源。罐... 和1個罐子令牌。

如何將jars @令牌替換爲jars文件名? 使輸出變爲:

<resources> 
    <jar href="log4J.jar"/> 
    <jar href="xpp.jar"/> 
    <jar href="resources.jar"/> 
</resources> 

這是我的螞蟻項目的一部分:

<target name="webstart" description="Deploy as jnlp webstart"> 
    <copy file="template.jnlp" tofile="test.jnlp"> 
     <filterchain> 
      <replacetokens> 
       <token key="codebase" value="myCodebase" /> 
       <token key="title" value="myTitle" /> 
       <token key="jars" value="jar href="xxx.jar" /> 
      </replacetokens> 
     </filterchain> 
    </copy> 
</target> 
<project/> 

回答

0

我設法與螞蟻的contrib來實現這一(感謝乍得Nouis在性能與CDATA提示):

<!-- Tricky part is XML content here CDATA Elements are needed, this is the first part--> 
<property name="pre"><![CDATA[ 
    <jar href="]]></property> 

<!-- Tricky part is XML content here CDATA Elements are needed, this is the last part--> 
<property name="after"><![CDATA["/>]]></property> 

<!-- this will be our temp file--> 
<delete file="temp.txt" failonerror="false"/> 

<!-- for task from ant-contrib--> 
<for param="file"> 
    <fileset dir="dist" includes="*.jar"/> 
    <sequential> 
    <!-- write it into a file, using var/properties did not work--> 
    <echo file="temp.txt" append="true">${pre}@{file}${after}</echo> 
    </sequential> 
</for> 

<!-- load file content in property--> 
<loadfile property="xml.data" srcfile="temp.txt"/> 

<!-- finish--> 
    <copy file="template.jnlp" tofile="test.jnlp" overwrite="true"> 
     <filterchain> 
      <replacetokens> 
       <token key="codebase" value="myCodebase" /> 
       <token key="title" value="myTitle" /> 
       <token key="jars" value="${xml.data}" /> 
      </replacetokens> 
     </filterchain> 
    </copy> 

鏈接:

0

從我個人理解,你想更換@jars帶有文字XML的@標記。看看這是你追求的:

<target name="run"> 
    <property name="xml.data"><![CDATA[ 
     <jar href="log4J.jar"/> 
     <jar href="xpp.jar"/> 
     <jar href="resources.jar"/> 
    ]]></property> 

    <copy file="template.jnlp" tofile="test.jnlp"> 
     <filterchain> 
      <replacetokens> 
       <token key="codebase" value="myCodebase" /> 
       <token key="title" value="myTitle" /> 
       <token key="jars" value="${xml.data}" /> 
      </replacetokens> 
     </filterchain> 
    </copy> 
</target> 
+0

這似乎是方法的一部分,我認爲他需要的財產內容進行分析動態根據文件集。 – oers 2012-03-21 13:53:33

+0

是的,動態地,我發現pathconvert在這裏[鏈接](http://stackoverflow.com/questions/1978866/is-it-possible-to-replace-text-in-properties-in-ants-build-xml) 並能夠替換 <來自= 「$ lib.dir」 來= replacestring 「< A HREF = "」 但我將如何替換元素結束「/>」 – Stefan 2012-03-21 14:46:30