Hello Ant

Create project

1
2
3
4
mkdir hello
mkdir src
cd src
vim Hello.java

Say hello to ant.

Build.xml

1
2
cd ..
vim Build.xml

Set up properties
the source file path/ class path/ jar path and so on.
the trigger target is rerun

1
2
3
4
5
<?xml version="1.0" encoding="UTF-8" ?>
<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 directory

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

1
2
3
<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${dest}" includeAntRuntime="false"/>
</target>


package project target to a jar and execute

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

1
2
3
4
5
<target name="clean">
<delete dir="${dest}" />
<delete file="${jar_path}" />
<echo>x</echo>
</target>


rerun the process

1
2
3
4
5
6
    <target name="rerun" depends="clean,run">
<ant target="run" />
<ant target="clean" />
<echo>xx</echo>
</target>
</project>

Procedure

  1. clean
  2. init
  3. compile
  4. build
  5. run
  6. 1 -> 5
  7. clean