zoukankan      html  css  js  c++  java
  • 借助Ant工具,实现快速开发

     

    当一个项目大了以后,每次重新编译,打包,运行就会变得复杂、费时、重复,c语言中有make脚本进行这些工作的批量完成,那么在java中,我们可以使用Ant

     

    Ant是一个构建工具,可以完成这些任务:编译java源代码、运行java程序、拷贝文件或目录、将编译完成的类打包等等。

    而且,Ant允许我们按照自己的方式,添加自己的任务(Task),比如通过Xdoclet完成项目中的hibernate文件生成。

     

    直接使用范例,那些基础知识看来枯燥无味,我会把他们放到最后,如果例子看不明白,再去看基础知识也不迟。

     

     

    使用Ant进行:j2ee项目打包发布,hibernate相关文件生成

     

    <?xml version="1.0" encoding="GBK"?>  
    <project name="测试ANT"  basedir=".">  
        <!--资源文件位置-->  
        <property name="src.dir" value="${basedir}/src" />  
        <!--编译的目标位置-->  
        <property name="classes" value="${basedir}/build/classes" />  
        <!--打包的目标位置-->  
        <property name="build" value="${basedir}/build" />  
        <!--Lib的位置-->  
        <property name="lib" value="${basedir}/WebContent/WEB-INF/lib" />  
        <!--tomcat.webapps位置-->  
        <property name="webapps" value="D:/tomcate/apache-tomcat-7.0.39/webapps" />  
        <!--Xdoclet.Home-->  
        <property name="xdoclet.home" value="D:/oa/xdoclet-plugins-1.0.3" />    
              
            <!-- Build classpath 指定定lib所在位置-->  
            <path id="classpath">  
                <fileset dir="${lib}">  
                    <include name="*.jar" />  
                </fileset>  
            </path>  
      
            <!--  删除build 路径-->  
            <target name="删除build 路径">  
                <delete dir="${build}" />  
            </target>  
      
            <!--  建立build/classes 路径,并编译class 文件到build/classes 路径下-->  
            <target name="建立build/classes 路径,并编译class 文件到build/classes 路径下" depends="删除build 路径">  
                <mkdir dir="${classes}" />  
                <javac srcdir="${basedir}/src" destdir="${classes}">  
                    <classpath refid="classpath" />  
                </javac>  
                <copy todir="${classes}">  
                    <fileset dir="${src.dir}">  
                        <include name="**/*.xml" />  
                        <include name="**/*.properties" />  
                    </fileset>  
                </copy>  
      
            </target>  
      
            <!-- 打war 包-->  
            <target name="打war 包" depends="建立build/classes 路径,并编译class 文件到build/classes 路径下">  
                <war destfile="${build}/WebTest1.war" webxml="${basedir}/WebContent/WEB-INF/web.xml">  
      
                    <!--  拷贝WebContent下所有文件-->  
                    <fileset dir="${basedir}/WebContent" />  
      
                    <!--  拷贝lib 目录下的jar 包-->  
                    <lib dir="${lib}" />  
                    <!--  拷贝build/classes 下的class 文件-->  
                    <classes dir="${classes}" />  
      
                </war>  
      
                <delete dir="${classes}" />  
      
            </target>  
      
            <!--发布到本地tomcat-->  
            <target name="发布到本地tomcat" depends="打war 包">  
                <copy file="${build}/WebTest1.war" tofile="${webapps}/WebTest1.war" />  
            </target>  
     
     
      <!-- 下面是借助xdoclet,进行自定义任务,完成hibernate相关文件生成 -->
     <!--
    xdoclet通过对源文件进行扫描,并读取源文件中的注释,然后生成相应的配置文件,因此xdoclet有自己的一套注释规则,详细看文档。
     -->
            <!-- Build classpath -->  
            <path id="xdoclet.task.classpath">  
                <fileset dir="${xdoclet.home}/lib">  
                    <include name="**/*.jar" />  
                </fileset>  
                <fileset dir="${xdoclet.home}/plugins">  
                    <include name="**/*.jar" />  
                </fileset>  
            </path>  
      
            <taskdef name="xdoclet" classname="org.xdoclet.ant.XDocletTask" classpathref="xdoclet.task.classpath" />  
               
            <!--生成Hibernate配置文件-->  
            <target name="生成Hibernate配置文件" depends="生成hibernate映射文件">  
                <xdoclet>  
                    <fileset dir="${src.dir}/com/tch/model">  
                        <include name="**/*.java" />  
                    </fileset>  
                    <component 
    classname="org.xdoclet.plugin.hibernate.HibernateConfigPlugin" 
    destdir="${src.dir}" 
    version="3.0" 
    hbm2ddlauto="update" 
    jdbcurl="jdbc:mysql://127.0.0.1/oa" 
    jdbcdriver="com.mysql.jdbc.Driver" 
    jdbcusername="root" 
    jdbcpassword="123456" 
    dialect="org.hibernate.dialect.MySQLDialect" 
    showsql="true" 
    />  
                </xdoclet>  
            </target>  
              
            <!--生成hibernate映射文件-->  
            <target name="生成hibernate映射文件">  
                <xdoclet>  
                    <fileset dir="${src.dir}/com/tch/model">  
                        <include name="**/*.java" />  
                    </fileset>  
                    <component 
    classname="org.xdoclet.plugin.hibernate.HibernateMappingPlugin" 
    version="3.0" 
    destdir="${src.dir}" />  
                </xdoclet>  
            </target>  
        </project>  



     

     基础

    1、搭建环境

    1. 确保你的机器已经安装了JDK,确保你的path环境变量中包含有java虚拟机的运行程序,确保你的环境变量中有JAVA_HOME变量
    1. 下载ant并解压
    1. 设置ANT_HOME环境变量,指向ant解压根目录
    1. path环境变量中,添加%ANT_HOME%in目录
    2. 打开控制台,运行:ant
    3. 如果能够运行ant(忽略这些异常:Buildfile: build.xml does not exist!),而不是出现诸如命令无法解释的错误,那么你的ant就安装成功了

     

    2、基本元素

    bulid.xml

     

    <?xml version="1.0" encoding="GBK"?>  
    <project name="ant测试" default="getBaseDir"  basedir="D:antTest">  
          
          
        <!--基本元素-->  
        <target name="getBaseDir">  
             <description>    
               the first example!    
            </description>    
            <echo message="hello world!" />  
        </target>  
          
        <target name="targetA" if ="ant.version">    
            <echo message ="Java Version: ${ant.version}"/>    
        </target>        
          
        <target name="targetC"  depends ="targetA" unless ="anotherTarget" >    
            <description>    
                show depends,unless,basedir,if  
            </description>    
            <echo message ="The base dir is: ${basedir}"/>    
        </target>    
          
        <!--property-->  
        <property name="name" value="jim" />  
        <property name="age" value="18" />  
          
        <target name="targetD" >  
            <echo message="name:${name},age:${age}" />  
        </target>  
      
        <!--echo-->  
        <target name="targetEcho">  
            <echo message="Hello,ANT" file="herfile/hello.log" append="true" />   
        </target>  
    </project>  



     

     project 元素是 Ant 构件文件的根元素, Ant 构件文件至少应该包含一个project 元素,否则会发生错误。在每个 project 元素下,可包含多个 target 元素。

    1) name 属性

        用于指定 project 元素的名称。

    2) default 属性

        用于指定 project 默认执行时所执行的target 的名称。

    3) basedir 属性

        用于指定基路径的位置。该属性没有指定时,使用 Ant 的构件文件的附目录作为基准目录。

     

    target 元素

       targetAnt的基本执行单元,它可以包含一个或多个具体的任务。多个target 可以存在相互依赖关系。它有如下属性:

    1) name 属性

        指定 target 元素的名称,这个属性在一个project 元素中是唯一的。我们可以通过指定 target 元素的名称来指定某个 target

    2) depends 属性

        用于描述 target 之间的依赖关系,若与多个target 存在依赖关系时,需要以“,”间隔。 Ant 会依照 depends 属性中 target 出现的顺序依次执行每个 target 。被依赖的 target 会先执行。

    3) if 属性

        用于验证指定的属性是否存在,若不存在,所在 target 将不会被执行。

    4) unless 属性

        该属性的功能与 if 属性的功能正好相反,它也用于验证指定的属性是否存在,若不存在,所在 target 将会被执行。

    5) description 属性

        该属性是关于 target 功能的简短描述和说明。

     

    property 元素

    property元素可看作参量或者参数的定义,project 的属性可以通过 property元素来设定,也可在 Ant 之外设定。若要在外部引入某文件,例如 build.properties 文件,可以通过如下内容将其引入:

    <property file=”build.properties”/>

    property 元素可用作 task 的属性值。在 task 中是通过将属性名放在“ ${”和“ } ”之间,并放在 task 属性值的位置来实现的。

     

    3、常用命令

    build.xml

     

    <?xml version="1.0" encoding="GBK"?>  
    <project name="ant测试" default="getBaseDir"  basedir="D:antTest">  
        <!--常用命令-->  
          
        <!--copy-->  
        <target name="targetCopy">  
            <copy file="myfile/hello.txt" tofile="herfile/copied.txt"/>   
            <copy todir="hisfile"> <fileset dir="myfile"/></copy>   
        </target>  
          
        <!--delete-->  
        <target name="targetDelete">  
            <delete file="myfile/hello.txt" />  
            <delete dir="myfile" />  
            <delete includeEmptyDirs="true">  
                <fileset dir="." includes="**/*.bak" />  
            </delete>  
        </target>  
          
        <!--mkdir-->  
        <target name="targetMkdir">  
            <mkdir dir="/myfile/src/build/classes"/>   
        </target>  
          
        <!--move-->  
        <target name="targetMove">  
            <move file="destfile.txt" tofile="hello.txt"/>   
            <move file="hello.txt" todir="myfile"/>   
            <move todir="herfile"> <fileset dir="myfile"/></move>  
        </target>  
    </project>  

     

    copy命令用来对文件和目录的复制功能。

    delete 命令对文件或目录进行删除

    mkdir 命令创建目录

    move 命令移动文件或目录

     

     

    Ant相当的工具有MavenMaven预设了一些方便的功能,而ant只能自己写,各有自己的优势。就像hibernateIBatishibernate做了一定的封装,方便了,却失去了一定的灵活性。

     

    java的世界太大,有许许多多的工具,为了快速进行开发,我们一定要善假于物也,对众多工具先有个了解,知道有这么回事,需要时才知道去找谁,再即用即学也不迟。

     

     

  • 相关阅读:
    Windows Azure 社区新闻综述(#64 版)
    Eclipse下配置C/C++开发环境
    有你同行,我不会寂寞物联网操作系统Hello China后续开发计划及开发者征集
    ObjectiveC新手推荐《ObjectiveC开发范例代码大全》
    虚拟网络添加跨界连接的新功能
    WebMatrix 3发布了!
    Windows Live最值得期待的功能 FolderShare
    ASP.NET 2.0 两种模式website和web application到底那个好?
    Sonata 1.2.1 发布
    DB2 9 使用拓荒(733 测验)认证指南,第 9 部分: 用户定义的例程(4)
  • 原文地址:https://www.cnblogs.com/pangblog/p/3253583.html
Copyright © 2011-2022 走看看