本文介绍了如何创建Ant任务嵌套元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能对任何Ant任务创建嵌套元素。对于例如。
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>
下面的任务复制我们有嵌套的元素作为 filterset 。现在,我想创建自己的嵌套元素的 encryptfilterset 的任务复制
Here for the task copy we are having nested element as filterset. 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?
推荐答案
我们必须扩展现有的任务来创建 CustomTask
现在,支持自定义嵌套元素XYZ创建在新的类中的方法
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
公共XYZ createXYZ();
结果
或结果
公共无效addXYZ(XYZ OBJ)
结果
或结果
公共无效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
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>
这篇关于如何创建Ant任务嵌套元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!