通过 Apache Ant 来运行 Tomcat

173 阅读1分钟

Tomcat安装目录下的lib文件夹里的catalina-ant.jar 提供了下述的任务:

  • InstallTask:安装一个 web 应用程序。 类名字为: org.apache.catalina.ant.InstallTask

注意,我用tomcat 9试的时候,InstallTask已经被替换成DeployTask:

  • ReloadTask:重新安装一个 web 应用程序。类名字为: org.apache.catalina.ant.ReloadTask
  • ListTask:列出所有的 web 应用程序。类名字为: Class Name: org.apache.catalina.ant.ListTask
  • StartTask:启动一个 web 应用程序。类名字为: org.apache.catalina.ant.StartTask
  • StopTask:停止一个 web 应用程序。类名字为: org.apache.catalina.ant.StopTask
  • ReloadTask:重新加载一个无需停止的 web 应用程序。类名字为:org.apache.catalina.ant.ReloadTask

build.properties的源代码:

# Ant properties for building the spring app

appserver.home=C:\\MyApp\\apache-tomcat-9.0.29

appserver.lib=${appserver.home}/lib

deploy.path=${appserver.home}/webapps

tomcat.manager.url=http://localhost:9032/manager
tomcat.manager.username=jerry
tomcat.manager.password=jerry@sap

build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project name="jerryjsp" basedir="." default="build">
	 <property file="build.properties"/>
   <property name="src.dir" value="src"/>
   <property name="web.dir" value="WebContent"/>
   <property name="build.dir" value="${web.dir}/WEB-INF/classes"/>
   <property name="name" value="jerryjsp"/>

   <path id="master-classpath">
      <fileset dir="${web.dir}/WEB-INF/lib">
         <include name="*.jar"/>
      </fileset>
    	<fileset dir="C:/MyApp/apache-tomcat-9.0.29/lib">
   	         <include name="*.jar"/>
   	      </fileset>
      <pathelement path="${build.dir}"/>
   </path>

   <target name="build" description="Compile source tree java files">
      <mkdir dir="${build.dir}"/>
      <javac destdir="${build.dir}" source="1.8" target="1.8">
         <src path="${src.dir}"/>
         <classpath refid="master-classpath"/>
      </javac>
   </target>

   <target name="clean" description="Clean output directories">
      <delete>
         <fileset dir="${build.dir}">
            <include name="**/*.class"/>
         </fileset>
      	<fileset dir=".">
      		<include name="*.war"/>
      	</fileset>
      </delete>
   </target>
	
	<target name = "generate-javadoc">
	   <javadoc packagenames="action.*" sourcepath="${src.dir}" 
	      destdir = "doc" version = "true" windowtitle = "Jerry Application">

	      <doctitle><![CDATA[= Jerrt Application =]]></doctitle>

	      <bottom>
	         <![CDATA[Copyright © 2020. JerryAll Rights Reserved.]]>
	      </bottom>

	      <group title = "action" packages = "action.*"/>
	   </javadoc>

	   <echo message = "java doc has been generated!" />
	</target>
	
	<target name="build-war" depends="build">
	   <war destfile="jerryjsp.war" webxml="${web.dir}/WEB-INF/web.xml">
	      <fileset dir="${web.dir}">
	         <include name="**/*.*"/>
	      </fileset>
	   </war>

	</target>
	
	<path id="catalina-ant-classpath">
	   <fileset dir="${appserver.lib}">
	      <include name="catalina-ant.jar"/>
	   </fileset>
	</path>
	
	<taskdef name="install" classname="org.apache.catalina.ant.DeployTask">
	   <classpath refid="catalina-ant-classpath"/>
	</taskdef>
	<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
	   <classpath refid="catalina-ant-classpath"/>
	</taskdef>
	<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
	   <classpath refid="catalina-ant-classpath"/>
	</taskdef>
	<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="deploywar" depends="build-war" description="Deploy application as a WAR file">
	      <copy todir="${deploy.path}" preservelastmodified="true">
	         <fileset dir=".">
	            <include name="*.war"/>
	         </fileset>
	      </copy>
	   </target>
</project>

执行命令行ant deploywar, 即可将JSP项目webcontent文件夹下的资源打成war包,然后复制到tomcat服务器的webapps文件夹内。


target deploywar依赖于build-war:

build-war依赖于build:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":