Create project
1 | mkdir hello |
Say hello to ant.
Build.xml
1 | cd .. |
Set up properties
the source file path/ class path/ jar path and so on.
the trigger target is rerun
1
2
3
4
5
<project name="hello" default="rerun" basedir=".">
<property name="src" value="src"/>
<property name="dest" value="classes"/>
<property name="jar_path" value="hello.jar"/>
Define the init method
clean workspace first
print a log and make classes directory1
2
3
4
5
6
7
8<target name="init" depends="clean">
<echo>+---------------------------------------------------+</echo>
<echo>| |</echo>
<echo>| B U I L D I N G ${dest} |</echo>
<echo>| |</echo>
<echo>+---------------------------------------------------+</echo>
<mkdir dir="${dest}"/>
</target>
compile project use javac command1
2
3<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" includeAntRuntime="false"/>
</target>
package project target to a jar and execute1
2
3
4
5
6<target name="build" depends="compile">
<jar jarfile="${jar_path}" basedir="${dest}"/>
</target>
<target name="run" depends="build">
<java classname="Hello" classpath="${jar_path}"/>
</target>
delete build files1
2
3
4
5<target name="clean">
<delete dir="${dest}" />
<delete file="${jar_path}" />
<echo>x</echo>
</target>
rerun the process1
2
3
4
5
6 <target name="rerun" depends="clean,run">
<ant target="run" />
<ant target="clean" />
<echo>xx</echo>
</target>
</project>
Procedure
- clean
- init
- compile
- build
- run
- 1 -> 5
- clean