您的位置:首页 > 编程语言 > Java开发

使用ant编译打包java文件

2009-02-10 09:04 543 查看
使用ant编译打包java文件
1.独立使用ant
1.1得到ant与安装
http://ant.apache.org/去下载。然后,解压在c:/ant下。(文档写出,路径尽量短)
1.2配置
设置环境变量:
ANT_HOME:c:/ant
PATH: ;c:/ant/bin;
1.3.使用
建立一个文件夹project.
1.2.1建立工程。
在其下建产src,用来存放源代码。
Lib用来存放要用到的jar包。
1.2.2建立一个xml文件
example.xml (ant认缺省的为build.xml),在里面开始写过程。一般包括设置文件夹属性值,初始化(清理文件夹中内

容,以便从新开始),编译,打包,执行,几个步骤。
   参考代码可见ant下的帮助手册(C:/ant/docs/manual/index.html )/Developing with Ant/Tutorials.很详细


例:
   <?xml version="1.0"?>
<project name="ant_test" default="run">

<!-- 说明各文件夹 -->
<property name="src.dir" value="src" />
<property name="report.dir" value="report" />
<property name="classes.dir" value="classes" />
<property name="lib.dir" value="lib" />
<property name="dist.dir" value="dist" />
<property name="doc.dir" value="doc" />
<!-- 存放Main函数的类 -->
<property name="main-class" value="test.Test"/>

<!-- 说明classpath,包括进来工程要用到的jar包 -->
<path id="classpath">
<fileset file="$(lib.dir)" includes="**/*.jar"/>
</path>

<!-- 初始化,清理文件夹中的文件,以便重新编译 -->
<target name="init">
<delete dir="${classes.dir}"/>
<delete dir="${dist.dir}"/>
<delete dir="${doc.dir}" />
</target>

<!-- 编译 -->
<target name="compile" depends="init"
description="compile the source files">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpath="classpath" />
</target>
<!-- 打包成jar文件,包名为工程名,或起其他名 -->
<target name="jar" depends="compile" description="make .jar file">
<mkdir dir="${dist.dir}" />
<jar destfile="${dist.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<!-- 运行 -->
<target name="run" depends="jar">
<java jar="${dist.dir}/${ant.project.name}.jar" fork="true"/>
</target>
</project>
1.2.3运行ant
命令行在project下, ant -buildfile example.xml run
注:
A.如果是build.xml可以直接写 :ant (运行build.xml 的default target)
B.如果要运行别的target,如init : ant -buildfile example.xml init
可参考手册 Runnint Ant/Command Line
2.在eclipse下使用ant
1.建立工程。
 建立source folder存放源代码。
 lib放要用的jar
build.xml文件
2.调出ant
window/show view选择ant,再选择add Buildfiles 在对话框里选择此工程的build.xml文件。
3运行
 如上编写build.xml。会在ant view里看见各个target,双击,可单独执行这个target
点 按钮run default target可运行缺省的target,这里设置缺省的为最后一个,可以把它依赖的各个target都执行一次。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: