Ant 简明教程

Ant - Creating JAR files

在编译完 java 源文件后的下一个逻辑步骤,就是构建 java 存档,即 Java 存档 (JAR) 文件。用 Ant 创建 JAR 文件非常容易,只需要 jar 任务。

The next logical step after compiling your java source files, is to build the java archive, i.e., the Java Archive (JAR) file. Creating JAR files with Ant is quite easy with the jar task.

Attributes

jar 任务常用的属性如下 −

The commonly used attributes of the jar task are as follows −

Sr.No

Attributes & Description

1

basedir The base directory for the output JAR file. By default, this is set to the base directory of the project.

2

compress Advises Ant to compress the file as it creates the JAR file.

3

keepcompression While the compress attribute is applicable to the individual files, the keepcompression attribute does the same thing, but it applies to the entire archive.

4

destfile The name of the output JAR file.

5

duplicate Advises Ant on what to do when duplicate files are found. You could add, preserve, or fail the duplicate files.

6

excludes Advises Ant to not include these comma separated list of files in the package.

7

excludesfile Same as above, except the exclude files are specified using a pattern.

8

inlcudes Inverse of excludes.

9

includesfile Inverse of excludesfile.

10

update Advises Ant to overwrite files in the already built JAR file.

续我们的 Hello World 传真应用程序项目,让我们添加一个新目标来生成 jar 文件。

Continuing our Hello World Fax Application project, let us add a new target to produce the jar files.

但是在这样做之前,我们考虑一下给定以下 jar 任务。

But before that, let us consider the jar task given below.

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

在这里, web.dir 属性指向 Web 源文件的路径。在我们的案例中,这会放置 util.jar 的位置。

Here, the web.dir property points to the path of the web source files. In our case, this is where the util.jar will be placed.

此示例中的 build.dir 属性指向 build 文件夹,此文件夹中可以找到 util.jar 的类文件。

The build.dir property in this example, points to the build folder, where the class files for the util.jar can be found.

在此示例中,我们使用 faxapp.util. * 包中的类创建了一个名为 util.jar 的 jar 文件。但是,我们会排除以 Test 名称结尾的类。输出 jar 文件将被放置在 Web 应用程序 lib 文件夹中。

In this example, we create a jar file called util.jar using the classes from the faxapp.util.* package. However, we are excluding the classes that end with the name Test. The output jar file will be placed in the web application lib folder.

如果我们要使 util.jar 成为可执行 jar 文件,需要使用带有 Main-Class 元属性的 manifest

If we want to make the util.jar an executable jar file, we need to add the manifest with the Main-Class meta attribute.

因此,上面的示例将更新如下:

Therefore, the above example will be updated as follows −

<jar destfile="${web.dir}/lib/util.jar"
   basedir="${build.dir}/classes"
   includes="faxapp/util/**"
   excludes="**/Test.class" class="ts"
   <manifest class="ts"
      <attribute name="Main-Class" value="com.tutorialspoint.util.FaxUtil"/>
   </manifest class="ts"
</jar class="ts"

要执行 jar 任务,请将其包装到目标内,最常见的是 build 或 package 目标,然后执行它们。

To execute the jar task, wrap it inside a target, most commonly, the build or package target, and execute them.

<target name="build-jar" class="ts"
<jar destfile="${web.dir}/lib/util.jar"
   basedir="${build.dir}/classes"
   includes="faxapp/util/**"
   excludes="**/Test.class" class="ts"
   <manifest class="ts"
      <attribute name="Main-Class" value="com.tutorialspoint.util.FaxUtil"/>
   </manifest class="ts"
</jar class="ts"
</target class="ts"

在此文件中运行 Ant 会为我们创建 util.jar 文件。

Running Ant on this file creates the util.jar file for us.

运行 Ant 文件后,会产生以下结果 −

The following outcome is the result of running the Ant file −

C:\ class="ts"ant build-jar
Buildfile: C:\build.xml

BUILD SUCCESSFUL
Total time: 1.3 seconds

util.jar 文件现在放置在输出文件夹中。

The util.jar file is now placed in the output folder.