lichaoyue888 发表于 2015-8-8 10:20:52

关闭启动tomcat

在发布war文件的时候经常需要关闭启动tomcat,太麻烦了,就起了怎么在ant里实现一体化的想法

在google了一下以后找到了答案,经过验证是可行的



一、失败的解决方案,不够好

通常的想法是写一个target来实行,可是这个样子好像不太来晒艾,ant会堵塞在那里一直到tomcat挂了为止.

执行以下的ant target,你只能得到以下的结果,永远得不到"END STARTUP TOMCAT 4.1",除非tomcat结束了
       STARTUP TOMCAT 4.1
       Using CATALINA_BASE:   C:/Tomcat4.1
       Using CATALINA_HOME:   C:/Tomcat4.1
       Using CATALINA_TMPDIR: C:/Tomcat4.1\temp
       Using JAVA_HOME:       C:/j2sdk1.4.2_02



   
   
      
      
      
   

   
   
      
      
      
   
   





二、有个强人给了一个解决办法,自写tomcat的任务

---------------------




   
   
   
   
      
            
      
   
   
   
   
      
   

---------------------


package org.rogerCompany;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* An Ant Task that does the following :
*
*create a java thread,
*
*start another Ant target in that thread. This target must be a
*blocking target.
*
*
*/
public class StartServerTask extends Task {
    /**
   * Helper class
   */
    private StartServerHelper helper;
    /**
   * Initialization
   */
    public void init() {
      this.helper = new StartServerHelper(this);
    }
    /**
   * @see Task#execute()
   */
    public void execute() throws BuildException {
      this.helper.execute();
    }
    /**
   * Ant will automatically call this method when the "startTarget" attribute
   * of our task is used.
   *
   * @param theStartTarget the Ant target to call
   */
    public void setStartTarget(String theStartTarget) {
      this.helper.setStartTarget(theStartTarget);
    }
}
---------------------


package org.rogerCompany;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.CallTarget;
/**
* A helper class for an Ant Task that does the following :
*create a java thread,
*
*start another Ant target in that thread. This target must be a
*blocking target.
*
*/
public class StartServerHelper implements Runnable {
    /**
   * The Ant target name that will start the blocking target.
   */
    private String startTarget;
    /**
   * The tasks that wraps around this helper class
   */
    private Task task;
    /**
   * @param theTask the Ant task that is calling this helper
   */
    public StartServerHelper(Task theTask) {
      this.task = theTask;
    }
    /**
   * @see Task#execute()
   */
    public void execute() throws BuildException {
      // Verify that a start target has been specified
      if (this.startTarget == null) {
            throw new BuildException("A startTarget Ant target name must " +
                  "be specified");
      }
      // Call the target in another thread. The called
      // target must be blocking.
      Thread thread = new Thread(this);
      thread.start();
      // We're done ... Ant will continue processing other tasks
    }
    /**
   * The thread that calls the Ant target that starts, for example, a server.
   * Must be a blocking target.
   */
    public void run() {
      // Call the Ant target using the "antcall" task.
      CallTarget callee;
      callee = (CallTarget) (this.task.getProject().createTask("antcall"));
      callee.setOwningTarget(this.task.getOwningTarget());
      callee.setTaskName(this.task.getTaskName());
      callee.setLocation(this.task.getLocation());
      callee.init();
      callee.setTarget(this.startTarget);
      callee.execute();
      // Should never reach this point as the target is blocking, unless the
      // server is stopped.
    }
    /**
   * @param theStartTarget the Ant target to call
   */
    public void setStartTarget(String theStartTarget) {
      this.startTarget = theStartTarget;
    }
}
页: [1]
查看完整版本: 关闭启动tomcat