Ant入门笔记
对ANT入门的例子在这里可以参考,其中注意事项为:
- 可以在任意地方创建一个文件夹,并命名为ant或者其他。在ant文件夹中创建src子目录,并在src子目录中创建并编写java程序
- 在ant目录下创建build.xml文件。格式如例子所示。
- 在终端进入ant目录,并使用#ant命令进行编译
- 编译成功后,可以在终端看到java程序的结果。
- 以下附带程序:HelloWorld.java源码
package com.sharetop.antdemo;//生成com.sharetop.antdemo.HelloWorld.class的路径名。即所需要的包创建格式 public class HelloWorld { public static void main( String args[] ) { System.out.println("Hello world. "); } }
build.xml源码
<!--网页显示的字符--> <?xml version="1.0" encoding="UTF-8"?> <!--创建工程名为HelloWorld,工程有4个target,分别是init,compile,build,run,缺省的是run,4个target相互依赖--> <project name="HelloWorld" default="run" basedir="." > <property name="src" value="src"/> <property name="dest" value="classes"/> <property name="hello_jar" value="hello.jar" /> <!--初始化当前目录为dest--> <target name="init"> <mkdir dir="${dest}"/> </target> <!--编译当前目录下的src目录中的HelloWorld.java程序--> <target name="compile" depends="init"> <javac srcdir="${src}" destdir="${dest}"/> </target> <!--build生成hello.jar,并存放在dest目录下,--> <target name="build" depends="compile"> <jar jarfile="${hello_jar}" basedir="${dest}"/> </target> <!--run则是执行这个HelloWorld类,用hello.jar作为classpath,因为com.sharetop.antdemo.HelloWorld.class在hello.jar压缩包中,
在build中生成的,并将结果显示在终端--> <target name="run" depends="build"> <java classname="com.sharetop.antdemo.HelloWorld" classpath="${hello_jar}"/> </target> </project>注意: 经常会出现以下错误1。其中build失败。解决方式:删除<?xml version="1.0" encoding="UTF-8"?>前行的所有信息。格式原因吧,因此build.xml文件的首行只能是版本信息。
BUILD FAILED /home/administrator/antlib/build.xml:2: The processing instruction target matching "[xX][mM][lL]" is not allowed. Total time: 0 seconds
- 错误2:注意:target的两个属性之间要用空格隔开,即name和depends,否则将出现以下错误。
-
BUILD FAILED /home/administrator/t1/build.xml:9: Element type "target" must be followed by either
attribute specifications, ">" or "/>".