ant的构建入门(一)

<?xml version="1.0"?>
<project name="EasyLife" default="sayBaseDir" basedir="E:\work\EasyLife">
<property name="name" value="sillycat" />
<!-- property属性,能在全局用${}来使用 -->
<property name="age" value="25" />

<!--
      name:   project元素的名称
   default:默认执行时所执行的target的名称
   basedir:基路径的位置
-->
<target name="sayBaseDir">
   <!--
   name:    target元素的名称
   depends: 依赖的任务,用“,”号分割
   if:      验证指定的属性是否存在,若存在执行
   unless: 验证指定的属性是否存在,若不存在执行
   description:描述
   -->
   <echo message="The base dir is: ${basedir}" />
</target>

<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>

<target name="example">
   <echo message="name: ${name}, age: ${age}" />
</target>

<target name="create">
   <!-- 生成文件夹 -->
   <mkdir dir="${basedir}/build" />
   <!-- 拷贝单个文件 -->
   <copy file="spy.log" tofile="test.log" />
   <!-- 拷贝文件夹 -->
   <copy todir="${basedir}/build/">
    <fileset dir="${basedir}/src/conf/" />
   </copy>
   <!-- 拷贝文件到文件夹 -->
   <copy file="test.log" todir="${basedir}/build/" />
</target>

<target name="delete">
   <echo message="Delete the files!" />
   <!-- 删除单个文件 -->
   <delete file="test.log" />
   <!-- 删除单个目录 -->
   <delete dir="${basedir}/build/" />
</target>

<target name="move">
   <copy file="spy.log" tofile="spy.txt1" />
   <copy file="spy.log" tofile="spy.txt2" />
   <mkdir dir="${basedir}/temp" />
   <mkdir dir="${basedir}/temp/test1" />
   <!-- 移动单个文件 -->
   <move file="spy.txt1" tofile="${basedir}/build/spy.log" />
   <!-- 移动单个文件到一个目录 -->
   <move file="spy.txt2" todir="${basedir}/build/" />
   <!-- 移动文件夹下的内容到另外一个文件夹 -->
   <move todir="${basedir}/build/">
    <fileset dir="${basedir}/temp" />
   </move>
</target>

<target name="say">
   <!-- 打印输出到文件 -->
   <echo message="Hello,sillycat" file="system.log" append="true" />
</target>

<!-- 删除编译区 -->
<target name="clean">
   <delete dir="${basedir}/build" />
</target>

<!-- classpath -->
<path id="classpath.compile">
   <fileset dir="${basedir}/WebRoot/WEB-INF/lib">
    <include name="**/*.jar" />
   </fileset>
</path>

<!-- 编译JAVA代码到build -->
<target name="compile" depends="clean">
   <mkdir dir="${basedir}/build/classes" />
   <javac srcdir="${basedir}/src/java" destdir="build/classes" encoding="utf-8">
    <classpath refid="classpath.compile" />
   </javac>
   <javac srcdir="${basedir}/src/test" destdir="build/classes" encoding="utf-8">
    <classpath refid="classpath.compile" />
   </javac>
</target>

<!-- 执行其中的JAVA程序 -->
<target name="run" depends="compile">
   <java classname="com.sillycat.SayHello">
    <classpath>
     <pathelement path="build/classes" />
    </classpath>
   </java>
</target>

<!-- class打成jar包 -->
<target name="jar" depends="run">
   <jar destfile="${basedir}/build/easyLife.jar" basedir="build/classes">
   </jar>
</target>

</project>

你可能感兴趣的:(java,xml,ant)