常用任务(copy,move,delete,mkdir,javac,java,jar)
<?xml version="1.0" encoding="gb2312" ?>
<project name="projectStudy">
<!--输出内置的属性值-->
<target name ="showSelfProperty">
<echo message ="The base dir is: ${basedir}"/>
<echo message ="The base dir is: ${ant.file}"/>
<echo message ="The base dir is: ${ant.version}"/>
<echo message ="The base dir is: ${ant.project.name}"/>
<echo message ="The base dir is: ${ant.java.version}"/>
</target>
<!--targetB任务依赖targetA任务( 当执行B是A先执行)-->
<target name ="targetA" if ="ant.java.version">
<echo message ="Java Version: ${ant.java.version}"/>
</target >
<target name ="targetB" depends ="targetA" unless ="amigo">
<description >
a depend example!
</description >
<echo message ="The base dir is: ${basedir}"/>
</target>
<!--自定义属性,并输出-->
<property name="stuName" value="张三"></property>
<property name="stuAge" value="25"></property>
<target name="DefineMyProperty">
<echo message="sturName=${stuName}"/>
<echo message="stuAge=${stuAge}"/>
</target>
<!--Ant常用任务练习-->
<!--copy 任务,路径默认为build.xml文件同一个目录-->
<target name="copyTarget">
<!--将绝对路径的文件复制到指定的目录,"${basedir}\"可省略(因为是默认值)-->
<copy file="E:\学习笔记\Struts2\10-2.doc" tofile="${basedir}\copy.doc"/>
<!--将一个目录的所有文件,复制到另一个目录-->
<copy todir="E:\学习笔记\AntBackup">
<fileset dir="E:\学习笔记\Ant"/>
</copy>
<!--将与build.xml文件在同一个目录的copy.txt文件,复制到另一个目录-->
<copy file="copy.txt" todir="E:\学习笔记\AntBackup">
</copy>
</target>
<!--delete任务-->
<target name="deleteTarget">
<!--删除指定的文件,不使用绝对路径,表示以${baseDir}为根目录-->
<delete file="copy.txt"/>
<!--删除目录
<delete dir="E:\学习笔记\AntBackup"/>
-->
<!--删除指定目录的,指定类型文件-->
<!--删除E:\学习笔记\AntBacup目录下,所有的doc文件,包括空目录(空目录实际没有删掉,原因不知)-->
<delete includeEmptyDirs="true">
<fileset dir="E:\学习笔记\AntBacup" includes="**/*.doc"/>
</delete>
</target>
<!--move任务-->
<target name="moveTarget">
<!--将copy.doc文件删除后,再复制一份到copy1.doc
<move file="copy.doc" tofile="copy1.doc"/>
-->
<!--将copy1.doc移动到E:\学习笔记\AntBacup
<move file="copy1.doc" todir="E:\学习笔记\AntBacup"/>
-->
<!--将E:\学习笔记\Ant所有文件移动到E:\学习笔记\AntBacup,移动后会删除E:\学习笔记\Ant目录-->
<move todir="E:\学习笔记\AntBacup">
<fileset dir="E:\学习笔记\Ant"/>
</move>
</target>
<!--mkdir创建目录任务-->
<target name="mkdirTarget">
<!--在build.xml同一个目录下,再创建一个目录build-->
<mkdir dir="build"/>
</target>
<!--echo任务-->
<target name="echoTarget">
<!--将message信息输出到指定文件,采用追加的方式(即当文件存在时,向后追加内容)-->
<!--当log目录,system.log文件不存在时,会自动创建-->
<echo message="Hello,Amigo" file="logs/system.log" append="true"/>
</target>
<!--javac,java任务-->
<!-- 将五子棋项目源码编译成class文件,并且运行-->
<!--指定五子棋项目的路径,和class的存放地址-->
<property name="rootPro" value="E:\学习笔记\Java五子棋\FiveCheerProject"></property>
<property name="rootBuild" value="${rootPro}\build"></property>
<property name="rootBuildClasses" value="${rootBuild}\classes"></property>
<target name ="clean">
<delete dir ="${rootBuild}"/>
</target>
<target name="compile" depends ="clean">
<mkdir dir ="${rootBuildClasses}"/>
<javac srcdir ="${rootPro}\src" destdir ="${rootBuildClasses}"/>
</target>
<!--实际运行发现可在Main方法中打印System.out.println()信息,但不能弹出图形界面(原因不知)-->
<target name ="run" depends ="compile">
<java classname ="com.test.TestMain">
<classpath>
<pathelement path ="${rootBuildClasses}"/>
</classpath>
</java>
</target>
<!--jar任务-->
<!--将上面classes编译好的class文件,打包为jar包-->
<!--点击打包后的jar文件,即可运行五子棋游戏(前提配置好了jdk环境变量)-->
<target name ="jarTarget" depends ="compile">
<jar destfile ="${rootBuild}\FiveCheerProject.jar" basedir ="${rootBuildClasses}">
<manifest >
<attribute name ="Main-class" value ="com.test.TestMain"/>
</manifest >
</jar>
</target>
</project>