2010-09-07 6 views
2

Est-il possible de créer l'élément imbriqué pour n'importe quelle tâche ant. Par exempleComment créer un élément imbriqué pour une tâche ant?

<copy todir="../backup/dir"> 
    <fileset dir="src_dir"/> 
    <filterset> 
     <filter token="TITLE" value="Foo Bar"/> 
    </filterset> 
</copy> 

Ici la tâche copie nous avons des éléments imbriqués comme filterset. Maintenant, je voudrais créer mon propre élément imbriqué encryptfilterset pour la tâche copier.

<copy todir="../backup/dir"> 
    <fileset dir="src_dir"/> 
    <encryptfilterset> 
     <filter token="TITLE" value="Foo Bar"/> 
    </encryptfilterset> 
</copy> 

Comment pouvons-nous faire cela?

Répondre

1

nous devons étendre la tâche existante pour créer CustomTask et maintenant pour soutenir l'élément personnalisé imbriqué XYZ créer une méthode dans y notre nouvelle classe

public XYZ createXYZ();
ou
public void addXYZ(XYZ obj)
ou
public void addXYZ(XYZ obj)

<taskdef name="CustomTask" classname="com.ant.task.Customtask"> 
     <classpath> 
      <path location="lib/"/>   
     </classpath> 
    </taskdef> 

<typedef name="XYZ" classname="com.ant.type.XYZ" > 
     <classpath> 
      <path location="lib/"/> 
     </classpath> 
    </typedef> 


    <target name="MyTarget" > 
      <CustomTask> 
       <XYZ></XYZ> 
      </CopyEncrypted> 
     </target> 

Alors mes fichiers ressemblait à: -

public class CopyEncrypted extends Copy { 

    public EncryptionAwareFilterSet createEncryptionAwareFilterSet() 
    {  
     EncryptionAwareFilterSet eafilterSet = new EncryptionAwareFilterSet();  
     getFilterSets().addElement(eafilterSet);  
     return eafilterSet; 
    } 
} 


public class EncryptionAwareFilterSet extends FilterSet{ 

    @Override 
    public synchronized void readFiltersFromFile(File file) 
      throws BuildException { 
     log("EncryptionAwareFilterSet::reading filters",0); 
     super.readFiltersFromFile(file); 

     Vector<Filter> filts = getFilters(); 
     for (Iterator iterator = filts.iterator(); iterator.hasNext();) { 
      Filter filter = (Filter) iterator.next(); 
      if (filter.getToken().equalsIgnoreCase("PASSWORD")){ 
       filter.setValue(Encryptor.getEncryptedValue (filter.getValue()) ); 
      } 
     } 
    } 
} 

build.xml

<target name="encrypted-copy" > 
     <CopyEncrypted todir="dist/xyz/config" overwrite="true"> 
      <fileset dir="config"/>     
      <encryptionAwareFilterSet> 
       <filtersfile file="conf/properties/blah-blah.properties" /> 
      </encryptionAwareFilterSet> 
     </CopyEncrypted> 
    </target> 
0

Vous pouvez créer votre propre macrodef

(ce qui est juste un exemple)

<macrodef name="myCopy"> 
    <attribute name="todir" /> 
    <element name="path" /> 
    <sequential> 
     <copy todir="@{todir}"> 
      <path/> 
     </copy> 
    </sequential> 
</macrodef> 

je jouer avec macrodef il y a quelques exemples là-bas

http://ant.apache.org/manual/Tasks/macrodef.html

+0

macrodef n'est pas l'élément imbriqué. –

Questions connexes