铁手剑谱

上善若水
数据加载中……
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 铁手 阅读(1739) 评论(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
  回复  更多评论    

只有注册用户登录后才能发表评论。


网站导航: