ANT使用范例
关键字: ANT
1. 把一些常用的方法抽象为一个独立的文件,如下:common.xml
xml 代码
- <path id="classpath">
- <fileset dir="${lib.dir}">
- <include name="*.jar" />
- </fileset>
- <fileset dir="${tomcat.home}/lib">
- <include name="*.jar" />
- </fileset>
- <!-- 指定依赖的源文件编译输出 -->
- <pathelement path="${build.dir}/classes" />
- </path>
- <!-- 设定辅助编译环境 -->
- <path id="buildclasspath">
- <path refid="classpath" />
- <fileset dir="${buildlib.dir}">
- <include name="*.jar" />
- </fileset>
- </path>
- <target name="help" description="is used to display the helping message">
- <echo message="" />
- <echo message="${webapp.name} build file" />
- <echo message="-----------------------------------" />
- <echo message="" />
- <echo message="Available targets are:" />
- <echo message="" />
- <echo message="help --> Print this screen" />
- <echo message="new.......Creates a new project with the specified name" />
- <echo message="clean.....Delete build dir, all .class and war files" />
- <echo message="compile --> Compile all Java files" />
- <echo message="test --> Test all Java files" />
- <echo message="build.........Build war file from .class and other files" />
- <echo message="deploy........Copy war file to the webapps directory" />
- <echo message="javadoc.......Generates javadoc for this application" />
- </target>
- <target name="compile" description="compile the java source and copy the configuration file">
- <mkdir dir="${build.dir}/classes" />
- <javac destdir="${build.dir}/classes" encoding="UTF-8" target="1.5" source="1.5" debug="true" deprecation="true" optimize="false" failonerror="true">
- <src path="${src.dir}" />
- <classpath refid="classpath" />
- </javac>
- <!-- Copy hibernate mapping files to ${build.dir}/classes -->
- <copy todir="${build.dir}/classes">
- <fileset dir="${src.dir}" includes="**/*.hbm.xml" />
- </copy>
- <copy todir="${build.dir}/classes">
- <fileset dir="${etc.dir}/classes" includes="**/*.xml" />
- <fileset dir="${etc.dir}/classes" includes="**/*.properties">
- <exclude name="**/*_zh.properties" />
- </fileset>
- </copy>
- <native2ascii dest="${build.dir}/classes" encoding="utf-8" src="${etc.dir}/classes" includes="**/*_zh.properties">
- </native2ascii>
- </target>
- <target name="clean" description="clean up any traces of the application">
- <delete dir="${build.dir}" />
- <mkdir dir="${build.dir}"/>
- <delete dir="${deploy.dir}" />
- <mkdir dir="${deploy.dir}"/>
- <!-- can't delete directory if Tomcat is running -->
- <delete dir="${tomcat.home}/webapps/${webapp.name}" failonerror="false" />
- <!-- deleting the deployed .war file is fine even if Tomcat is running -->
- <delete dir="${tomcat.home}/webapps/${webapp.name}.war" />
- <!-- delete the javadoc -->
- <delete dir="${doc.dir}" />
- <mkdir dir="${doc.dir}"/>
- </target>
- <target name="build" depends="compile" description="copy the project to the deploy and war!">
- <copy todir="${deploy.dir}/${webapp.name}" preservelastmodified="true">
- <fileset dir="${web.dir}">
- <include name="**/*.*" />
- <exclude name="**/readme.txt" />
- </fileset>
- </copy>
- <!-- Copy the class file -->
- <copy todir="${deploy.dir}/${webapp.name}/WEB-INF/classes" preservelastmodified="true">
- <fileset dir="${build.dir}/classes">
- <exclude name="*.xml" />
- <exclude name="test" />
- <exclude name="hibernate.properties" />
- </fileset>
- </copy>
- <!-- Copy configuration file -->
- <copy todir="${deploy.dir}/${webapp.name}/WEB-INF" preservelastmodified="true">
- <fileset dir="${etc.dir}" >
- <exclude name="classes/*.*" />
- </fileset>
- </copy>
- <!-- Create the <war> file -->
- <jar jarfile="${deploy.dir}/${webapp.name}.war" basedir="${deploy.dir}" />
- </target>
- <target name="deploy" depends="build" description="copies the war into the Tomcat webapp directory">
- <!-- Copy the contents of the build directory -->
- <copy todir="${tomcat.home}/webapps" file="${deploy.dir}/${webapp.name}.war" />
- </target>
- <target name="new" description="creates a new project with the specified name">
- <echo level="info">
- +-------------------------------------------------------------+
- | -- Welcome to the AppDemo New Application Wizard! -- | | |
- | To create a new application, please answer the following |
- | questions. |
- +-------------------------------------------------------------+
- </echo>
- <echo />
- <!-- Prompt user for input -->
- <input message="What would you like to name your application [myapp]?" addproperty="app.name" defaultvalue="myapp" />
- <echo level="info">
- Creating new application named '${app.name}'...
- </echo>
- <copy todir="../${app.name}">
- <fileset dir="${basedir}">
- <exclude name="**/*.*" />
- <include name="**" />
- <include name="${basedir}/*.*" />
- </fileset>
- </copy>
- <!-- replace app name -->
- <replaceregexp flags="g">
- <!-- pattern="正则表达式" -->
- <regexp pattern="message" />
- <!-- expression="将要替换的值" -->
- <substitution expression="${app.name}" />
- <fileset dir="../${app.name}">
- <include name="**" />
- <exclude name="**/*.jar" />
- </fileset>
- </replaceregexp>
- </target>
- <target name="javadoc" depends="compile" description="this task creates javadoc">
- <mkdir dir="${doc.dir}/api" />
- <javadoc sourcepath="${src.dir}" charset="utf-8" encoding="utf-8" destdir="${doc.dir}/api" packagenames="${javadoc.pkg.top}.*" />
- </target>
2. 每个要使用这个方法的项目都可调用共用文件里的方法,如下范例:build.xml
xml 代码
- <property name="webapp.name" value="work" />
- <import file="common.xml" />
- <taskdef name="xdoclet2" classname="org.xdoclet.ant.XDocletTask" classpathref="buildclasspath"/>
- <target name="hibernate" description="Generate mapping documents">
- <xdoclet2>
- <fileset dir="${src.dir}">
- <include name="cn/janwer/model/*.java" />
- fileset>
- <component
- classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin"
- version="3.0" destdir="${src.dir}" />
- <component
- classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin"
- version="3.0"
- jdbcurl="jdbc:mysql://localhost:3306/work?useUnicode=true&characterEncoding=utf8&autoReconnect=true&autoReconnectForPools=true"
- jdbcdriver="com.mysql.jdbc.Driver"
- dialect="org.hibernate.dialect.MySQLDialect" jdbcusername="root"
- jdbcpassword="junwei" hbm2ddlauto="update" showsql="true"
- properties="${etc.dir}/hibernate.properties"
- destdir="${src.dir}" />
- xdoclet2>
- target>
- <taskdef name="xdoclet" classname="xdoclet.DocletTask" classpathref="buildclasspath"/>
- <taskdef name="webdoclet" classname="xdoclet.modules.web.WebDocletTask" classpathref="buildclasspath"/>
- <target name="struts" description="Generate struts configuration">
- <delete file="${etc.dir}/config/struts-config.xml" />
- <delete file="${etc.dir}/config/validation.xml" />
- <xdoclet destdir="${src.dir}" excludedtags="@version,@author"
- verbose="false">
- <fileset dir="${src.dir}">
- <include name="cn/janwer/model/*.java" />
- fileset>
- <actionform>
- <packageSubstitution packages="model"
- substituteWith="form" />
- actionform>
- xdoclet>