<!-- 创建文件并编译 -->
<target name="mkdir" depends="deletedir">
<mkdir dir="build/classes" />
<javac srcdir="src" destdir="build/classes" />
<echo message="创建文件并编译" />
</target>
<!-- 删除目录 -->
<target name="deletedir">
<delete dir="build" />
<echo message="删除文件"></echo>
</target>
<!-- run设置 -->
<target name="run" depends="mkdir">
<java classname="HelloWorld">
<classpath>
<pathelement path="build/classes"></pathelement>
</classpath>
</java>
</target>
<!-- 生成jar文件 -->
<target name="jar" depends="run">
<jar destfile="helloworld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-class" value="HelloWorld"/>
</manifest>
</jar>
<target name="mkdir" depends="deletedir">
<mkdir dir="build/classes" />
<javac srcdir="src" destdir="build/classes" />
<echo message="创建文件并编译" />
</target>
<!-- 删除目录 -->
<target name="deletedir">
<delete dir="build" />
<echo message="删除文件"></echo>
</target>
<!-- run设置 -->
<target name="run" depends="mkdir">
<java classname="HelloWorld">
<classpath>
<pathelement path="build/classes"></pathelement>
</classpath>
</java>
</target>
<!-- 生成jar文件 -->
<target name="jar" depends="run">
<jar destfile="helloworld.jar" basedir="build/classes">
<manifest>
<attribute name="Main-class" value="HelloWorld"/>
</manifest>
</jar>
</target>
如果针对src下面有包结构的工程,则在生成jar文件时,写法如下:<attribute name="Main-class" value="包名.类名"/>则运行成功。