2009-06-03 14 views
31

में एक सबस्ट्रिंग को खींचने के लिए कैसे एक चींटी संपत्ति से एक सबस्ट्रिंग खींचने और उस जगह को अपनी संपत्ति में रखने के लिए कोई तरीका है?चींटी

+0

क्या आप जो भी करने की कोशिश कर रहे हैं उसके बारे में अधिक विशिष्ट हो सकते हैं? किसी संपत्ति को उस मूल्य को पकड़ने के लिए और अधिक समझ क्यों नहीं लेना चाहिए जिस पर आप निर्भर हैं और इसे कई स्थानों पर उपयोग करें? यदि आपकी चींटी गुण अक्सर बदल रहे हैं कि आपको प्रोग्रामेटिक रूप से उन पर प्रतिक्रिया करने की आवश्यकता है, तो आप कुछ और गलत कर रहे हैं। – PanCrit

उत्तर

33

मैं scriptdef का उपयोग करने के लिए एक-स्ट्रिंग जावास्क्रिप्ट टैग बनाने के लिए कर रहे हैं उदाहरण के लिए:

<project> 
    <scriptdef name="substring" language="javascript"> 
    <attribute name="text" /> 
    <attribute name="start" /> 
    <attribute name="end" /> 
    <attribute name="property" /> 
    <![CDATA[ 
     var text = attributes.get("text"); 
     var start = attributes.get("start"); 
     var end = attributes.get("end") || text.length(); 
     project.setProperty(attributes.get("property"), text.substring(start, end)); 
    ]]> 
    </scriptdef> 
    ........ 
    <target ...> 
    <substring text="asdfasdfasdf" start="2" end="10" property="subtext" /> 
    <echo message="subtext = ${subtext}" /> 
    </target> 
</project> 
2

मैं जानवर बल के साथ जाने के लिए और एक कस्टम चींटी कार्य लिखना होगा:

public class SubstringTask extends Task { 

    public void execute() throws BuildException { 
     String input = getProject().getProperty("oldproperty"); 
     String output = process(input); 
     getProject().setProperty("newproperty", output); 
    } 
} 

क्या String process(String) को लागू करने और (oldproperty और newproperty मूल्यों के लिए उदाहरण के लिए) setters के एक जोड़े

21
जोड़ने के लिए यह बचा है

आप PropertyRegex from Ant-conrtib का उपयोग करने का प्रयास कर सकते हैं।

<propertyregex property="destinationProperty" 
       input="${sourceProperty}" 
       regexp="regexToMatchSubstring" 
       select="\1" 
       casesensitive="false" /> 
+0

अतिरिक्त नोट: यदि यह एक लूप में गतिशील रूप से किया जाता है, तो किसी भी पिछले मान को ओवरराइड करने के लिए 'override = "true" 'का उपयोग करें। – robinst

0

मैं उस उद्देश्य के लिए स्क्रिप्ट कार्य का प्रयोग करेंगे, मैं गहरे लाल रंग का पसंद करते हैं, उदाहरण के काट पहले 3 वर्ण =

<project> 
    <property name="mystring" value="foobarfoobaz"/> 
    <target name="main"> 
    <script language="ruby"> 
    $project.setProperty 'mystring', $mystring[3..-1] 
    </script> 
    <echo>$${mystring} == ${mystring}</echo> 
    </target>  
    </project> 

उत्पादन =

main: 
    [echo] ${mystring} == barfoobaz 

साथ चींटी एपीआई का उपयोग कर एक मौजूदा संपत्ति पर विधि project.setProperty() प्रॉपर्टी इसे ओवरराइट कर देगी, इस तरह आप मानक चींटी व्यवहार के आसपास काम कर सकते हैं, इसका मतलब है संपत्ति एक बार सेट एँ, अपरिवर्तनीय

+0

हाय, मुझे यह त्रुटि मिली: रूबी –

+0

जारोड के लिए जावाक्स स्क्रिप्ट इंजन बनाने में असमर्थ, आपको bsf.jar (http://jakarta.apache.org/bsf) और jruby.jar की आवश्यकता है (http: //www.jruby .org), http://ant.apache.org/manual/install.html#librarydependencies भी देखें, ध्यान दें कि उस पृष्ठ पर jruby का लिंक गलत है। – Rebse

+0

ओह धन्यवाद रुब्स –

8

जब से मैं वेनिला चींटी का उपयोग करना पसंद है, मैं एक अस्थायी फ़ाइल का उपयोग करें। हर जगह काम करता है और आप स्ट्रिंग के हिस्से से छुटकारा पाने के लिए replregex का लाभ उठा सकते हैं नहीं चाहते हैं। गिट संदेशों को मंग करने के लिए उदाहरण:

<exec executable="git" output="${git.describe.file}" errorproperty="git.error" failonerror="true"> 
     <arg value="describe"/> 
     <arg value="--tags" /> 
     <arg value="--abbrev=0" /> 
    </exec> 
    <loadfile srcfile="${git.describe.file}" property="git.workspace.specification.version"> 
     <filterchain> 
      <headfilter lines="1" skip="0"/> 
      <tokenfilter> 
       <replaceregex pattern="\.[0-9]+$" replace="" flags="gi"/> 
      </tokenfilter> 
      <striplinebreaks/> 
     </filterchain> 
    </loadfile> 
+4

यदि आप किसी प्रॉपर्टी से शुरुआत करने के लिए काम कर रहे हैं, तो आप $ {property.here} Scott

+0

धन्यवाद! मैंने निष्पादन के बजाय ' 'का उपयोग किया और पूरी तरह से काम किया –