开发者

How to create nested element for ant task?

开发者 https://www.devze.com 2023-01-15 16:52 出处:网络
Is it possible to create the nested element for any ant task. For e.g. <copy todir=\"../backup/dir\">

Is it possible to create the nested element for any ant task. For e.g.

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

Here for the task copy we are having nested element as fi开发者_Go百科lterset. Now, i would like to create my own nested element encryptfilterset for the task copy.

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

How can we do that?


we have to extend the existing task to create CustomTask and now to support the custom nested element XYZ create a method in your new class

public XYZ createXYZ();
or
public void addXYZ(XYZ obj)
or
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>

So my files looked like:-

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>


You can create your own macrodef

(this is just an example)

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

I would play around with macrodef there are a few examples out there

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号