在上一章中,我们学习了如何打包应用程序并将其部署到文件夹。

在本章中,我们将直接将Web应用程序部署到应用程序服务器deploy文件夹中,然后将添加一些Ant目标以启动和停止服务。让我们继续使用Hello World传真Web应用程序。这是上一章的续篇,新组件以粗体突出显示

build.properties

  1. # Ant properties for building the springapp
  2.  
  3. appserver.home = c:\\\\install\\\\apache-tomcat-7.0.19
  4. # for Tomcat 5 use $appserver.home}/server/lib
  5. # for Tomcat 6 use $appserver.home}/lib
  6. appserver.lib = ${appserver.home}/lib
  7.  
  8. deploy.path = ${appserver.home}/webapps
  9.  
  10. tomcat.manager.url = http://www.tutorialspoint.com:8080/manager
  11. tomcat.manager.username = tutorialspoint
  12. tomcat.manager.password = secret

build.xml

  1. <?xml version = "1.0"?>
  2.  
  3. <project name = "fax" basedir = "." default = "usage">
  4. <property file = "build.properties"/>
  5. <property name = "src.dir" value = "src"/>
  6. <property name = "web.dir" value = "war"/>
  7. <property name = "javadoc.dir" value = "doc"/>
  8. <property name = "build.dir" value = "${web.dir}/WEB-INF/classes"/>
  9. <property name = "name" value = "fax"/>
  10.  
  11. <path id = "master-classpath">
  12. <fileset dir = "${web.dir}/WEB-INF/lib">
  13. <include name = "*.jar"/>
  14. </fileset>
  15.  
  16. <pathelement path = "${build.dir}"/>
  17. </path>
  18.  
  19. <target name = "javadoc">
  20. <javadoc packagenames = "faxapp.*" sourcepath = "${src.dir}"
  21. destdir = "doc" version = "true" windowtitle = "Fax Application">
  22.  
  23. <doctitle><![CDATA[<h1> = Fax Application = </h1>]]></doctitle>
  24. <bottom><![CDATA[Copyright © 2011. All Rights Reserved.]]></bottom>
  25. <group title = "util packages" packages = "faxapp.util.*"/>
  26. <group title = "web packages" packages = "faxapp.web.*"/>
  27. <group title = "data packages" packages = "faxapp.entity.*:faxapp.dao.*"/>
  28. </javadoc>
  29. </target>
  30.  
  31. <target name = "usage">
  32. <echo message = ""/>
  33. <echo message = "${name} build file"/>
  34. <echo message = "-----------------------------------"/>
  35. <echo message = ""/>
  36. <echo message = "Available targets are:"/>
  37. <echo message = ""/>
  38. <echo message = "deploy --> Deploy application as directory"/>
  39. <echo message = "deploywar --> Deploy application as a WAR file"/>
  40. <echo message = ""/>
  41. </target>
  42.  
  43. <target name = "build" description = "Compile main source tree java files">
  44.  
  45. <mkdir dir = "${build.dir}"/>
  46.  
  47. <javac destdir = "${build.dir}" source = "1.5" target = "1.5" debug = "true"
  48. deprecation = "false" optimize = "false" failonerror = "true">
  49. <src path = "${src.dir}"/>
  50. <classpath refid = "master-classpath"/>
  51. </javac>
  52. </target>
  53.  
  54. <target name = "deploy" depends = "build" description = "Deploy application">
  55. <copy todir = "${deploy.path}/${name}"
  56. preservelastmodified = "true">
  57.  
  58. <fileset dir = "${web.dir}">
  59. <include name = "**/*.*"/>
  60. </fileset>
  61. </copy>
  62. </target>
  63.  
  64. <target name = "deploywar" depends = "build" description =
  65. "Deploy application as a WAR file">
  66.  
  67. <war destfile = "${name}.war" webxml = "${web.dir}/WEB-INF/web.xml">
  68. <fileset dir = "${web.dir}">
  69. <include name = "**/*.*"/>
  70. </fileset>
  71. </war>
  72.  
  73. <copy todir = "${deploy.path}" preservelastmodified = "true">
  74. <fileset dir = ".">
  75. <include name = "*.war"/>
  76. </fileset>
  77. </copy>
  78. </target>
  79.  
  80. <target name = "clean" description = "Clean output directories">
  81. <delete>
  82. <fileset dir = "${build.dir}">
  83. <include name = "**/*.class"/>
  84. </fileset>
  85. </delete>
  86. </target>
  87.  
  88. <!-- ============================================================ -->
  89. <!-- Tomcat tasks -->
  90. <!-- ============================================================ -->
  91.  
  92. <path id = "catalina-ant-classpath">
  93. <!-- We need the Catalina jars for Tomcat -->
  94. <!-- * for other app servers - check the docs -->
  95.  
  96. <fileset dir = "${appserver.lib}">
  97. <include name = "catalina-ant.jar"/>
  98. </fileset>
  99. </path>
  100.  
  101. <taskdef name = "install" classname = "org.apache.catalina.ant.InstallTask">
  102. <classpath refid = "catalina-ant-classpath"/>
  103. </taskdef>
  104.  
  105. <taskdef name = "reload" classname = "org.apache.catalina.ant.ReloadTask">
  106. <classpath refid = "catalina-ant-classpath"/>
  107. </taskdef>
  108.  
  109. <taskdef name = "list" classname = "org.apache.catalina.ant.ListTask">
  110. <classpath refid = "catalina-ant-classpath"/>
  111. </taskdef>
  112.  
  113. <taskdef name = "start" classname = "org.apache.catalina.ant.StartTask">
  114. <classpath refid = "catalina-ant-classpath"/>
  115. </taskdef>
  116.  
  117. <taskdef name = "stop" classname = "org.apache.catalina.ant.StopTask">
  118. <classpath refid = "catalina-ant-classpath"/>
  119. </taskdef>
  120.  
  121. <target name = "reload" description = "Reload application in Tomcat">
  122. <reload url = "${tomcat.manager.url}"username = "${tomcat.manager.username}"
  123. password = "${tomcat.manager.password}" path = "/${name}"/>
  124. </target>
  125. </project>

在此示例中,我们使用Tomcat作为我们的应用程序服务器。首先,在构建属性文件中,我们定义了一些其他属性。

  • appserver.home指向安装路径到Tomcat应用服务器。

  • appserver.lib指向Tomcat的安装文件夹中的库文件。

  • 现在,deploy.path变量指向Tomcat中的webapp文件夹。

可以使用Tomcat管理器应用程序停止和启动Tomcat中的应用程序。管理器应用程序的URL,用户名和密码也在build.properties文件中指定。接下来,我们声明一个包含catalina-ant.jar的新CLASSPATH 。通过Apache Ant执行Tomcat任务需要该jar文件。

catalina-ant.jar提供以下任务

Sr.No. 属性和说明
1

InstallTask

安装Web应用程序。

类名称:org.apache.catalina.ant.InstallTask

2

ReloadTask

重新加载Web应用程序。

类名称:org.apache.catalina.ant.ReloadTask

3

ListTask

列出所有Web应用程序。

类名称: org.apache.catalina.ant.ListTask

4

StartTask

启动一个Web应用程序。

Class Name: org.apache.catalina.ant.StartTask

5

StopTask

停止Web应用程序。

Class Name: org.apache.catalina.ant.StopTask

6

ReloadTask

重新加载Web应用程序而不会停止。

Class Name: org.apache.catalina.ant.ReloadTask

重新加载任务需要以下附加参数

  • 经理应用程序的网址

  • 重新启动Web应用程序的用户名

  • 重新启动Web应用程序的密码

  • 要重新启动的Web应用程序的名称

让我们发出deploy-war命令将webapp复制到Tomcat webapps文件夹,然后让我们重新加载Fax Web应用程序。以下结果是运行Ant文件的结果

  1. C:\\>ant deploy-war
  2. Buildfile: C:\\build.xml
  3.  
  4. BUILD SUCCESSFUL
  5. Total time: 6.3 seconds
  6.  
  7. C:\\>ant reload
  8. Buildfile: C:\\build.xml
  9.  
  10. BUILD SUCCESSFUL
  11. Total time: 3.1 seconds

运行上述任务后,将部署Web应用程序并重新加载Web应用程序。