软件频道>程序开发>JavaVBVCDelphiC/C++Web开发微软专栏移动数据库程序人生软件工程|开发客
您现在的位置: 天极网 > 开发频道 > Java Web应用中使用ANT在线编译不用重起tomcat的方法
全文

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

2008-05-14 08:59作者:来自网络出处:天极网责任编辑:nancy
       在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中.

相关搜索:
关注此文读者还看过
文章排行
本周
本月
最近更新
关于我们|About us|网站律师|天极服务|电子杂志|RSS订阅|加入我们|网站地图
TMG
Copyright (C) 1999-2009 Chinabyte.com, All Rights Reserved 版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868
版权声明 在线提交意见反馈 渝ICP证B2-20030003号
经营性网站备案信息 网警备案 中国网站排名
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众