用ant+tomcat编译打包jsp文件

本文介绍了如何利用Ant工具在Tomcat环境下将jsp文件编译为class,并打包成jar,以保护源代码并提升访问速度。通过编写build.properties和build.xml配置文件,执行Ant命令后,会生成包含编译结果的文件夹和jar包,只需将必要文件部署到WebRoot/WEB-INF目录下,即可完成无源码的发布。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们在tomcat下打包发布web程序程序时,需要把jsp文件先编译成class文件,这样有利于保护源代码,也可以提高访问速度。
首先我们编写build.properties,设置一些基本路径:

tomcat.home=D:/Tomcat 5.0
java.home
=D:/jdk1.4
#工程目录
project.home
=D:/projects/preference/trunk/code
#web模块目录
webapp.path
=D:/projects/preference/trunk/code/webapp
#code目录
src
=D:/projects/preference/trunk/code/javacode

 

编写好build.properties后,我们编写build.xml.

 

<?xml version="1.0" encoding="GB2312"?>
<project name="Webapp Precompilation" default="all" basedir="."> 
<property file="build.properties"/>
<!-- 设定工程编译临时存放class路径 -->
<property name="dest" value="build/classes.ant"/>
<!-- 设定工程发布后的路径 -->
<property name="webdest" value="build/WebRoot"/>
<!-- 设定工程依赖的jar包 -->
  
<path id="project.class.path">
    
<pathelement location="${java.home}/jre/lib/rt.jar"/>
    
<pathelement location="${java.home}/lib/tools.jar"/>
    
<pathelement location="${dest}"/>
    
<fileset file="${tomcat.home}/bin/*.jar"/> 
    
<fileset file="${tomcat.home}/server/lib/*.jar"/> 
    
<fileset file="${tomcat.home}/common/lib/*.jar"/> 
    
  
</path>
<!-- 编译jsp文件 -->
  
<target name="jspc"> 
  
<taskdef classname="org.apache.jasper.JspC" name="jasper2" > 
      
<classpath id="jspc.classpath"> 
 
    
<pathelement location="${java.home}/jre/lib/rt.jar"/>
    
<pathelement location="${java.home}lib/tools.jar"/>
     
      
<pathelement location="${dest}"/>
        
<fileset dir="${tomcat.home}/bin"> 
          
<include name="*.jar"/> 
        
</fileset> 
        
<fileset dir="${tomcat.home}/server/lib"> 
          
<include name="*.jar"/> 
        
</fileset> 
        
<fileset dir="${tomcat.home}/common/lib"> 
          
<include name="*.jar"/> 
        
</fileset> 
       
      
</classpath> 
    
</taskdef> 
  
<jasper2 javaEncoding="GBK"
             validateXml
="false" 
             uriroot
="${webapp.path}" 
             webXmlFragment
="${project.home}/build/generated_web.xml" 
             outputDir
="${project.home}/build/src" /> 

  
</target> 
 
<target name="jspcompile">

    
<mkdir dir="${project.home}/build/WEB-INF/classes"/>
    
<mkdir dir="${project.home}/build/WEB-INF/lib"/>

    
<javac destdir="${project.home}/build/WEB-INF/classes" srcdir="${project.home}/build/src" fork="true" memoryMaximumSize="512000k"
           debug
="false" deprecation="true" encoding="GBK" nowarn="false" target="1.4">
           
      
      
<classpath>
     
<pathelement location="${java.home}/jre/lib/rt.jar"/>
    
<pathelement location="${java.home}lib/tools.jar"/>
     
      
<pathelement location="${dest}"/>
        
<fileset dir="${tomcat.home}/bin"> 
          
<include name="*.jar"/> 
        
</fileset> 
        
<fileset dir="${tomcat.home}/server/lib"> 
          
<include name="*.jar"/> 
        
</fileset> 
        
<fileset dir="${tomcat.home}/common/lib"> 
          
<include name="*.jar"/> 
        
</fileset> 
        
<pathelement location="${tomcat.home}/common/classes"/>
        
<pathelement location="${tomcat.home}/shared/classes"/>
         
      
</classpath>
      
<include name="**" />
      
<exclude name="tags/**" />
    
</javac>
<jar jarfile="${project.home}/build/jsp.jar" basedir="${project.home}/build/WEB-INF/classes"/>
<jar jarfile="${project.home}/build/code.jar" basedir="${dest}"/>
  
</target>


  
<!-- 初始化任务 -->
  
<target name="init">
    
<delete dir="${dest}"/>
    
<mkdir dir="${dest}"/>
    
<delete dir="${webdest}"/>
    
<mkdir dir="${webdest}"/>
    
<delete dir="${project.home}/build/src"/>
    
<mkdir dir="${project.home}/build/src"/>
    
<delete dir="${project.home}/build/WEB-INF/classes"/>
    
<mkdir dir="${project.home}/build/WEB-INF/classes"/>
    
  
</target>
 
<target name="javacompileonly" depends="init">
    
<!-- 编译java源文件到目标中 -->
    
<echo message="编译java源文件app到目标路径:${dest}"/>
    
<javac bootclasspathref="project.class.path"  debug="true" deprecation="false" destdir="${dest}" encoding="GBK" nowarn="false" target="1.4">
      
<src path="${src}"/>
    
</javac>
  
</target>
  
<!-- 编译java源代码 -->
  
<target name="javacompile" depends="javacompileonly">
    
<!-- 编译java源文件到目标中 -->
    
<echo message="编译java源文件到目标路径:${dest}"/>
    
<!-- 将源代码下的配置文件拷到目标路径 -->
    
<echo message="将源代码下的配置文件拷到目标路径${dest}"/>    
    
<copy todir="${dest}">
      
<fileset dir="${src}">
        
<include name="**/*.*"/>
        
<include name="**/*"/>
        
<exclude name="*.svn/"/>
        
<exclude name="**/*.java"/>
        
<exclude name="**/*.bak"/>
      
</fileset>
    
</copy>
  
</target>
   
  
<!--拷贝资源 文件 -->
  
<target name="webresource" >
    
<echo message="拷贝发布文件目标路径${dest}"/>
    
<copy todir="${webdest}">
      
<fileset dir="${webapp.path}">
        
<include name="**/*.*"/>
        
<include name="**/*"/>
        
<exclude name="*.snv/"/>
        
<exclude name="**/*.jsp"/>
        
<exclude name="**/*.bak"/>
      
</fileset>
    
</copy>
    
<echo message="拷贝lib目标路径${dest}"/>
    
<copy todir="${webdest}/WEB-INF/lib">
      
<fileset dir="${webapp.path}/WEB-INF/lib">
       
<include name="*.jar"/>
        
<exclude name="*.snv/"/>
      
</fileset>
      
    
</copy>
  
</target>
  
<target name="all" depends="init,javacompile,jspc,jspcompile,webresource">
  
</target>

</project>


然后把build.properties和build.xml拷贝到工程目录下,在工程目录下执行ant,编译完成后,
如果成功可以看到产生一个build文件夹,里面有classes.ant,src,WebRoot,WEB-INF四个文件夹,
另外还有code.jar,jsp.jar,和generated_web.xml三个文件。classes.ant,src,WEB-INF是临时文件,
已经没用,code.jar是java代码打成的jar包,jsp.jar是jsp打成的jar包。只须把code.jar,jsp.jar拷到WebRoot/WEB-INF/lib下,然后把generated_web.xml的内容加到
WebRoot/WEB-INF/web.xml里。现在你就可以拿WebRoot去发布了,里面没有任何源程序代码。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值