关于ant实现自定义的任务

yukang_ky 2009-01-09 11:21:33
自定义任务类:

package test;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

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

public class FileSorter extends Task {

private File file;
private File tofile;
public void setFile(File file) {
this.file = file;
}
public void setTofile(File tofile) {
this.tofile = tofile;
}
@SuppressWarnings("unchecked")
@Override
public void execute() throws BuildException {
System.out.println("Sorting file="+file);
try {
BufferedReader form=new BufferedReader(new FileReader(file));
BufferedWriter to=new BufferedWriter(new FileWriter(tofile));

List allList=new ArrayList();
String line=form.readLine();
while(line!=null){
allList.add(line);
line=form.readLine();
}
form.close();
//sort the lise
Collections.sort(allList);
//write out the sorted list
for(ListIterator i=allList.listIterator();i.hasNext();){
String s=(String)i.next();
to.write(s);
to.newLine();
}
to.close();
} catch (FileNotFoundException e) {
throw new BuildException();
//e.printStackTrace();
} catch(IOException e){
throw new BuildException();
}
}
}



build.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project name="CustomTaskExample" default="main" basedir=".">
<property name="src" location="src"/>
<property name="compile" location="classes"/>

<taskdef name="filesorter" classname="FileSorter" classpath="classes" />

<target name="main" depends="compile">
<filesorter file="input.txt" tofile="output.txt"/>
</target>

<target name="compile">
<mkdir dir="${compile}"/>
<javac srcdir="${src}" destdir="${compile}"/>

</target>
</project>

执行build.xml文件时发生错误:
F:\workstudy\ant\build1.xml:22: taskdef class FileSorter cannot be found

请教各位,该如何解决,谢谢。
...全文
192 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yukang_ky 2009-01-12
  • 打赏
  • 举报
回复
好的
yukang_ky 2009-01-09
  • 打赏
  • 举报
回复
自己顶一下
socool627 2009-01-09
  • 打赏
  • 举报
回复
类找不打到。
<taskdef name="filesorter" classname="FileSorter" classpath="classes" />
classpath=""用绝对路径试试
yukang_ky 2009-01-09
  • 打赏
  • 举报
回复
怎么没有人呢?

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧