关闭启动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;
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值