鐵手劍譜

          上善若水
          數據加載中……
          Struts秘籍之起式:第1.7式:使用Ant進行構建和部署

          第1.7式. 使用Ant 進行構建和部署

          問題

          你希望能夠以一種能夠重復和可移動的情況下構建和部署Struts 應用。

          動作要領

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

          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>

           

          動作變化

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

          基于Ant的構建可以幫助你完成各種開發任務:

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

          通過使用構建腳本來創建WAR 文件,你可以根據你覺得合適的方式來對源代碼和Struts 奮發的物理文件位置進行結構化。然后你可以使用Ant 腳本將它們整合到一起。

          相關招式

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

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

          評論

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

          本系列源改編自O'Reily的Strus Cookbook
          [引用提示]阿泠引用了該文章, 地址: http://blog.donews.com/inclear/archive/2005/11/12/624363.aspx
            回復  更多評論    
          主站蜘蛛池模板: 土默特左旗| 冷水江市| 贡嘎县| 潍坊市| 桦甸市| 乡城县| 武定县| 小金县| 剑川县| 佛山市| 苍溪县| 宁德市| 呼图壁县| 深州市| 五家渠市| 达孜县| 北安市| 金秀| 乐昌市| 南开区| 田东县| 阿拉尔市| 栖霞市| 河津市| 衢州市| 遵义县| 淳化县| 本溪市| 锡林浩特市| 清丰县| 鹤庆县| 额敏县| 娱乐| 文化| 手游| 清新县| 广元市| 民县| 苏州市| 达日县| 九龙坡区|