ant 的常用语法和功能

1. <property file="build.properties"/>

 

把build.properties文件里的键值对导入到build.xml ,以后就可以在build.xml 里使用${db.driver}来读到build.properties里配置的值org.hsqldb.jdbcDriver
这个很有用,需要改变值的时候,只需改变build.properties的值,但build.xml文件不用修改

db.url=jdbc:hsqldb:hsql://localhost/training
db.driver=org.hsqldb.jdbcDriver
db.username=sa
db.password=
hibernate.dialect=net.sf.hibernate.dialect.HSQLDialect

 2.<path id="ant.lib.path"></path>

指定了一个路径,路径下放着指定的jar文件

3. <path location="${classes.dir}"/>

指定了一个路径里的所有文件

4. <fileset dir="${xdoclet.lib.dir}">
      <include name="*.jar"/>
    </fileset>

这个表示把路径${xdoclet.lib.dir}里的所有的.jar文件包括进来,不包括子文件夹里的.jar文件

如果用这个 <include name="**/*.jar"/>,表示包括这个文件夹里所有的 .jar文件,包括所有子文件夹里的.jar文件

5.<copy toDir="${depends.dir}">
      <mapper type="flatten"/>
      <fileset refid="runtime.fileset"/>
    </copy>

表示将id名为runtime.fileset的<fileset >里包括的文件去掉文件夹copy到${depends.dir}

6. 打包工程

 <war destfile="${war.file}" webxml="${webxml.file}">
      <lib dir="${lib.dir}" includes="*.jar"/>
      <lib dir="${depends.dir}" includes="*.jar"/>
     
      <classes dir="${classes.dir}"/>
      <classes dir="${hbm.dir}"/>
      <webinf dir="${webinf.dir}" excludes="web.xml"/>
      <webinf dir="${target.dir}/webapp/WEB-INF"/>
      <fileset dir="${web.src.dir}" includes="index.html,jsp/**"/>
    </war>

destfile 在指定位置创造了一个.war文件

webxml 将指定位置的web.xml 放在webapp/WEB-INF里

<lib  将所包含的jar文件放在WEB-INF/lib

<classes 将所包含的文件放在WEB-INF/classes

<webinf   将指定路径的所有文件,除了web.xml,全部都copy到WEB-INF/里

<fileset  将指定路径的所有文件全部都copy到WEB-INF/的父目录,则和WEB-INF在同一个目录

7.用于将build.properties文件里的值来修改hibernate.properties里的属性值,动态生成一个hibernate.properties文件

<filter token="db.url" value="${db.url}"/>

<copy todir="${target.dir}/webapp/WEB-INF"
        file="${config.dir}/hibernate.properties"
        filtering="true"
        overwrite="true"/>

 

表示但把  file指定路径下的hibernate.properties文件copy到指定路径时,将会执行一个动作,在hibernate.properties文件里所有出现的这个字符串(token指定的字符串)将被value指定的值所替换

 

 

<?xml version="1.0"?>
<!-- ====================================================================== 
     Aug 19, 2004 7:18:13 PM                                                        

     Spring Training    
     The Spring Training application from Spring in Action
                   
     Craig Walls                                             
     Ryan Breidenbach              
     ====================================================================== -->
<project name="SpringTraining" default="war">
  
  <description>
    The Spring Training application from Spring in Action
  </description>
  
  <property name="target.dir" location="target"/>
  <property name="src.dir" location="src"/>
  <property name="java.src.dir" location="${src.dir}/java/main"/>
  <property name="test.src.dir" location="${src.dir}/java/test"/>
  <property name="web.src.dir" location="${src.dir}/webapp"/>
  <property name="webinf.dir" location="${web.src.dir}/WEB-INF"/>
  <property name="classes.dir" location="${target.dir}/classes"/>
  <property name="lib.dir" location="lib"/>
  <property name="war.file" location="${target.dir}/${ant.project.name}.war"/>
  <property name="webxml.file" location="${webinf.dir}/web.xml"/>
  <property name="depends.dir" location="${target.dir}/dependencies"/>
  <property name="xdoclet.lib.dir" location="xdocletlib"/>
  <property name="hbm.dir" location="${target.dir}/hbm"/>
  <property name="config.dir" location="config"/>
  
  <property file="build.properties"/>
<!-- 指定了一个路径,路径下放着指定的jar文件   --> 
  <path id="ant.lib.path">
    <fileset dir="${xdoclet.lib.dir}">
      <include name="*.jar"/>
    </fileset>
    <fileset dir="${spring.home}/lib">
      <include name="**/*.jar"/>
    </fileset>
    <fileset dir="${lib.dir}" includes="hsqldb*.jar"/>
    <path location="${classes.dir}"/> 
  </path>
  
  <fileset id="runtime.fileset" dir="${spring.home}/lib">
    <include name="aopalliance/aopalliance.jar" />
    <include name="hibernate/*.jar" />
    <include name="log4j/*.jar" />
    <include name="jakarta-commons/commons-logging.jar" />
    <include name="jakarta-commons/commons-lang.jar" />
    <include name="jakarta-commons/commons-beanutils.jar" />
    <include name="jakarta-commons/commons-digester.jar" />
    <include name="cglib/*.jar" />
    <include name="dom4j/*.jar" />
    <include name="j2ee/jta.jar" />
    <!--
    <include name="j2ee/jstl.jar" />
    <include name="jakarta-taglibs/*.jar" />
    -->
    <include name="itext/*.jar" />
    <include name="poi/*.jar" />
    <include name="oro/*.jar" />
    <include name="caucho/*.jar" />
  </fileset>

  <!-- - - - - - - - - - - - - - - - - - 
        target: copy dependency jars 
       - - - - - - - - - - - - - - - - - -->
  <target name="-copyDependencies">
    <mkdir dir="${depends.dir}"/>
    <copy toDir="${depends.dir}">
      <mapper type="flatten"/>
      <fileset refid="runtime.fileset"/>
    </copy>
  </target>
  
  <!-- ================================= 
        target: default              
       ================================= -->
  <target name="war" depends="-copyDependencies,compile,generate-hbm,config" 
      description="--> Build the WAR file">
    <war destfile="${war.file}" webxml="${webxml.file}">
      <lib dir="${lib.dir}" includes="*.jar"/>
      <lib dir="${depends.dir}" includes="*.jar"/>
      
      <classes dir="${classes.dir}"/>
      <classes dir="${hbm.dir}"/>
      <webinf dir="${webinf.dir}" excludes="web.xml"/>
      <webinf dir="${target.dir}/webapp/WEB-INF"/>
      <fileset dir="${web.src.dir}" includes="index.html,jsp/**"/>
    </war>
  </target>

  <!-- - - - - - - - - - - - - - - - - - 
        target: generate hbm files
       - - - - - - - - - - - - - - - - - -->
  <target name="config">
    <filter token="db.url" value="${db.url}"/>
    <filter token="db.driver" value="${db.driver}"/>
    <filter token="db.username" value="${db.username}"/>
    <filter token="db.password" value="${db.password}"/>
    <filter token="hibernate.dialect" value="${hibernate.dialect}"/>
    <filter token="logfile.path" value="${logfile.path}"/>

    <mkdir dir="${target.dir}/webapp/WEB-INF/classes"/> 

    <copy todir="${target.dir}/webapp/WEB-INF" 
        file="${config.dir}/hibernate.properties"
        filtering="true"
        overwrite="true"/>
    
    <copy todir="${target.dir}/webapp/WEB-INF/classes"
        file="${config.dir}/log4j.properties"
        filtering="true"
        overwrite="true"/>
  </target> 

  <!-- - - - - - - - - - - - - - - - - - 
        target: generate hbm files
       - - - - - - - - - - - - - - - - - -->
  <target name="generate-hbm" depends="compile">
    <mkdir dir="${hbm.dir}"/>
    
    <taskdef name="hibernatedoclet" 
        classname="xdoclet.modules.hibernate.HibernateDocletTask"
        classpathref="ant.lib.path" />

    <hibernatedoclet destdir="${hbm.dir}">
      <fileset dir="${java.src.dir}">
        <include name="**/*.java" />
      </fileset>

      <hibernate version="2.0"/> 
    </hibernatedoclet>
  </target>

  <!-- - - - - - - - - - - - - - - - - - 
        target: generate database schema
       - - - - - - - - - - - - - - - - - -->
  <target name="schema" depends="generate-hbm,config">
   
    <taskdef name="schemaexport"
        classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
        classpathref="ant.lib.path"/>

    <schemaexport properties="${target.dir}/webapp/WEB-INF/hibernate.properties" 
        quiet="yes" 
        text="no" 
        drop="no" 
        delimiter=";" 
        output="target/schema.sql"> 

      <fileset dir="${hbm.dir}"> 
        <include name="**/*.hbm.xml"/> 
      </fileset> 
    </schemaexport>  
  </target>
    
  <!-- - - - - - - - - - - - - - - - - - 
        target: setup database
       - - - - - - - - - - - - - - - - - -->
  <target name="setup_db" depends="schema">  
    <sql driver="${db.driver}"
         url="${db.url}"
         userid="${db.username}"
         password="${db.password}"
         src="setup.sql"
         print="yes">
      <classpath>
        <pathelement location="lib/hsqldb.jar"/>
      </classpath>
    </sql>
  </target> 
  
  
  <!-- - - - - - - - - - - - - - - - - - 
        target: start Hypersonic                      
       - - - - - - - - - - - - - - - - - -->
  <target name="hsql">
    <echo message="Starting HSQLDB"/>
    <java fork="true" dir="." classname="org.hsqldb.Server">
      <classpath>
        <pathelement path="${lib.dir}/hsqldb.jar"/>
      </classpath>
      <arg value="-database.0"/>
      <arg value="${hsql.path}"/>
      <arg value="-dbname.0"/>
      <arg value="${hsql.name}"/>
    </java>
  </target>
  
  <!-- - - - - - - - - - - - - - - - - - 
        target: compile                      
       - - - - - - - - - - - - - - - - - -->
  <target name="compile">
    <mkdir dir="${classes.dir}"/>
    <javac srcdir="${java.src.dir}" destdir="${classes.dir}">
      <classpath>
        <pathelement path="${classpath}"/>
        <fileset dir="${xdoclet.lib.dir}">
          <include name="**/*.jar"/>
        </fileset>
        <fileset dir="${spring.home}/lib">
          <include name="**/*.jar"/>
        </fileset>
        <pathelement location="${spring.home}/dist/spring.jar"/>
      </classpath>
    </javac>
  </target>
    
  <!-- - - - - - - - - - - - - - - - - - 
          target: deploy web application
         - - - - - - - - - - - - - - - - - -->
  <target name="deploy" depends="war">
    <copy file="${war.file}" todir="${deploy.target}"/>
  </target>
  
  <!-- - - - - - - - - - - - - - - - - - 
        target: clean                      
       - - - - - - - - - - - - - - - - - -->
  <target name="clean">
    <delete dir="${target.dir}"/>
  </target>
  
  
  <target name="zipItUp" depends="clean">
    <delete file="${target.dir}/${ant.project.name}.zip"/>
    <mkdir dir="${target.dir}"/>    

    <zip destfile="${target.dir}/${ant.project.name}.zip">
      <zipfileset dir="." prefix="${ant.project.name}">
        <include name="**"/>
        <exclude name="bin/**"/>
        <exclude name="target/**"/>
        <exclude name="todo.txt"/>
        <exclude name="src/webapp/WEB-INF/freemarker/**"/>
        <exclude name="src/webapp/WEB-INF/velocity/**"/>
      </zipfileset>
    </zip>
  </target>
</project>

 

 

你可能感兴趣的:(spring,Hibernate,Web,ant,HSQLDB)