在WEB应用中可通过ANT的API调用ant的工程配置文件来在线编译java文件,在工程配置文件中(如build.xml)将编译的class文件或者变更的xml文件直接复制到WEB-INF\\classes中的对应目录,不用重新启动tomcat.
由于在平台应用中经常由用户定义表结构,并由表结构生成java实体类和hibernate映射文件,通过热编译部署的方式 可不用停止WEB应用,下面是在Java中调用ant的代码,注意这种方式不是调用ant的批处理的,也不提倡这样做,下面的方式可使用户通过点击WEB页面上的按钮来调用ANT编译:
package org.apache.easframework.common;
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 {
public static synchronized 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);
System.out.println(\"默认target:\");
System.out.println(project.getDefaultTarget()); //默认的target是help
project.executeTarget(\"all\"); //调用的task是all
project.fireBuildFinished(null);
return true;
} catch (BuildException e) {
project.fireBuildFinished(e);
return false;
}
}
public static String startCompile(String buildFile,String logFile)
{
if(CompileJava.compile(buildFile, logFile))
{
return \"编译成功!\";
}
else
{
re