ANT使用范例

关键字: ANT

1. 把一些常用的方法抽象为一个独立的文件,如下:common.xml

xml 代码
  1. <path id="classpath">     
  2.         <fileset dir="${lib.dir}">     
  3.             <include name="*.jar" />     
  4.         </fileset>     
  5.         <fileset dir="${tomcat.home}/lib">     
  6.             <include name="*.jar" />     
  7.         </fileset>     
  8.         <!-- 指定依赖的源文件编译输出  -->     
  9.         <pathelement path="${build.dir}/classes" />     
  10.     </path>     
  11.      
  12.     <!-- 设定辅助编译环境 -->     
  13.     <path id="buildclasspath">     
  14.         <path refid="classpath" />     
  15.         <fileset dir="${buildlib.dir}">     
  16.             <include name="*.jar" />     
  17.         </fileset>     
  18.     </path>     
  19.      
  20.     <target name="help" description="is used to display the helping message">     
  21.         <echo message="" />     
  22.         <echo message="${webapp.name} build file" />     
  23.         <echo message="-----------------------------------" />     
  24.         <echo message="" />     
  25.         <echo message="Available targets are:" />     
  26.         <echo message="" />     
  27.         <echo message="help      --> Print this screen" />     
  28.         <echo message="new.......Creates a new project with the specified name" />     
  29.         <echo message="clean.....Delete build dir, all .class and war files" />     
  30.         <echo message="compile   --> Compile all Java files" />     
  31.         <echo message="test   --> Test all Java files" />     
  32.         <echo message="build.........Build war file from .class and other files" />     
  33.         <echo message="deploy........Copy war file to the webapps directory" />     
  34.         <echo message="javadoc.......Generates javadoc for this application" />     
  35.     </target>     
  36.        
  37.     <target name="compile" description="compile the java source and copy the configuration file">     
  38.         <mkdir dir="${build.dir}/classes" />     
  39.         <javac destdir="${build.dir}/classes" encoding="UTF-8" target="1.5" source="1.5" debug="true" deprecation="true" optimize="false" failonerror="true">     
  40.             <src path="${src.dir}" />     
  41.             <classpath refid="classpath" />     
  42.         </javac>     
  43.      
  44.         <!-- Copy hibernate mapping files to ${build.dir}/classes -->     
  45.         <copy todir="${build.dir}/classes">     
  46.             <fileset dir="${src.dir}" includes="**/*.hbm.xml" />     
  47.         </copy>     
  48.      
  49.         <copy todir="${build.dir}/classes">     
  50.             <fileset dir="${etc.dir}/classes" includes="**/*.xml" />     
  51.             <fileset dir="${etc.dir}/classes" includes="**/*.properties">     
  52.                                 <exclude name="**/*_zh.properties" />     
  53.             </fileset>     
  54.         </copy>     
  55.      
  56.               
  57.         <native2ascii dest="${build.dir}/classes" encoding="utf-8" src="${etc.dir}/classes" includes="**/*_zh.properties">     
  58.         </native2ascii>     
  59.     </target>     
  60.      
  61.     <target name="clean" description="clean up any traces of the application">     
  62.         <delete dir="${build.dir}" />     
  63.         <mkdir dir="${build.dir}"/>     
  64.         <delete dir="${deploy.dir}" />     
  65.         <mkdir dir="${deploy.dir}"/>     
  66.         <!-- can't delete directory if Tomcat is running -->     
  67.         <delete dir="${tomcat.home}/webapps/${webapp.name}" failonerror="false" />     
  68.         <!-- deleting the deployed .war file is fine even if Tomcat is running -->     
  69.         <delete dir="${tomcat.home}/webapps/${webapp.name}.war" />     
  70.         <!-- delete the javadoc -->     
  71.         <delete dir="${doc.dir}" />     
  72.         <mkdir dir="${doc.dir}"/>     
  73.     </target>     
  74.       
  75.     <target name="build" depends="compile" description="copy the project to the deploy and war!">     
  76.               
  77.         <copy todir="${deploy.dir}/${webapp.name}" preservelastmodified="true">     
  78.             <fileset dir="${web.dir}">     
  79.                 <include name="**/*.*" />     
  80.                 <exclude name="**/readme.txt" />     
  81.             </fileset>     
  82.         </copy>     
  83.         <!-- Copy the class file -->     
  84.         <copy todir="${deploy.dir}/${webapp.name}/WEB-INF/classes" preservelastmodified="true">     
  85.             <fileset dir="${build.dir}/classes">     
  86.                 <exclude name="*.xml" />     
  87.                 <exclude name="test" />     
  88.                 <exclude name="hibernate.properties" />     
  89.             </fileset>     
  90.         </copy>     
  91.         <!-- Copy configuration file -->     
  92.         <copy todir="${deploy.dir}/${webapp.name}/WEB-INF" preservelastmodified="true">     
  93.             <fileset dir="${etc.dir}" >     
  94.                 <exclude name="classes/*.*" />     
  95.             </fileset>     
  96.         </copy>     
  97.         <!-- Create the <war> file -->     
  98.         <jar jarfile="${deploy.dir}/${webapp.name}.war" basedir="${deploy.dir}" />     
  99.     </target>      
  100.      
  101.     <target name="deploy" depends="build" description="copies the war into the Tomcat webapp directory">     
  102.         <!-- Copy the contents of the build directory -->     
  103.         <copy todir="${tomcat.home}/webapps" file="${deploy.dir}/${webapp.name}.war" />     
  104.     </target>     
  105.      
  106.           
  107.     <target name="new" description="creates a new project with the specified name">     
  108.         <echo level="info">     
  109.             +-------------------------------------------------------------+      
  110.             | -- Welcome to the AppDemo New Application Wizard! -- | | |      
  111.             | To create a new application, please answer the following |      
  112.             | questions. |      
  113.             +-------------------------------------------------------------+      
  114.         </echo>     
  115.         <echo />     
  116.      
  117.         <!-- Prompt user for input -->     
  118.         <input message="What would you like to name your application [myapp]?" addproperty="app.name" defaultvalue="myapp" />     
  119.      
  120.         <echo level="info">     
  121.             Creating new application named '${app.name}'...      
  122.         </echo>     
  123.         <copy todir="../${app.name}">     
  124.             <fileset dir="${basedir}">     
  125.                 <exclude name="**/*.*" />     
  126.                 <include name="**" />     
  127.                 <include name="${basedir}/*.*" />     
  128.             </fileset>     
  129.         </copy>     
  130.      
  131.         <!-- replace app name -->     
  132.         <replaceregexp flags="g">     
  133.             <!-- pattern="正则表达式" -->     
  134.             <regexp pattern="message" />     
  135.             <!-- expression="将要替换的值" -->     
  136.             <substitution expression="${app.name}" />     
  137.             <fileset dir="../${app.name}">     
  138.                 <include name="**" />     
  139.                 <exclude name="**/*.jar" />     
  140.             </fileset>     
  141.         </replaceregexp>     
  142.      
  143.     </target>      
  144.      
  145.     <target name="javadoc" depends="compile" description="this task creates javadoc">     
  146.         <mkdir dir="${doc.dir}/api" />     
  147.         <javadoc sourcepath="${src.dir}" charset="utf-8" encoding="utf-8" destdir="${doc.dir}/api" packagenames="${javadoc.pkg.top}.*" />     
  148.     </target>    

2. 每个要使用这个方法的项目都可调用共用文件里的方法,如下范例:build.xml

xml 代码

  1. <property name="webapp.name" value="work" />  
  2.     <import file="common.xml" />  
  3.   
  4.     <taskdef name="xdoclet2" classname="org.xdoclet.ant.XDocletTask" classpathref="buildclasspath"/>  
  5.   
  6.     <target name="hibernate" description="Generate mapping documents">  
  7.         <xdoclet2>  
  8.             <fileset dir="${src.dir}">  
  9.                 <include name="cn/janwer/model/*.java" />  
  10.             fileset>  
  11.   
  12.             <component  
  13.                 classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"  
  14.                 version="3.0" destdir="${src.dir}" />  
  15.   
  16.             <component  
  17.                 classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"  
  18.                 version="3.0"  
  19.                 jdbcurl="jdbc:mysql://localhost:3306/work?useUnicode=true&amp;characterEncoding=utf8&amp;autoReconnect=true&amp;autoReconnectForPools=true"  
  20.                 jdbcdriver="com.mysql.jdbc.Driver"  
  21.                 dialect="org.hibernate.dialect.MySQLDialect" jdbcusername="root"  
  22.                 jdbcpassword="junwei" hbm2ddlauto="update" showsql="true"  
  23.                 properties="${etc.dir}/hibernate.properties"  
  24.                 destdir="${src.dir}" />  
  25.         xdoclet2>  
  26.     target>  
  27.   
  28.     <taskdef name="xdoclet" classname="xdoclet.DocletTask" classpathref="buildclasspath"/>  
  29.   
  30.     <taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask" classpathref="buildclasspath"/>  
  31.   
  32.     <target name="struts" description="Generate struts configuration">  
  33.   
  34.         <delete file="${etc.dir}/config/struts-config.xml" />  
  35.         <delete file="${etc.dir}/config/validation.xml" />  
  36.   
  37.         <xdoclet destdir="${src.dir}" excludedtags="@version,@author"  
  38.             verbose="false">  
  39.   
  40.             <fileset dir="${src.dir}">  
  41.                 <include name="cn/janwer/model/*.java" />  
  42.             fileset>  
  43.   
  44.             <actionform>  
  45.                 <packageSubstitution packages="model"  
  46.                     substituteWith="form" />  
  47.             actionform>  
  48.         xdoclet>
原文地址:https://www.cnblogs.com/licheng/p/1326663.html