2009-06-18 14 views
5

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

<target name="createZip" depends="build"> 
    <zip destfile="../dist/MyApp_${MyApp.version}.zip"> 
     <fileset dir="../dist"> 
      <include name="**/MyApp"/> 
     </fileset> 
    </zip> 
</target> 

मैं सिर्फ यकीन है कि क्या $ {MyApp.version} के स्थान पर डाल करने के लिए नहीं कर रहा हूँ। मुझे एहसास है कि मैं इसे बिल्ड प्रॉपर्टी फाइल में डाल सकता हूं, लेकिन इसे सीधे उस क्लास फ़ाइल से खींचने में सक्षम होना बहुत सुविधाजनक होगा, जिसे मैं पहले से ही संग्रहीत कर रहा हूं।

+0

एक और विकल्प: http://stackoverflow.com/questions/823909/how-ant-can-get-a-value-read-from-a-file-into-a-property-value – whiskeyspider

उत्तर

2

मुझे नहीं लगता कि चींटी कार्यों में कोई भी बनाया गया है जो आप चाहते हैं। हालांकि, आप चींटी की एक्स्टेंसिबल प्रकृति का लाभ उठाकर अपना खुद का रोल कर सकते हैं।

मैंने वास्तव में हैक किया है (और मेरा मतलब है वास्तव में) गंदा उदाहरण आप एक उचित कार्य के लिए वसंत बोर्ड के रूप में उपयोग कर सकते हैं।

package q1015732; 

import java.io.ByteArrayOutputStream; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.InputStream; 
import java.lang.reflect.Field; 

import org.apache.tools.ant.BuildException; 
import org.apache.tools.ant.Task; 

/** 
* Custom ant task that binds a property name to a static member of a class file. 
* 
* @author Montrose 
*/ 
public class BindPropertyTask extends Task{ 
    private String _classFile = null; 
    private String _fieldName = null; 
    private String _propName = null; 

    /** 
    * Set the field name of the class. 
    * 
    * @param fieldName 
    */ 
    public void setFieldName(String fieldName){ 
     _fieldName = fieldName; 
    } 

    /** 
    * The class file. 
    * @param classFile 
    */ 
    public void setClassFile(String classFile){ 
     _classFile = classFile; 
    } 

    /** 
    * The property name to bind 
    * @param propName 
    */ 
    public void setProperty(String propName) 
    { 
     _propName = propName; 
    } 

    /** 
    * Load the class file, and grab the value of the field. 
    * 
    * Throws exceptions if classfile, fieldname, or property have not been set. 
    * 
    * Throws more execeptions if classfile does not exist, the field does not exist, or the field is not static. 
    */ 
    public void execute() throws BuildException{ 
     if(_classFile == null) throw new BuildException("ClassFile is a required attribute"); 
     if(_fieldName == null) throw new BuildException("FieldName is a required attribute"); 
     if(_propName == null) throw new BuildException("Property is required attribute"); 

     CustomLoader loader = new CustomLoader(); 
     Class toInspect = null; 
     Field toBind = null; 
     Object value = null; 

     try { 
      toInspect = loader.loadClass(new FileInputStream(_classFile)); 
     } catch (Exception e) { 
      throw new BuildException("Couldn't load class ["+e.getMessage()+"], in ["+(new File(_classFile).getAbsolutePath())+"]"); 
     } 

     try{ 
      toBind = toInspect.getField(_fieldName); 
     }catch(NoSuchFieldException e){ 
      throw new BuildException("Couldn't find field, '"+_fieldName+"'"); 
     } 

     //Let us bind to private/protected/package-private fields 
     toBind.setAccessible(true); 

     try{ 
      value = toBind.get(null); 
     }catch(NullPointerException e){ 
      throw new BuildException("Field is not static"); 
     } catch (Exception e) { 
      throw new BuildException("Unable to access field ["+e.getMessage()+"]"); 
     } 

     if(value != null) 
      this.getProject().setProperty(_propName, value.toString()); 
     else 
      this.getProject().setProperty(_propName, null); 
    } 

    /** 
    * Custom class loader, for loading a class directly from a file. 
    * 
    * This is hacky and relies on deprecated methods, be wary. 
    * 
    * @author Montrose 
    */ 
    class CustomLoader extends ClassLoader{ 
     public CustomLoader(){ 
      super(ClassLoader.getSystemClassLoader()); 
     } 

     /** 
     * Warning, here be (deprecated) dragons. 
     * @param in 
     * @return 
     * @throws Exception 
     */ 
     @SuppressWarnings("deprecation") 
     public Class loadClass(InputStream in) throws Exception{ 
      byte[] classData = loadData(in); 
      return this.defineClass(classData, 0, classData.length); 
     } 

     private byte[] loadData(InputStream in) throws Exception{ 
      byte[] buffer = new byte[1024]; 
      ByteArrayOutputStream out = new ByteArrayOutputStream(); 
      int i; 


      while((i = in.read(buffer)) != -1){ 
       out.write(buffer, 0, i); 
      } 

      return out.toByteArray(); 
     } 
    } 
} 

एक उदाहरण का निर्माण फ़ाइल का उपयोग करते हुए इस कार्य:

<project name="q1015732"> 

<target name="test"> 
<taskdef name="static_bind" classname="q1015732.BindPropertyTask" /> 

<static_bind fieldname="StringBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="string.value" /> 
<static_bind fieldname="IntBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="int.value" /> 
<static_bind fieldname="DoubleBind" classfile="C:\Users\Montrose\workspace\StackOverflow Questions\q1015732\test\DummyMain.class" property="double.value" /> 

<echo message="StringBind: ${string.value}" /> 
<echo message="IntBind: ${int.value}" /> 
<echo message="DoubleBind: ${double.value}" /> 

</target> 

</project> 

DummyMain.java:

package q1015732.test; 

public class DummyMain { 
    public static String StringBind = "I'm a String!"; 
    public static int IntBind = 1024; 
    public static double DoubleBind = 3.14159; 
} 

चींटी फ़ाइल का उपयोग कर निर्माण का परिणाम:

परीक्षण :
[ई cho] स्ट्रिंगबिंद: मैं एक स्ट्रिंग हूँ!
[गूंज] IntBind: 1024
[गूंज] DoubleBind: 3.14159
BUILD सफल

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

+0

सहायता के लिए धन्यवाद ! मैंने केवल "आवश्यकतानुसार" आधार पर चींटी में डब किया है;) – Ross

+0

आपका स्वागत है।मेरे विवेक को एक अनुकूल वातावरण में फेंकने से पहले कोड को थोड़ा सा और विस्तार-फिक्स-अप करें। :) –

1

custom ant task जो विस्तारित करता है डिफ़ॉल्ट ?

org.apache.tools.ant.taskdefs.Zip क्लास बढ़ाएं और setDestFile() विधि को ओवरराइड करें ताकि यह MyApp.version विशेषता प्राप्त हो और उस फ़ाइल को रखने के लिए गंतव्य फ़ाइल नाम सेट कर सके।

एक अन्य समाधान CVS और SVN (शायद GIT और मर्क्युरियल इस सुविधा भी है) से कीवर्ड प्रतिस्थापन सुविधा का उपयोग करते हैं और इस (SVN में) की तरह कुछ के साथ एक संपत्ति फ़ाइल होने की तरह इस सुविधा के चतुर उपयोग करने के लिए है:

 
myApp.revision=$Revision$ 

और उस संदर्भ को देखें कि आपके कोड को थ्रूगआउट करें और स्क्रिप्ट बनाएं।

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

  • कोई संबंधित समस्या नहीं^_^