Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ant generates the JAR file


May 25, 2021 Apache Ant


Table of contents


Ant generates the JAR file

Once you've compiled your java source file, build the java archive, for example, the JAR file. C reating a JAR file in Ant is simple, using the jar task to generate the jar package. The properties commonly used in jar tasks are as follows:

Property Describe
basedir Represents the base directory that outputs the JAR file. By default, it is the base directory for the project.
compress Indicates that Ant is told to compress the JAR file that was created.
keepcompression Represents the absolute path to the project base directory.
destfile Represents the name of the output JAR file.
duplicate Represents what Ant does when a duplicate file is found. This can be added, saved, or invalidated for the duplicate file.
excludes Represents a list of files that are removed, with commas separating multiple files.
excludesfile Excludes files with the same as above, but using pattern matching.
inlcudes It's the opposite of excludes.
includesfile Represents a file that already exists in the archived file mode. In contrast to excludesfile.
update Represents telling Ant to override an established JAR file.

Continue with our Hello World fax app project to generate jar files by adding a new target target. But before we do, let's consider the jar task given below.

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

Here, the web.dir property indicates the path to the web source file. In our case, the web source file path is where the util .jar stored.

In our case, the build.dir property indicates the storage path for the configuration folder, where the util .jar class files are stored.

In the code above, we created a jar package called util using a class file from the faxapp.util package .jar the package. H owever, we exclude class files with the name Test. The output jar file will be stored in the web app's profile lib.

If we want .jar to become an executable, simply add a manifest to the Main-Class meta-attribute .

Thus, the code given above, after adding the Main-Class meta-attribute, can be updated to the following form:

<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>

In order to perform the jar task, wrap it in the target target, most commonly, wrap the jar task in the configuration target or package target (build target or package target) and execute the wrapped target.

<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>

By running Ant on the file above, you can create a util .jar.

When the above file runs Ant, you get the following output:

C:\>ant build-jar
Buildfile: C:\build.xml

BUILD SUCCESSFUL
Total time: 1.3 seconds

The resulting output util .jar stored in the output folder.