原先我在程序员调用ant是使用java调用外部程序的方法,现在发现这是一种最笨的方法,因为ant进程一旦当掉,就会影响RCP程序,很难调整。
其实,ant本身就是java 程序,提供了大量的接口供开发人员调用,下面这个类是一个完整的ant调用类,可以根据文件夹路径对该文件夹下的java文件进行编译:
/*
* @ CompileJava.java 2006-3-24
*
* @ Author GuanHui
*
* Copyright GuanHui . All rights reserved.
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
public class CompileJava {
/**
* 利用ant对java文件进行编译
*
* @param buildFilePath 需要编译的java文件夹
* @param logFilePath ant log文件路径
*/
public boolean Compile(String buildFilePath, String logFilePath) {
File buildFile = new File(buildFilePath);
Project project = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
try {
FileOutputStream fs = new FileOutputStream(logFilePath);
PrintStream ps = new PrintStream(fs);
consoleLogger.setErrorPrintStream(ps);
consoleLogger.setOutputPrintStream(ps);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(consoleLogger);
try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
return true;
} catch (BuildException e) {
project.fireBuildFinished(e);
return false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
CompileJava compileJava = new CompileJava();
compileJava.Compile("C:\\MyData\\Java\\temp\\build.xml",
"C:\\MyData\\Java\\temp\\build.log");
}
}
其实,ant本身就是java 程序,提供了大量的接口供开发人员调用,下面这个类是一个完整的ant调用类,可以根据文件夹路径对该文件夹下的java文件进行编译:
/*
* @ CompileJava.java 2006-3-24
*
* @ Author GuanHui
*
* Copyright GuanHui . All rights reserved.
*
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DefaultLogger;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
public class CompileJava {
/**
* 利用ant对java文件进行编译
*
* @param buildFilePath 需要编译的java文件夹
* @param logFilePath ant log文件路径
*/
public boolean Compile(String buildFilePath, String logFilePath) {
File buildFile = new File(buildFilePath);
Project project = new Project();
DefaultLogger consoleLogger = new DefaultLogger();
try {
FileOutputStream fs = new FileOutputStream(logFilePath);
PrintStream ps = new PrintStream(fs);
consoleLogger.setErrorPrintStream(ps);
consoleLogger.setOutputPrintStream(ps);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
project.addBuildListener(consoleLogger);
try {
project.fireBuildStarted();
project.init();
ProjectHelper helper = ProjectHelper.getProjectHelper();
helper.parse(project, buildFile);
project.executeTarget(project.getDefaultTarget());
project.fireBuildFinished(null);
return true;
} catch (BuildException e) {
project.fireBuildFinished(e);
return false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
CompileJava compileJava = new CompileJava();
compileJava.Compile("C:\\MyData\\Java\\temp\\build.xml",
"C:\\MyData\\Java\\temp\\build.log");
}
}