| |
技术文档>>JAVA>>新手入门>>基础入门>查看文档 |
|
| |
使用 ant 协助高效开发 java 项目 |
|
| |
文章作者:未知 文章来源:水木森林 |
|
| |
查看:109次 录入:管理员--2007-11-17 |
|
| |
ant是apache的开源项目,目前在java的项目开发中被广泛的采用,功能非常的强大!因此我们有必要熟悉一下这个基于java的工具。 首先我们应该安装ant,从apache.org下载后直接安装到机器上就可以使用了。最好设置一下相关的环境变量,虽然很多系统能够自动识别出来。你还是应该设置ant_home,java_home,path这几个环境变量,例如你的ant安装在c:/ant1.6 你的java安装在c:/j2sdk1.4.2.那么我们可以执行下面的操作设置环境变量(winxp): set ant_home=c:/ant1.6 set java_home= c:/j2sdk1.4.2 set path=%path%;%path%/bin 设置好ant以后 在command里面执行ant -version看看是不是可以输出ant的版本信息确认安装成功 由于很多开发工具都内置了ant,所以我直接以eclipse为例说明如何用ant开发java项目,这里的project非常简单,新建一个项目为testant,然后再src里面写两个类内容如下: package com.north; /** * @author p2800 * * todo to change the template for this generated type comment go to * window - preferences - java - code style - code templates */ public class testant { public static void main(string[] args) { new mywork().print(); } } package com.north; /* * created on 2004-7-23 * * todo to change the template for this generated file go to * window - preferences - java - code style - code templates */ /** * @author p2800 * * todo to change the template for this generated type comment go to * window - preferences - java - code style - code templates */ public class mywork { public void print() { system.out.println(system.getenv("classpath")); } } 我不准备介绍过多的ant的使用,通过察看用户手册你能很快上手的。而是直接给出build.xml的内容并对相关的内容作一些必要的说明。你在project testant里面新建一个文件build.xml,这个是ant默认去寻找的文件,如果你用其他文件名的话你应该使用ant -buildfile mybuildfile.xml this is my test for ant tool 这个build.xml并不复杂 他的目的是对project里面的源代码编译 然后打包到dist目录。在build.xml里面主要有如下几个元素:project target task property。其中property是让你去定义一些属性值,在以后的target或者task里面去使用。每个project必须指定一个default的target来执行。每个target是task的集合,用来完成一个特定的任务,一般比单个task复杂。target的一个参数depends说明了编译的顺序,例如在这里你要让jar执行的话,必须compile先执行,而compile执行之前init先执行。所以顺序是init->compile->jar。task是最重要的,在ant中有很多内置的task,你看一下列表就会觉得ant真是非常强大。具体你要使用哪个task就要参考他的文档。 选中build.xml右键选择run->ant build,你将在console看到 buildfile: c:/eclipse/workspace/testant/build.xml init: [mkdir] created dir: c:/eclipse/workspace/testant/dist [mkdir] created dir: c:/eclipse/workspace/testant/classes compile: [javac] compiling 2 source files to c:/eclipse/workspace/testant/classes [javac] note: c:/eclipse/workspace/testant/src/com/north/mywork.java uses or overrides a deprecated api. [javac] note: recompile with -deprecation for details. jar: [jar] building jar: c:/eclipse/workspace/testant/dist/myjar-20040723.jar build successful total time: 2 seconds 这表示编译成功了,但是提示有个deprecated api在程序中使用了,这个留给读者去查看一下手册看如何显示出哪个api?
|
|
|
|
相关文档
|