`

用ant进行工程管理

阅读更多

1.典型地,一个ant工程脚本如下:

  1. <?xml version="1.0"?>  
  2. <project name="springapp" basedir="." default="usage">  
  3.     <property file="build.properties" />  
  4.     <path id="master-classpath">  
  5.     <target name="usage">  
  6.     <target name="build" description="Compile main source tree java files">  
  7.     <target name="deploy" depends="build" description="Deploy application">  
  8.     <target name="deploywar" depends="build" description="Deploy application as a WAR file">  
  9.     <target name="clean" description="Clean output directories">  
  10.     <target name="undeploy" description="Un-Deploy application">  
  11. </project>  

其中的property属性可以理解为ant在运行期间所要用到的常量,它是ant的基本数据类型,代表了一个String常量,该常量可以采用直接定义的方式,也可以采用将属性配置到一个properties文件中,在上面的这个例子中,我们同时使用了两种方式定义ant运行时的property属性。在ant脚本中如果要使用外部的配置属性就必须指明文件的确切位置及文件名称。如下所示:

xml 代码
  1. <property file="build.properties" />  

其中build.properties 的文件内容如下:

properties 代码
  1. # Ant properties for building the springapp   
  2. appserver.home=D:\\tomcat6   
  3. deploy.path=${appserver.home}/deploy   
  4. tomcat.manager.url=http://localhost:8080/manager   
  5. tomcat.manager.username=admin   
  6. tomcat.manager.password=tomcat  

 
2.path属性为ant在编译整个工程的过程中提供所需的第三方资源包,ant除了拥有基本数据类型外,还提供了一些高级结构以包括一系列的String,其主要有:path/pathelement、fileset以及dirset  由于我们在前面已经定义过了web.dir常量,因此,在这里我们可以直接把他的值拿来使用(同理:build.dir),根据我们的常量设置,${web.dir}/WEB-INF/lib路经的值在ant运行期间会被转换为war/WEB-INF/lib:

xml 代码
  1. <path id="master-classpath">  
  2.    <fileset dir="${web.dir}/WEB-INF/lib">  
  3.        <include name="*.jar" />  
  4.    </fileset>  
  5.    <pathelement path="${build.dir}" />  
  6. </path>  

 
 
 
 
3.接下来就是N多个 target 属性,ant支持定义一个默认target和多个可选target 属性,下面的代码说明了默认使用name为 usage 的 target 属性。

xml 代码
  1. <project name="springapp" basedir="." default="usage">  

 
查看该节点信息,可以看出,在该 target 节点下仅仅定义了一系列的 echo message 信息。我们在这里定义的这些信息仅仅用于提示一些ant当前可用操作(build、deploy、deploywar、clean、undeploy)。

xml 代码
  1. <target name="usage">  
  2.    <echo message="" />  
  3.    <echo message="${appserver.home}      --->the server root (Danlley Wei)" />  
  4.    <echo message="${name} build file" />  
  5.    <echo message="-----------------------------------" />  
  6.    <echo message="" />  
  7.    <echo message="Available targets are:" />  
  8.    <echo message="" />  
  9.    <echo message="build     --> Build the application" />  
  10.    <echo message="deploy    --> Deploy application as directory" />  
  11.    <echo message="deploywar --> Deploy application as a WAR file" />  
  12.    <echo message="clean     --> clean the generated file which is like *.class " />  
  13.    <echo message="undeploy  --> clean all files in web server tomcat" />  
  14.    <echo message="" />  
  15. </target>  


 
4.name为 build 的 target 节点:

xml 代码
  1. <target name="build" description="Compile main source tree java files">  
  2.    <mkdir dir="${build.dir}" />  
  3.    <javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">  
  4.        <src path="${src.dir}" />  
  5.        <classpath refid="master-classpath" />  
  6.    </javac>  
  7. </target>  

 
 
从该节点定义的信息可以看出,ant把整个工程的编译过程分为两步,首先创建编译路经,这里你根本不需要担心该路经是否已经存在,因为如果不存在,ant就会自动创建该工程编译路经

xml 代码
  1. <mkdir dir="${build.dir}" />  

 
 
接下来对工程进行编译,其编译后的文件将存放在destdir="${build.dir}" 下 ,注意这里的target="1.5"  这里指明了使用JDK1.5,当然你还可以使用其它的版本。

xml 代码
  1. <javac destdir="${build.dir}" target="1.5" debug="true" deprecation="false" optimize="false" failonerror="true">  
  2.     <src path="${src.dir}" />  
  3.     <classpath refid="master-classpath" />  
  4. </javac>  

 
5.name为 deploy 的 target 节点,其实就是一个工程的拷贝过程,ant在发现 ${name} 路经不存在后会自动创建该目录,因此你没必要去手动创建一个${name} 目录 :

xml 代码
  1. <target name="deploy" depends="build" description="Deploy application">  
  2.    <copy todir="${deploy.path}/${name}" preservelastmodified="true">  
  3.        <fileset dir="${web.dir}">  
  4.           <include name="**/*.*" />  
  5.        </fileset>  
  6.    </copy>  
  7. </target>  

 
6.name为 deploywar 的 target 节点:

xml 代码
  1. <target name="deploywar" depends="build" description="Deploy application as a WAR file">  
  2.    <war destfile="${name}.war" webxml="${web.dir}/WEB-INF/web.xml">  
  3.        <fileset dir="${web.dir}">  
  4.           <include name="**/*.*" />  
  5.        </fileset>  
  6.    </war>  
  7.    <copy todir="${deploy.path}" preservelastmodified="true">  
  8.        <fileset dir=".">  
  9.           <include name="*.war" />  
  10.        </fileset>  
  11.    </copy>  
  12. </target>  


 
7.name为 clean 的 target 节点:

xml 代码
  1. <target name="clean" description="Clean output directories">  
  2.     <delete>  
  3.         <fileset dir="${build.dir}">  
  4.             <include name="**/*.class"/>  
  5.         </fileset>  
  6.     </delete>  
  7. </target>  


 
8.name为 undeploy 的 target 节点:

xml 代码
  1. <target name="undeploy" description="Un-Deploy application">  
  2.     <delete>  
  3.         <fileset dir="${deploy.path}/${name}">  
  4.             <include name="**/*.*"/>  
  5.         </fileset>  
  6.     </delete>  
  7. </target>  


 

xml 代码
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics