您的位置:首页 > 其它

ANT教程之九 Ant创建JAR文件

2015-11-07 16:35 260 查看
编译Java源文件后的下一个合乎逻辑的步骤,是建立在Java归档,JAR文件。创建JAR文件与Ant用jar任务很容易。以下展示的是jar任务的常用属性

属性描述
basedirThe base directory for the output JAR file. By default, this is set to the base directory of the project.
compressAdvises ant to compress the file as it creates the JAR file.
keepcompressionWhile the compress attribute is applicable to the individual files, thekeepcompression attribute does
the same thing, but it applies to the entire archive.
destfileThe name of the output JAR file
duplicateAdvises Ant on what to do when duplicate files are found. You could add, preserve or fail the duplicate files.
excludesAdvises Ant to not include these comma separated list of files in the package.
excludesfileSame as above, except the exclude files are specified using a pattern.
inlcudesInverse of excludes
includesfileInverse of excludesfile.
updateAdvises ant to overwrite files in the already built JAR file.
继续我们的Hello World传真应用程序项目,让我们添加一个新的目标,产生的jar文件。但在此之前,让我们考虑一下jar任务:

<jar destfile="${web.dir}/lib/util.jar"
basedir="${build.dir}/classes"
includes="faxapp/util/**"
excludes="**/Test.class"
/>


在这个例子中,web.dir属性指向的网页源文件的路径。在我们的例子中,这是其中的util.jar将被放置。

在这个例子中,build.dir属性指向build文件夹在哪里可以找到 util.jar的类文件。

在这个例子中,我们创建了一个名为util.jar使用的类从faxapp.util一个jar文件。*包。然而,我们不包括用名称测试结束课程。输出的jar文件会发生在webapp的lib文件夹。

如果我们想使util.jar一个可执行JAR文件,我们需要添加清单与主Classmeta属性。

因此,上面的例子将被更新为:
<jar destfile="${web.dir}/lib/util.jar"
basedir="${build.dir}/classes"
includes="faxapp/util/**"
excludes="**/Test.class">
<manifest>
<attribute name="Main-Class" value="com.yiibai.util.FaxUtil"/>
</manifest>
</jar>


要执行jar任务,一个目标里面把它包(最常见,构建或包的目标,并运行它们。
<target name="build-jar">
<jar destfile="${web.dir}/lib/util.jar" basedir="${build.dir}/classes" includes="faxapp/util/**" excludes="**/Test.class"> <manifest> <attribute name="Main-Class" value="com.yiibai.util.FaxUtil"/> </manifest> </jar>
</target>


在这个文件运行Ant会为我们创建util.jar文件

下面的结果是运行Ant文件的结果:
C:>ant build-jar
Buildfile: C:uild.xml

BUILD SUCCESSFUL
Total time: 1.3 seconds


现在的util.jar文件放置在输出文件夹。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: