是否可以为任何ant任务创建嵌套元素。例如,
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<filterset>
<filter token="TITLE" value="Foo Bar"/>
</filterset>
</copy>
此处针对任务复制,我们将嵌套元素设为 filterset 。现在,我想为任务复制创建自己的嵌套元素 encryptfilterset 。
<copy todir="../backup/dir">
<fileset dir="src_dir"/>
<encryptfilterset>
<filter token="TITLE" value="Foo Bar"/>
</encryptfilterset>
</copy>
我们怎么能这样做?
答案 0 :(得分:1)
我们必须扩展现有任务以创建CustomTask
,现在支持自定义嵌套元素XYZ在新类中创建方法
public XYZ createXYZ();
或
public void addXYZ(XYZ obj)
或
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>
所以我的文件看起来像: -
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>
答案 1 :(得分:0)
您可以创建自己的macrodef
(这只是一个例子)
<macrodef name="myCopy">
<attribute name="todir" />
<element name="path" />
<sequential>
<copy todir="@{todir}">
<path/>
</copy>
</sequential>
</macrodef>
我会玩macrodef,那里有几个例子