2012-03-29 10 views
7

का उपयोग कर एक एक्सएमएल फ़ाइल में संपत्ति को बदलें, मैं एएनटी स्क्रिप्ट का उपयोग कर build.xml फ़ाइल में संस्करण संख्या को प्रतिस्थापित करने का प्रयास कर रहा हूं।एएनटी

मैंने विभिन्न दृष्टिकोणों की कोशिश की है, जवाब के लिए StackOverflow की खोज की और फिर से खोज की लेकिन सटीक क्वेरी नहीं मिल सका। अब के रूप में यू देख सकते हैं संस्करण कल तिथि है

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

<project name="feature" default="main" basedir="."> 
<target name="init">  
    <property name="Version" value="1.0.0.20120327"/> 
</target> 

    <target name="main" depends="init"> 
    <description>Main target</description> 
</target> 
</project> 

:

इसलिए यहाँ मेरी xml फ़ाइल है। मुझे इसे वर्तमान तारीख से बदलना होगा।

यहाँ मैं क्या कोशिश की है है:

<target name="replace"> 
    <tstamp > 
    <format property="touch.time" pattern="yyyyMMdd"/> 
    </tstamp> 

<property name="Feature.dir" location="../feature" /> 

<!--Didnt Work-->  
<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml" 
filtering="yes" overwrite="yes"> 
<filterset> 
    <filter token="Version" value="1.0.0.${touch.time}"/> 
</filterset> 
    </copy> 

    <!--Didnt work 

    <replacetoken><![CDATA[<property name="Version" value=""/>]]> 
    </replacetoken> 
    <replacevalue><![CDATA[<property name="Version"value="1.0.0.${touchtime}" />]]> 
    </replacevalue> 

    --> 


<!-- Didnt work 
    <copy file="${Feature.dir}/build.xml" tofile="${Feature.dir}/build1.xml" > 
     <filterchain> 
     <tokenfilter> 
       <replaceregex pattern="^[ \t]*Version[ \t]*=.*$" 
           replace="Version=1.0.0.${touch.time}"/> 
     </tokenfilter> 
      </filterchain> 
</copy> 
--> 
</target> 

उत्तर

9

मैं एक filterchain अंदर replaceregex का प्रयोग करेंगे।

उदाहरण के लिए:

<copy file="${Feature.dir}\build.xml" tofile="${Feature.dir}\build1.xml"  
    filtering="yes" overwrite="yes"> 
    <filterchain> 
     <tokenfilter> 
      <replaceregex pattern="1.0.0.[0-9.]*" replace="1.0.0.${touch.time}"/> 
     </tokenfilter> 
    </filterchain> 
</copy> 

आप, फ़ाइल को बदलने के लिए एक अस्थायी फ़ाइल के लिए नकल करने के लिए स्वतंत्र लग रहा है और इसे वापस ले जाना चाहते हैं।

<tempfile property="build.temp.file.name"/> 
<copy file="${Feature.dir}\build.xml" tofile="${build.temp.file.name}" ... /> 
<move file="${build.temp.file.name}" tofile="${Feature.dir}\build.xml" /> 
+0

काम किया धन्यवाद। लेकिन सिर्फ एक सवाल है। क्या होगा यदि मैं एक ही फाइल में यह परिवर्तन करना चाहता हूं? मैंने कोशिश की: <कॉपी फ़ाइल = "$ {Feature.dir} \ build.xml" tofile = "$ {Feature.dir} \ build.xml" फ़िल्टरिंग = "हाँ" ओवरराइट = "हाँ"> नहीं काम – sloggers1894

+0

कोई चिंता नहीं। मेरा संपादित उत्तर देखें। – Synesso

संबंधित मुद्दे