鐵手劍譜

          上善若水
          數(shù)據(jù)加載中……
          Struts秘籍之起式:第1.7式:使用Ant進行構(gòu)建和部署

          第1.7式. 使用Ant 進行構(gòu)建和部署

          問題

          你希望能夠以一種能夠重復和可移動的情況下構(gòu)建和部署Struts 應(yīng)用。

          動作要領(lǐng)

          可以創(chuàng)建一個Ant (http://ant.apache.org) 構(gòu)建腳本并使用Ant (或者你的 IDE集成的Ant ) 來編譯、測試、打包和部署你的應(yīng)用。Example 1-8是一個樣板的Ant build 文件,可以編譯、構(gòu)建和部署Struts 應(yīng)用

          Example 1-8. 樣板Ant build 文件

           

           

          <project name="jsc-ch01-r02" default="dist" basedir=".">
            
          <description>
                Jakarta Struts Cookbook - Ant Template
            
          </description>

            
          <!-- Enable access to environment variables -->
            
          <property environment="env"/>

            
          <!-- Set to use JDK 1.4 -->
            
          <property name="build.compiler" value="javac1.4"/>

            
          <!-- set global properties for this build -->
            
          <property name="src.dir" location="src"/>
            
          <property name="build.dir" location="build"/>
            
          <property name="dist.dir"  location="dist"/>
            
          <property name="server.dir" location="${env.CATALINA_HOME}"/>
            
          <property name="servlet.jar" 
               location
          ="${server.dir}/common/lib/servlet-api.jar"/>
            
          <property name="jsp.jar" location="${server.dir}/common/lib/jsp-api.jar"/>
            
          <property name="struts.dist.dir" location="c:/jakarta-struts-1.1/lib"/>

            
          <!-- Struts -->
            
          <fileset id="struts.lib.files" dir="${struts.dist.dir}">
                 
          <include name="**/*.jar"/>
            
          </fileset>
            
          <path id="struts.classpath">
                 
          <fileset refid="struts.lib.files"/>
            
          </path>

            
          <path id="project.class.path">
              
          <pathelement location="${servlet.jar}"/>
              
          <pathelement location="${jsp.jar}"/>
              
          <path refid="struts.classpath"/>
            
          </path>

            
          <!-- Deployment Properties -->
            
          <property name="deploy.dir" location="${server.dir}/webapps"/>

            
          <target name="clean"
                  description
          ="clean up" >
              
          <!-- Delete the ${build.dir} and ${dist.dir} directory trees -->
              
          <delete dir="${build.dir}"/>
              
          <delete dir="${dist.dir}"/>
            
          </target>

            
          <target name="init">
              
          <!-- Create the build directory structure used by compile -->
              
          <mkdir dir="${build.dir}"/>
            
          </target>

            
          <target name="compile" depends="init"
                  description
          ="compile the source " >
              
          <!-- Compile the java code from ${src.dir} into ${build.dir} -->
              
          <javac srcdir="${src.dir}" destdir="${build.dir}" debug="on">
                
          <classpath>
                    
          <path refid="project.class.path"/>
                
          </classpath>
              
          </javac>

              
          <copy todir="${build.dir}">
                
          <fileset dir="${src.dir}">
                  
          <include name="**/*.properties"/>
                
          </fileset>
              
          </copy>
            
          </target>

            
          <target name="dist" depends="compile"
                  description
          ="generate the distribution" >

              
          <!-- Create the distribution directory -->
              
          <mkdir dir="${dist.dir}"/>

              
          <!-- Copy the build dir to WEB-INF/classes -->
              
          <mkdir dir="web/WEB-INF/classes"/>
                
              
          <copy todir="web/WEB-INF/classes">
                  
          <fileset dir="${build.dir}"/>
              
          </copy>

              
          <!-- Put everything in ${build} into the war file -->
              
          <war destfile="${dist.dir}/${ant.project.name}.war" 
                   webxml
          ="web/WEB-INF/web.xml">
                
          <fileset dir="web" excludes="**/web.xml"/>
                  
          <webinf dir="web/WEB-INF">
                    
          <include name="*.xml"/>
                    
          <exclude name="web.xml"/>
                  
          </webinf>
                
          <lib dir="web/WEB-INF/lib">
                  
          <include name="${struts.dist.dir}/**/*.jar"/>
                  
          <include name="${struts.dist.dir}/**/*.tld"/>
                
          </lib>
                
          <classes dir="build"/>
              
          </war>
            
          </target>

            
          <!-- Deploy the application by copying it to the deployment directory -->
            
          <target name="deploy" depends="dist"
                     description
          ="deploy to server" >
                
          <unjar src="${dist.dir}/${ant.project.name}.war"
                    dest
          ="${deploy.dir}/${ant.project.name}"/>
            
          </target>

          </project>

           

          動作變化

          上面展示的構(gòu)建文件可以知進行一些最小的修改就可以用于大部分的Struts web 應(yīng)用。你應(yīng)該將project元素的name屬性的值修改為你的應(yīng)用的名稱。項目名稱將用于創(chuàng)建WAR 是所用的名稱,以及應(yīng)用部署時的文件夾名稱。此外,你應(yīng)該將struts.dist.dir屬性的值設(shè)置為Struts 分發(fā)包所安裝的特定的lib目錄。

          基于Ant的構(gòu)建可以幫助你完成各種開發(fā)任務(wù):

          • 從源代碼控制系統(tǒng)(即, CVS)中獲取最新的源代碼
          • 將Struts 應(yīng)用打包為WAR 文件
          • 運行單元測試
          • 使用XDoclet 來從Ant中產(chǎn)生代碼和配置文件
          • 部署Struts應(yīng)用到應(yīng)用服務(wù)器中
          • 預(yù)編譯JSP 文件以檢測轉(zhuǎn)換錯誤

          通過使用構(gòu)建腳本來創(chuàng)建WAR 文件,你可以根據(jù)你覺得合適的方式來對源代碼和Struts 奮發(fā)的物理文件位置進行結(jié)構(gòu)化。然后你可以使用Ant 腳本將它們整合到一起。

          相關(guān)招式

          你可以訪問Ant 的站點獲取更多詳細的信息http://ant.apache.org。另外,Jesse E. Tilly 和Eric M. Burke (O'Reilly)所著的《Ant: The Definitive Guide》也是使用Ant的一個非常棒的參考。第1.8式將展示使用XDoclet 工具來產(chǎn)生Struts-的相關(guān)文件。

          posted on 2005-05-08 09:49 鐵手 閱讀(1752) 評論(1)  編輯  收藏 所屬分類: JavaStruts系列

          評論

          # Struts 秘籍(CookBook)[TrackBack] 2005-11-12 18:29 阿泠

          本系列源改編自O(shè)'Reily的Strus Cookbook
          [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
            回復  更多評論    
          主站蜘蛛池模板: 广州市| 云南省| 兰考县| 五原县| 阿鲁科尔沁旗| 高安市| 静安区| 象州县| 盐山县| 朝阳市| 桂林市| 鄂托克前旗| 神农架林区| 保靖县| 波密县| 灵石县| 密山市| 开原市| 五峰| 五指山市| 仁怀市| 孟州市| 淮北市| 桃园市| 庆阳市| 民县| 吉林省| 蓬溪县| 新绛县| 正安县| 大名县| 黄石市| 敦煌市| 大安市| 浏阳市| 富顺县| 基隆市| 拜泉县| 万安县| 攀枝花市| 阜康市|