making more than jar files with Ant

In a previous post, making jar files with Ant,  I briefly covered the creation of a Ant script that would compile the Java source files and then create jar file from them.

It was a really neat example of how with less than 100 lines of script ant can compile the source, create a zip file from the source, create a jar and even include the source files in the jar file (optionally).

This is possible due to the powerful list of (over 150) tasks that are available to your scripts.  This list ranges from the more common tasks like

  • copy files or directories
  • delete a file or directory
  • make directory
  • zip / unzip
  • tar / untar
  • fix operating system line endings
  • auto incrementing build number
  • secure copy
  • clearcase access tool
  • cvs access tool

This is just a small list of what is available.  If the list of tasks doesn’t contain what you need you can either execute any system command.  It is also possible to extend Ant with new tasks but I won’t be getting into that right now.

Creating a package

It is beyond the scope of this blog entry to cover the creation of something really interesting like creating a Debian or Red Hat Package but Ant could probably do it.

One of the IT groups that I have worked with in the past had their own custom package installation system.  It was really just a zip file with one of the files containing the inventory of files in the package as well as which types of operations should be done as part of the install.

The package installer then read the inventory file and proceeded to copy, delete and in general install files to their machine.  The actual format of their package is irrelevant for this example, however, below is a similar style of how we used Ant to build our packages for that client.

	<target name="package" depends="jarfile,srcfile" description="prepare for release">
		<echo>"preparing ..."</echo>
 
step 1		<!-- create a temp directory with name of our package -->
		<mkdir dir="${packagename}"/>
 
		<!-- fill it with all sorts of program goodness -->

step2		<!-- contents of our "static" files-->
		<copy todir="${packagename}/${packagename}" verbose="No" preservelastmodified="Yes">
			<fileset dir="${package.dir}" includes="**" />
		</copy>
 
step 3		<!-- contents of other libraries -->
		<copy todir="${packagename}/${packagename}/lib" verbose="No" preservelastmodified="Yes">
			<fileset dir="${lib.dir}" includes="**" />
		</copy>
 
step 4		<!-- copy our program -->
		<copy file="${build.dir}/${jarname}.jar" todir="${packagename}/${packagename}/lib" verbose="No" preservelastmodified="Yes"/>

step 5		<!-- turn it into a zip file -->
		<zip zipfile="${build.dir}/${packagename}.zip" basedir="${packagename}" 
			includes="**">
		</zip>
 
step 6		<!-- turn it into a tar file -->
		<tar compression="gzip" destfile="${build.dir}/${zipfile}-${version.num}.tar.gz">
			<tarfileset dir="${packagename}" >
				<include name="**" /> 
			</tarfileset>
		</tar>
 
step 7		<!-- clean up after ourselves -->
		<delete dir="${packagename}"/>
	</target>

The actual ant tasks and their comment should actually be enough, but just in case I have summarized each step in the following table.

 

Step Description
1. Create a temporary directory using the name of the package.
2. Copy all files from my directory that contained some of the more static package files into the temporary directory.  This directory contains any other configuration or data files.
3. Copy all libraries from the lib directory into the temporary directory.
4. Copy our newly created java library into the temporary directory.
Note: we could have created the java library in that directory directly but that would have not been so obvious compared to the build file we have.
5. Create a zip file using the temporary directory as our source.  This will create the zip file with the same name as our package which is quite convenient.
Note: the permissions get lost on the runInterface.sh file
6. Create a gzipped tar file using the temporary directory as our source. This will create the tar file with the same name as our package which is quite convenient.
Note: the permissions get lost on the runInterface.sh file
You could get around this by running an operating system command (tar).
7. Remove the temporary directory as no longer necessary.

Below is the complete source code for a log4j2 based Hello world application.  It isn’t the program that is so interesting but how the build script, using the package generation seen above can create our zip with our program.

If you look closely at the build script you will see it includes the two log4j2 jar files in the classpath allowing us to have a runnable jar not just a normal jar.

Simply unzip the package into a directory and the scripts can be run immediately.

 

Note: The example was compiled with java 1.8 and uses log4j-api-2.6.2.jar and log4j-core-2.6.2.jar.

This entry was posted in programming, Setup From Scratch and tagged , , . Bookmark the permalink.