向蚂蚁致敬(3)

王朝java/jsp·作者佚名  2006-01-09
宽屏版  字体: |||超大  

清单五:

Manifest-Version: 1.0

Created-By: Dorian

Main-Class: Dorian.HelloWorldFrame

Class-Path: panel.jar

Manifest-Version: 1.0

Created-By: Dorian

Main-Class: Dorian.HelloWorldMain

Class-Path: frame.jar

怎么样你的HelloWorldMain.jar是否可以运行了?不过再次有点小问题:那就是HelloWorldMain依赖于HelloWorldFrame,HelloWorldFrame依赖于HelloWorldPanel。当我试图在HelloWorldFrame和HelloWorldPanel之前构件HelloWorldMain,将会产生错误。

因此我们需要一种方法以正确的顺序构件文件。这时就可以用到ant的主构件文件。

接着来,在HelloWorld主目录中建立构件文件build.xml (清单六)。

清单六:

<?xml version="1.0"?>

<project name="HelloWorld" default="build">

<target name="setProps" unless="setProps">

<property name="outdir" value="/tmp/app/"/>

<property name="setProps" value="true"/>

</target>

<target name="init" depends="setProps">

<property name="lib" value="${outdir}/lib"/>

</target>

<target name="clean" depends="init">

<ant dir="./Panel" target="clean">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

<ant dir="./Frame" target="clean">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

<ant dir="./Main" target="clean">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

<delete dir="${outdir}"/>

</target>

<target name="prepare" depends="init">

<mkdir dir="${build}"/>

<mkdir dir="${lib}"/>

</target>

<target name="build" depends="prepare">

<ant dir="./Panel" target="package">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

<ant dir="./Frame" target="package">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

<ant dir="./Main" target="package">

<property name="outdir" value="${outdir}"/>

<property name="setProps" value="true"/>

</ant>

</target>

</project>

可以看到主构件文件中简单的委托了其子项目并确保以正确的顺序调用子项目的构件文件。

现在只要在HelloWorld目录中在命令行运行ant 就可以构件整个项目了。

小结

虽然只是个简单的实现,但却演示了如何在一个可执行文件中创建应用程序。还演示了如何将被多个应用程序共享的若干个类打包在同一个JAR文件中。

当然你也许会问这个作用并不是十分明显,但当你在一个有着数百个(也许十几个就够了)子项目,在这些子项目够建了上千个组件的项目中。你会感到使用ant构件是件多么幸福的事情。

参考资料

资源

l 本文的原代码: http://www.y365.com/sxhv998/java/Source/Ant.rar

l Ant下载地址: http://jakart.apache.org/ant/index.html

书籍

l 《敏捷开发》

Web 站点

l http://jakarta.apache.org Apache软件组织。

附录:Ant的命令行参数

ant [options] [target [target2 [target3] ...]]

Options:

-help print this message

-projecthelp print project help information

-version print the version information and exit

-diagnostics print information that might be helpful to diagnose or report problems.

-quiet, -q be extra quiet

-verbose, -v be extra verbose

-debug print debugging information

-emacs produce logging information without adornments

-logfile <file> use given file for log

-l <file> ''

-logger <classname> the class which is to perform logging

-listener <classname> add an instance of class as a project listener

-buildfile <file> use given buildfile

-file <file> ''

-f <file> ''

-D<property>=<value> use value for given property

-propertyfile <name> load all properties from file with -D

properties taking precedence

-inputhandler <class> the class which will handle input requests

-find <file> search for buildfile towards the root of the filesystem and use it

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有