服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

java技巧:hello world ant


  another neat tool(ant)是apache jakarta中一个很好用的java开发工具,但是它的安装却很烦琐。然而如果你用一个简单的hello world风格的build.xml安装就会很容易的完成这个任务。
  
  下面简要介绍了一个完整的ant安装过程。下面这段代码在测试包中使用了hello world类:
  
  // the hello world class
  package test;
  
  public class helloworld {
  
  static public void main(string[] args) {
    helloworld hw = new
    helloworld();
    hw.invoke();
  }
  
  public void invoke()
  {
    system.out.println("hello,i am "+getclass().getname());
  }
  }
   
  在unix符号下,该hello world类的路径是./src/test/helloworld.java。
  
  接下来,你需要一个build.xml文件。在ant中该文件和makefile的作用是相同的,而且该文件包含了开发过程中所需要的一切信息。build.xml包含了一系列目标指令。当“all”目标运行时,所有它依赖的目标也都会自动运行――清理目录,生成.jar文件,创建javadoc,然后运行。“build”目标依赖于“compile”目标,因此“compile”目标也会在.jar文件生成前运行。
  
  这里是build文件:
  
   java技巧:hello world ant(图一)
  java技巧:hello world ant(图二)
  build.xml文件的目录和src/所在目录一样。执行ant有以下两种方式:一、运行build.bat或build.sh脚本;二、运行新版本的ant.bat或ant脚本。你具体使用哪个方法取决于你的ant版本。通常来讲,build.bat/build.sh文件有许多副本,而ant.bat/ant文件会进入path变量。
  
  通过在含有build.xml文件的目录中键入ant,build或build.sh运行build.xml脚本就会输出如下结果。在这个例子中,build.xml文件保存在/home/ant/example/build.xml目录中,而helloworld.java文件保存在/home/ant/example/src/test/helloworld.java目录中:
  
  buildfile: build.xml
  
  clean:
  [delete] deleting directory /home/ant/example/classes
  [delete] deleting directory /home/ant/example/build
  [delete] deleting directory /home/ant/example/docs
  
  compile:
  [mkdir] created dir: /home/ant/example/classes
  [javac] compiling 1 source file to /home/ant/example/classes
  
  build:
  [mkdir] created dir: /home/ant/example/build
   [jar] building jar: /home/ant/example/build/helloworld.jar
  
  docs:
  [mkdir] created dir: /home/ant/example/docs
   [javadoc] generating javadoc
   [javadoc] javadoc execution
   [javadoc] loading source files for package test...
   [javadoc] constructing javadoc information...
   [javadoc] building tree for all the packages and classes...
   [javadoc] building index for all the packages and classes...
   [javadoc] building index for all classes...
  
  test:
  hello, i am test.helloworld
  
  all:
  
  build successful
  
  total time: 4 seconds
  通过这个开发脚本和示例java文件,我们可以测试和安装一个ant系统。该系统的总体结构为改善后的开发项目提供了一个构造平台。请访问
  apache jakarta开始下载和安装ant。

扫描关注微信公众号