技术教程 | 经验技巧 | 网络专区 | 安全防线 更多 >>
Java Web应用中使用ANT在线编译不重起tomcat的方法
2008-5-14 20:55:31  (人气:)
       在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 
  { 
   return "编译失败!"; 
  } 
 } 

 /** 
  * @param args 
  */ 
 public static void main(String[] args) { 
  System.out.println("开始编译..."); 
    
  boolean b = CompileJava.compile("D:/easdev/build/userbuild.xml", 
    "D:/easdev/build/userbuild.log"); 
  if(b) 
  { 
   System.out.println("编译成功!"); 
  } 
  else 
  { 
   System.out.println("编译失败!"); 
  } 
  System.out.println("结束编译..."); 

 } 




下面是Java页面通过dwr调用此类的代码: 

<%@ page contentType="text/html; charset=GBK"%> 

<%@ page import="org.apache.easframework.common.*" %> 
<%@ taglib prefix="ww" uri="webwork" %> 
<%@ include file="/common/head.jsp"%>  
<script type='text/javascript' src='<%=request.getContextPath()%>/js/util.js?Math.random();'></script> 
<script type='text/javascript' src='<%=request.getContextPath()%>/js/engine.js?Math.random();'></script> 
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/CompileJava.js?Math.random();'></script>  
<meta http-equiv="epires" content=0> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<meta http-equiv="pragma" content="no-cache"> 
<meta http-equiv="cache-control" content="no-cache"> 
<script language="JavaScript"> 
<!--  
function compileJava() 

 CompileJava.startCompile("D:/easdev/build/userbuild.xml","D:/easdev/build/userbuild.log",callback); 
  
  // document.listForm.elements['pageLoaderInfo.custSort'].value = sortValue; 
  // document.listForm.operate.value="selectPageList"; 
  // document.listForm.submit(); 

function callback(data) 

   alert(data); 

--> 
</script> 
  
<table><tr><td><input type="button" name="compile" value="编译" class="mybutton" onClick="compileJava()"></td></tr></table> 
<%@include file="/common/tail.jsp"%> 

上面在编译成功时弹出编译成功的JS提示,否则提示编译失败 

另外需要说明的是,要实现在线ANT编译,需要把ant工具相关的一些jar包复制到tomcat的shared\lib或者WEB-INF\lib中. 
评论 】 【 打印 】 【 关闭