频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Java Web应用中使用ANT在线编译不用重起tomcat的方法

来源: 作者:来自网络 出处:巧巧读书 2008-05-14 进入讨论组
       在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中.

更多文章 更多内容请看Java环境安装配置Java编程开发手册Java的类专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章