Ant 生成 JAR 文件
编译完你的 java 源文件后,接下来就构建 java 存档,例如: JAR 文件。创建 Ant 中的 JAR 文件十分简单,运用 jar 任务来生成 jar 包。在 jar 任务中常用的属性如下所示:
属性 | 描述 |
basedir | 表示输出 JAR 文件的基目录。默认情况下,为项目的基目录。 |
compress | 表示告知 Ant 对于创建的 JAR 文件进行压缩。 |
keepcompression | 表示 project 基目录的绝对路径。 |
destfile | 表示输出 JAR 文件的名字。 |
duplicate | 表示发现重复文件时 Ant 执行的操作。可以是添加、保存、或者是使该重复文件失效。 |
excludes | 表示移除的文件列表,列表中使用逗号分隔多个文件。 |
excludesfile | 与上同,但是使用模式匹配的方式排除文件。 |
inlcudes | 与 excludes 正好相反。 |
includesfile | 表示在被归档的文件模式下,打包文件中已有的文件。与 excludesfile 相反。 |
update | 表示告知 Ant 重写已经建立的 JAR 文件。 |
继续我们的 Hello World 传真应用项目,通过添加一个新的目标 target 来产生 jar 文件。 但是在此之前,让我们先来考虑下面给出的 jar 任务。
<jar destfile = "${web.dir}/lib/util.jar"
basedir = "${build.dir}/classes"
includes = "faxapp/util/**"
excludes = "**/Test.class" />
这里,web.dir 属性指出了 web 源文件的路径。在我们的案例中, web 源文件路径也就是存放 util.jar 的地方。
在我们的案例中,build.dir 属性指出了配置文件夹的存储路径,也就是存放 util.jar 类文件的地方。
在上面的代码中,我们利用来自 faxapp.util 包中的类文件创建了一个名为 util.jar 的 jar 包。然而,我们排除名字为 Test 的类文件。输出的 jar 文件将会存放在 web 应用的配置文件 lib 中。
如果我们想 util.jar 成为可执行文件,只需在 Main-Class 元属性中加入manifest.
这样,上面给出的代码,在加入 Main-Class 元属性后,可以更新为如下形式:
<jar destfile = "${web.dir}/lib/util.jar"
basedir = "${build.dir}/classes"
includes = "faxapp/util/**"
excludes = "**/Test.class">
<manifest>
<attribute name = "Main-Class" value = "com.tutorialspoint.util.FaxUtil"/>
</manifest>
</jar>
为了执行 jar 任务,将它包装在目标 target 中,最常见的情况是,将 jar 任务包装在配置目标或者打包目标中(build 目标或 package 目标),并执行包装后的目标。
<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.tutorialspoint.util.FaxUtil"/>
</manifest>
</jar>
</target>
在上述文件上运行 Ant ,就能创建出 util.jar。
上述文件运行 Ant 后,得到以下的输出:
C:\>ant build-jar
Buildfile: C:\build.xml
BUILD SUCCESSFUL
Total time: 1.3 seconds
最后得到的输出 util.jar 将被存储在输出文件夹中。
我的Demo
build.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project name="demo" default="build">
<property file="build.properties"/>
<!-- build .java -->
<target name="build" description="编译demo项目">
<mkdir dir="${build.dir}"/>
<!--
srcdir:java源文件的文件夹
source:指定用哪个版本的编译器对java源码进行编译
target:指定生成的class文件将保证和哪个版本的虚拟机进行兼容
deprecation:是否给出不建议使用的API,默认值false
optimize:指出是否应该用优化方式编译源代码,默认为 off
failonerror:Ant 任务在出现编译错误的情况下是否继续执行,默认值为 True
-->
<javac destdir="${build.dir}" source="1.5" target="1.5"
deprecation="true" optimize="false" failonerror="true" encoding="utf-8">
<src path="${src.dir}"/>
<classpath refid="master-classpath"/>
</javac>
</target>
<path id="master-classpath">
<fileset dir="lib">
<include name="*.jar"/>
</fileset>
<pathelement path="${build.dir}"/>
</path>
<!-- clean -->
<target name="cl" description="清除编译生成的所有class文件">
<!-- 删除指定目录下的所有文件和目录,不包括指定目录-->
<delete includeEmptyDirs="true">
<fileset dir="${build.dir}" includes="**/*"/>
</delete>
</target>
<!-- build jar -->
<target name="bj" description="把class打包成jar">
<!-- 删除旧jar,那个<jar>标签的update属性好像用不了 -->
<delete file="${lib.dir}/${ant.project.name}.jar"/>
<!-- destfile是文件全名,包括路径 -->
<!-- basedir是输入路径 -->
<jar destfile="${lib.dir}/${ant.project.name}.jar" basedir="${build.dir}"
includes="com/**" update="true" >
</jar>
</target>
</project>
<!--
<jar>标签属性的设置:
basedir:The base directory for the output JAR file. By default, this is set to the base directory of the project.
compress:Advises ant to compress the file as it creates the JAR file.
keepcompression:While the%uA0compress%uA0attribute is applicable to the individual files, thekeepcompression%uA0attribute does the same thing, but it applies to the entire archive.
destfile:The name of the output JAR file
duplicate:Advises Ant on what to do when duplicate files are found. You could add, preserve or fail the duplicate files.
excludes:Advises Ant to not include these comma separated list of files in the package.
excludesfile:Same as above, except the exclude files are specified using a pattern.
inlcudes:Inverse of excludes
includesfile:Inverse of excludesfile.
update:Advises ant to overwrite files in the already built JAR file.
-->
build.properties:
src.dir=src
web.dir=demo
# properties文件也可以引用web.dir属性
build.dir=${web.dir}/WEB-INF/classes
lib.dir=lib