我需要使用Ant脚本启动和停止Tomcat服务器,但我在Stack Overflow上找到了一个脚本,但当我使用它时,它会给出下面提到的错误。
下面提到了我在控制台上得到的脚本和消息:
<project name="sample">
<property name="name" value="sample"/>
<path id="catalina-ant-classpath">
<fileset dir="C:UsersvivsharmDownloadsapache-tomcat-7.0.64-windows-x64apache-tomcat-7.0.64lib">
<include name="catalina-ant.jar"/>
</fileset>
</path>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
<classpath refid="catalina-ant-classpath"/>
</taskdef>
<target name="start" description="Start Tomcat application">
<start url="http://localhost:8080/"
username="uname"
password="pwd"/>
</target>
<target name="stop" description="Stop Tomcat application">
<stop url="http://localhost:8080/"
username="uname"
password="pwd"/>
</target>
</project>
我得到的错误信息如下:
C:StartAndStopTomcat>ant start
Buildfile: C:StartAndStopTomcatbuild.xml
start:
BUILD FAILED
C:StartAndStopTomcatbuild.xml:19: Must specify 'path' attribute
Total time: 0 seconds
C:StartAndStopTomcat>ant stop
Buildfile: C:StartAndStopTomcatbuild.xml
stop:
BUILD FAILED
C:StartAndStopTomcatbuild.xml:24: Must specify 'path' attribute
Total time: 0 seconds
enter code here
C:StartAndStopTomcat>
我们的构建脚本很旧,所以有更好的方法可以做到这一点,但我们使用
<property name="tomcat.bin.dir" location="${tomcat.catalina_home}/bin" />
<property name="tomcat.startup.file" location="${tomcat.bin.dir}/startup${os.script_extension}" />
...
<target name="tom-start" description="start tomcat server">
<echo message="starting ${tomcat.catalina_base}" />
<exec executable="${os.command_interpreter}">
<arg line="${os.command_interpreter.mandatory_argument}" />
<arg line="${tomcat.startup.file}" />
<env key="CATALINA_BASE" value="${tomcat.catalina_base}" />
<env key="JAVA_HOME" value="${javac.jdk.dir}" />
<env key="JRE_HOME" value="${javac.jdk.dir}" />
<env key="JAVA_OPTS" value="${JAVA_OPTS}" />
</exec>
</target>
<target name="tom-shut" description="stop tomcat server (remember that it might not shut down immediately)">
<echo message="shutting down ${tomcat.catalina_base}" />
<exec executable="${os.command_interpreter}">
<arg line="${os.command_interpreter.mandatory_argument}" />
<arg line="${tomcat.shutdown.file}" />
<env key="CATALINA_BASE" value="${tomcat.catalina_base}" />
<env key="JAVA_HOME" value="${javac.jdk.dir}" />
<env key="JRE_HOME" value="${javac.jdk.dir}" />
</exec>
</target>
在linux机器上,os.command_interpreter
是/bin/bash
,在Windows机器上是C:\Windows\System32\cmd.exe
。当然,您还需要预设tomcat.catalina_home
和其他属性。