Ant与批处理(win环境)学习笔记(2)

    技术2025-11-16  8

    在《Ant与批处理(win环境)学习笔记》中学习了Ant的一些基础知识,这期继续深入学习 ——————————Ant常用task———————————————————————— 1、使用classPath    <target> <javac> <classpath refid="project.class.path"/> </javac> </target> 2、设置classpath <classpath id="project.class.path"> <pathelement path="${classpath}"/> <fileset dir="lib"> <include name="**/*.jar"/> </fileset> <pathelement location="classes"/> <dirset dir="build"> <include name="apps/**/classes"/> <exclude name="apps/**/*Test*"/> </dirset> <filelist refid="xxx"/> </classpath> 3、 输出信息 输出文本信息使用echo;  输出xml使用echoxml; 引入文件使用import 4、 拷贝文件  <copy file="myfile.txt" tofile="yourfile.txt"/> copy的属性有: file、 dir、fileset等; copydir:拷贝目录 5、 删除文件 <delete file="xxx"/> delete属性有file、dir deletetree:删除文件目录树 6、 剪切文件 <move todir="xxx">   //被剪切的东西 </move> 7、 重命名 <rename src="xxx" dest="yyyy"/> 8、建立临时文件 <tempfile property="xxx" destDir="yyy" suff=".xml"/> 9、 touch的使用 <touch file="myfile" datetime="xxx"/> 10、 Condition的使用[color=blue][/color] 有<and>、<or>、<not>等tag,示例如下: <condition property="isLinuxButNotRedHat"> <and> <os family="Linux"/> <not> <os family="RedHat"/> </not> </and> </condition> 11、 替换replace replace、replacefilter 12、 调用chmod 示例: <chmod perm="go-rwx" type="file"> <fileset dir="/web"> <include name="xxx"/> <exclude name="yyy"/> </fileset> </chmod> 13、 设置Property 有如下一些情况: 设置属性name-value;读取属性文件中的配置:<property file="xxx.properties"/> 读取网络中的property-set: <property url="xxx..."/>;读取环境变量:<property environmen="xxx"/> 13、 建立目录 <mkdir dir="xxx"/> 14、 打jar包 <jar destfile="${dist}" basedir="xxx"/> 15、 打ear包 <ear destfile="build/myapp.ear" appxml="src/metadata/application.xm">    <fileset> dir="build" includes="*.jar, *.war"/> </ear> 16、 执行程序 <target name="help">    <exec exectuable="cmd">       <arg value="/c"/>       <arg value="ant.bat"/>       <arg value="-p"/>    </exec> </target> 17、 运行jar包 <java classname="test.Main"> <arg value="-h"/> <classpath> <pathelement location="dist/test.jar"/> <pathelement path="/xxxx/yyy.jar"/> </classpath> </java> 疑问: location与path的区别? path可以用于指向存在多个文件的位置,而location只能指向单个的文件或目录。另外path可以被设定id,供其它的path或classpath引用。如:<path id="main-classpath">,而location则没有。 18、 制作Javadoc ... ... 19、 定义一个新的task类库    <taskdef name="myjavadoc" classname="xxxxx"/> 20、 运行sql     <sql> driver="xxxx" url="yyy" userid="sa" password="123456"> insert into table test_table values(1,2,3); truncate table some_other_table; </sql> 21、 Filter的使用 <filter token="xxx" value="yyy"/> 22、还有一些常用的task如发送邮件、解压缩   例子略去... ... 23、 antcall 调用target,DoSomethingelse: <target name=default"> <antcall target="doSomethingelse"> <param name="param1" value="value"/> </antcall> <target name="doSomethingelse"> <echo message="Hello World"/> </target> </target> 在target中还可以使用if...else类的控制流程 ———————— Ant中使用CVS的示例———————————— <?xml version="1.0" encoding="UTF-8"?> <project> <project name="cvsroot" value=":pserver:yaoxxxx"/> <project name="basedir" value="xxx"/> <project name="cvs.password" value=":yyyy"/> <project name="cvs.passfile" value="zzzz"/> <target name="initpass"> <cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/> </target> <target name="checkout" depends="initpass"> <cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject" dese="${distdir}" passfile="${cvs.passfile}"/> </target> </project> </xml> Ant的学习就此告一段落,平时多看看开源项目的build.xml,翻翻ant docs。这是一个积累的过程... ... 我积累呀我积累呀!!! —————————— 家庭作业 1、 使用Eclipse集成Ant 2、 使用Ant构建tomcat的源码 3、 使用Ant结合Junit进行自动化测试
    最新回复(0)