- ANT - Listeners and Loggers
- ANT - Custom Components
- ANT - Using If Else arguments
- ANT - Using Command Line Arguments
- ANT - Using Token
- ANT - Extending Ant
- ANT - JUnit Integration
- ANT - Eclipse Integration
- ANT - Executing Java code
- ANT - Deploying Applications
- ANT - Packaging Applications
- ANT - Create WAR Files
- ANT - Creating JAR files
- ANT - Build Documentation
- ANT - Building Projects
- ANT - Data Types
- ANT - Property Files
- ANT - Property Task
- ANT - Build Files
- ANT - Environment Setup
- ANT - Introduction
- ANT - Home
Apache ANT Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Ant - Creating JAR files
The next logical step after compipng 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
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 apppcable to the inspanidual files, the keepcompression attribute does the same thing, but it apppes to the entire archive. |
4 | destfile The name of the output JAR file. |
5 | duppcate Advises Ant on what to do when duppcate files are found. You could add, preserve, or fail the duppcate files. |
6 | excludes Advises Ant to not include these comma separated pst 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. |
Continuing our Hello World Fax Apppcation project, let us add a new target to produce the jar files.
But before that, let us consider the jar task given below.
<jar destfile="${web.dir}/pb/util.jar" basedir="${build.dir}/classes" includes="faxapp/util/**" excludes="**/Test.class" />
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.
The build.dir property in this example, points to the build folder, where the class files for the util.jar can be found.
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 apppcation pb folder.
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}/pb/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"
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}/pb/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"
Running Ant on this file creates the util.jar file for us.
The following outcome is the result of running the Ant file −
C: class="ts"ant build-jar Buildfile: C:uild.xml BUILD SUCCESSFUL Total time: 1.3 seconds
The util.jar file is now placed in the output folder.
Advertisements