ant大家熟知的项目构建工具,在编程领域得到了极大的应用,对于项目jar,war文件的生成有其独特的优势,ant你可以在IDE中使用,也可以在IDE外使用,其配置过程如下
1.在apache的官网下载ant包,并将其解压,我的是放在C盘根目录
2.将bin的目录添加到Administrator用户的path目录中C:\apache-ant-1.8.2\bin
3.在Administrator用户变量中添加一个变量ANT_HOME,并将其值设为ant解压后的目录C:\apache-ant-1.8.2
下面是用ant创建目录的例子,创建一个名为hellowoorld的文件,当然你也可以指定多级目录,在ant同级目录中新建一个xml文件名为build.xml这是ant默认构建文件,你也可以指定自己的构建文件只需设置ant -f 构建名.xml,以下为一个创建目录的例子
<?xml version="1.0" encoding="UTF-8"?>
<project default="init">
<target name="init">
<mkdir dir="helloworld"/>
</target>
</project>
删除只需将mkdir改为delete
当然在IDE中使用你只需在项目根目录下新建一个build.xml文件,编写自己的target,以下是一些小例子
<?xml version="1.0" encoding="UTF-8"?>
<project name="myAntProject" basedir=".">
<description>属性的定义</description>
<property name="compile" value="compile">
</property>
<property name="dist" value="dist">
</property>
<target name="init">
</target>
<description>文件目录的生成</description>
<target name="preprocess" depends="init">
<mkdir dir="${compile}" />
<mkdir dir="${dist}" />
</target>
<target name="compile" depends="preprocess">
</target>
<target name="package" depends="compile">
</target>
<description>java源文件的编译</description>
<target name="mycompile" depends="preprocess">
<javac srcdir="src" destdir="${compile}">
</javac>
</target>
<description>java字节码文件打包成jar</description>
<target name="jar" depends="mycompile">
<jar destfile="${dist}/package.jar" basedir="${compile}">
</jar>
</target>
<description>文件的删除</description>
<target name="deletefile">
<delete file="${dist}/package.jar"></delete>
</target>
<description>文件的复制</description>
<target name="copyfile">
<copy file="src/com/lamp/test/Test1.java" tofile="src/Copy.java"></copy>
</target>
<description>文件的移动</description>
<target name="movefile">
<move file="src/com/lamp/test/Test2.java" todir="D:\Move.java"></move>
</target>
<description>文件中指定字符串的替换,将原来的字符串kkk改为aaa</description>
<target name="replaceString">
<replace file="input.txt" token="kkk" value="aaa"></replace>
</target>
<description>匹配模式的复制</description>
<target name="copyPattern">
<copy todir="dist">
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</copy>
</target>
<description>自定义ant任务,前提是要继承ant中的org.apache.tools.ant.Task类,并重写其execute()方法</description>
<taskdef name="filesorterExample" classname="com.lamp.test.FileSorter" classpath="bin"></taskdef>
<target name="myfilesort">
<filesorterExample srcFile="input.txt" destFile="output.txt"/>
</target>
</project>