关闭启动tomcat

在发布war文件的时候经常需要关闭启动tomcat,太麻烦了,就起了怎么在ant里实现一体化的想法
在google了一下以后找到了答案,经过验证是可行的

一、失败的解决方案,不够好
通常的想法是写一个target来实行,可是这个样子好像不太来晒艾,ant会堵塞在那里一直到tomcat挂了为止.
执行以下的ant target,你只能得到以下的结果,永远得不到"END STARTUP TOMCAT 4.1",除非tomcat结束了
[echo] STARTUP TOMCAT 4.1
[exec] Using CATALINA_BASE: C:/Tomcat4.1
[exec] Using CATALINA_HOME: C:/Tomcat4.1
[exec] Using CATALINA_TMPDIR: C:/Tomcat4.1\temp
[exec] Using JAVA_HOME: C:/j2sdk1.4.2_02
<target name="rroger">
<echo message="SHUTDOWN TOMCAT 4.1"></echo>
<exec dir="c:/Tomcat4.1/bin" executable="cmd">
<env key="JAVA_HOME" value="C:/j2sdk1.4.2_02"/>
<env key="CATALINA_HOME" value="C:/Tomcat4.1"/>
<arg value="/c shutdown.bat"/>
</exec>

<echo message="STARTUP TOMCAT 4.1"></echo>
<exec dir="${tomcat_home}/bin" executable="cmd">
<env key="JAVA_HOME" value="C:/j2sdk1.4.2_02"/>
<env key="CATALINA_HOME" value="C:/Tomcat4.1"/>
<arg value="/c startup.bat"/>
</exec>
<echo message="END STARTUP TOMCAT 4.1"></echo>
</target>


二、有个强人给了一个解决办法,自写tomcat的任务
-----[build.xml]----------------
<project name="My Application" default="BlockingTask" basedir=".">
<taskdef name="startServer" classname="org.rogerCompany.StartServerTask" classpath="${classpath}"/>


<target name="BlockingTask">
<exec dir="C:/Tomcat4.1/bin" executable="cmd.exe">
<arg line="/c startup.bat"/>
</exec>
</target>


<target name="startServerAsync">
<startServer startTarget="BlockingTask"/>
</target>
</project>
-----[StartServerTask.java]----------------
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);
}
}
-----[StartServerHelper.java]----------------
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;
}
}
posted @ 2008-10-21 15:48  Earl_86  阅读(1154)  评论(0编辑  收藏  举报