zoukankan      html  css  js  c++  java
  • ant使用小结

    使用builder.xml的方式;
    完成的工作:打jar包并运行,其中引用了第三方jar和配置文件;

    <?xml version="1.0" encoding="UTF-8"?>
    <project name="InternetBusinessSurfer" default="run" basedir=".">
    <property name="src" value="src"></property>
    <property name="dest" value="classes" />
    <property name="lib" value="lib"></property>
    <property name="jarname" value="InternetBusinessSurfer.jar"></property>
    
    <target name="init" >
    <mkdir dir="${dest}"></mkdir>
    </target>
    
    <target name="compile" depends="init">
    <javac srcdir="${src}" destdir="${dest}" debug="true" includeantruntime="on">
    <classpath>
    <fileset dir="${lib}" includes="*.jar"></fileset>
    </classpath>
    </javac>
    </target>
    
    <target name="build" depends="compile">
    <jar jarfile="${jarname}" basedir="${dest}">
    </jar>
    </target>
    
    <target name="run" depends="build">
    <java classname="com.control.Main" classpath="${jarname}">
    <classpath>
    <fileset dir="${lib}" includes="*.jar"></fileset>
    <pathelement location="${src}"></pathelement>
    </classpath>
    </java>
    </target>
    
    <target name="clean">
    <delete dir="${dest}"></delete>
    </target>
    
    <target name="rerun" depends="clean,run">
    <ant target="clean"></ant>
    <ant target="run" ></ant>
    </target>
    
    </project>

    解析:

    <?xml version="1.0" encoding="UTF-8"?> //xml文件的定义
    <project name="InternetBusinessSurfer" default="run" basedir="."> 
    </project>//ant的所有内容必须包含在这个里边,name是你给它取的名字,basedir故名思意就是工作的根目录 .代表当前目录。default代表执行的target,必填项。
    <property name="src" value="src"></property> //定义的变量,在后面的target中写物理路径也行,但不方便修改
    
    <target name="init" > //定义name,其它target引用时使用
    <mkdir dir="${dest}"></mkdir> //建立一个文件夹
    </target> //一个任务


    <target name="compile" depends="init"> 
    <javac srcdir="${src}" destdir="${dest}" debug="true" includeantruntime="on">
     //includeantruntime,不指定会有提示:warning: 'includeantruntime' was not set,设置引属性后ant输出ant用到的时间。eg:Total time: 1 second
    <classpath> 
    <fileset dir="${lib}" includes="*.jar"></fileset> 
    </classpath> //classpath是ant内置的关键字,用来指定程序编译用到的jar,即claspath。如果引用第三方jar,不设置会编译失败
    </javac> 
    </target> 

    <target name="compile" depends="init"> :depends是它所依赖的target,在执行这个target 例如这里的compile之前ant会先检查init是否曾经被执行过,如果执行
    过则直接直接执行compile,如果没有则会先执行它依赖的target例如这里的init,然后在执行这个target


    <target name="run" depends="build"> 
    <java classname="com.control.Main" classpath="${jarname}"> 
    <classpath> 
    <fileset dir="${lib}" includes="*.jar"></fileset> 
    <pathelement location="${src}"></pathelement>
    </classpath> 
    </java> 
    </target> 

    加载配置文件时,下面写法会报错:

    <classpath>
    <fileset dir="${lib}" includes="*.jar"></fileset>
    <fileset dir="${src}">
    <include name="*.xml"></include>
    <include name="*.properties"></include>
    </fileset>
    </classpath>

    报错信息:Unable to obtain resource from x:javaworkspaceInternetBusinessSurfersrclog4j.properties: java.util.zip.ZipException: error in opening zip file

    解决方法是使用pathelement元素:

    <classpath> 
    <fileset dir="${lib}" includes="*.jar"></fileset> 
    <pathelement location="${src}"></pathelement>
    </classpath> 


    以下两个target不是必须的:
    <target name="clean">
    <delete dir="${dest}"></delete>
    </target>//删除生成的文件
    
    <target name="rerun" depends="clean,run">
    <ant target="clean"></ant>
    <ant target="run" ></ant>
    </target>//再次运行,在一个target里边调用其他的target
     
    web项目的:
    <?xml version="1.0"?>
    <project name="antwebproject"  default="war"basedir=".">
     <property name="classes" value="build/classes"/>
        <property name="build"value="build"/>
        <property name="lib"value="WebRoot/WEB-INF/lib"/>
        <!-- 删除build路径-->
        <target name="clean">
           <delete dir="build"/>
        </target>
    
        <!-- 建立build/classes路径,并编译class文件到build/classes路径下-->
        <target name="compile" depends="clean">
           <mkdir dir="${classes}"/>
    
           <javac srcdir="src" destdir="${classes}"/>
        </target>
    
        <!-- 打war包-->
        <target name="war" depends="compile">
    <war destfile="${build}/antwebproject.war" webxml="WebRoot/WEB-INF/web.xml">
               <!-- 拷贝WebRoot下除了WEB-INF和META-INF的两个文件夹-->
        <fileset dir="WebRoot" includes="**/*.jsp"/>
    
               <!-- 拷贝lib目录下的jar包-->
               <lib dir="${lib}"/>
               <!-- 拷贝build/classes下的class文件-->
               <classesdir="${classes}"/>
           </war>
        </target>
    </project>

    http://reiz6153.blog.163.com/blog/static/4010891520119251583163/
    http://www.cnblogs.com/xwdreamer/archive/2011/11/23/2296925.html
    http://www.cnblogs.com/wufengxyz/archive/2011/11/24/2261797.html
    http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html
    http://www.cnblogs.com/ilahsa/archive/2012/09/07/2674733.html
    http://blog.csdn.net/sunmoon631/article/details/6703949
    http://www.cnblogs.com/zdz8207/p/jva-eclipse-ant.html

  • 相关阅读:
    nsq 启动流程讲解
    nsq 初识
    【资料】http包接口和结构体
    http包详解 2
    http包详解 1
    openstack多节点部署运维
    一款简单实用的串口通讯框架(SerialIo)
    ~MySQL Perfect~
    linux创建用户设置密码
    linux安装tomcat且配置环境变量
  • 原文地址:https://www.cnblogs.com/softidea/p/3337178.html
Copyright © 2011-2022 走看看