包装应用( Packaging Applications)

优质
小牛编辑
134浏览
2023-12-01

我们已经使用Hello World传真Web应用程序逐点了解Ant的不同方面。

现在是时候把所有东西放在一起来创建一个完整而完整的build.xml文件。 考虑build.propertiesbuild.xml文件,如下所示 -

build.properties

deploy.path = c:\tomcat6\webapps

build.xml

<?xml version = "1.0"?>
<project name = "fax" basedir = "." default = "usage">
   <property file = "build.properties"/>
   <property name = "src.dir" value = "src"/>
   <property name = "web.dir" value = "war"/>
   <property name = "javadoc.dir" value = "doc"/>
   <property name = "build.dir" value = "${web.dir}/WEB-INF/classes"/>
   <property name = "name" value = "fax"/>
   <path id = "master-classpath">
      <fileset dir = "${web.dir}/WEB-INF/lib">
         <include name = "*.jar"/>
      </fileset>
      <pathelement path = "${build.dir}"/>
   </path>
   <target name = "javadoc">
      <javadoc packagenames = "faxapp.*" sourcepath = "${src.dir}" 
         destdir = "doc" version = "true" windowtitle = "Fax Application">
         <doctitle><![CDATA[<h1> =  Fax Application  = </h1>]]>
         </doctitle>
         <bottom><![CDATA[Copyright © 2011. All Rights Reserved.]]>
         </bottom>
         <group title = "util packages" packages = "faxapp.util.*"/>
         <group title = "web packages" packages = "faxapp.web.*"/> 
         <group title = "data packages" packages = "faxapp.entity.*:faxapp.dao.*"/>
      </javadoc>
   </target>
   <target name = "usage">
      <echo message = ""/>
      <echo message = "${name} build file"/>
      <echo message = "-----------------------------------"/>
      <echo message = ""/>
      <echo message = "Available targets are:"/>
      <echo message = ""/>
      <echo message = "deploy    --> Deploy application as directory"/>
      <echo message = "deploywar --> Deploy application as a WAR file"/>
      <echo message = ""/>
   </target>
   <target name = "build" description = "Compile main source tree java files">
      <mkdir dir = "${build.dir}"/>
      <javac destdir = "${build.dir}" source = "1.5" target = "1.5" debug = "true"
         deprecation = "false" optimize = "false" failonerror = "true">
         <src path = "${src.dir}"/>
         <classpath refid = "master-classpath"/>
      </javac>
   </target>
   <target name = "deploy" depends = "build" description = "Deploy application">
      <copy todir = "${deploy.path}/${name}" preservelastmodified = "true">
         <fileset dir = "${web.dir}">
            <include name = "**/*.*"/>
         </fileset>
      </copy>
   </target>
   <target name = "deploywar" depends = "build" description = 
      "Deploy application as a WAR file">
      <war destfile = "${name}.war" webxml = "${web.dir}/WEB-INF/web.xml">
         <fileset dir = "${web.dir}">
            <include name = "**/*.*"/>
         </fileset>
      </war>
      <copy todir = "${deploy.path}" preservelastmodified = "true">
         <fileset dir = ".">
            <include name = "*.war"/>
         </fileset>
      </copy>
   </target>
   <target name = "clean" description = "Clean output directories">
      <delete>
         <fileset dir = "${build.dir}">
            <include name = "**/*.class"/>
         </fileset>
      </delete>
   </target>
</project>

在这个例子中 -

  • 我们首先在构建属性文件中将Tomcat中webapps文件夹的路径声明为deploy.path变量。

  • 我们还在src.dir变量中声明了java文件的源文件夹。

  • 然后我们在web.dir变量中声明web文件的源文件夹。 javadoc.dir是用于存储java文档的文件夹, build.dir是用于存储构建输出文件的路径。

  • 然后我们声明Web应用程序的名称,在我们的例子中是fax

  • 我们还定义了主类路径,其中包含项目的WEB-INF/lib文件夹中存在的JAR文件。

  • 我们还在master类路径中包含build.dir中存在的类文件。

  • Javadoc目标生成项目所需的javadoc,使用目标用于打印构建文件中存在的公共目标。

上面的示例显示了两个部署目标: deploydeploywar.

部署目标将文件从Web目录复制到deploy目录,同时保留上次修改的日期时间戳。 在部署到支持热部署的服务器时,这非常有用。

clean目标清除所有以前构建的文件。

deploywar目标构建war文件,然后将war文件复制到应用程序服务器的deploy目录。