Home | FAQ | Contact me

Creating a Java archive (JAR)

Here's how to create an ant target to JAR up the contents of subdirectory bin (compiled Java class files). Similar things must happen to create a WAR file (ant target <war ... />).

<?xml version="1.0" encoding="UTF-8"?>
<project name="helloworld.makejar" default="makejar" basedir=".">

	<target name="makejar" description="Create a JAR for the HelloWorld project">
		<delete file="helloworld.jar" />

		<jar jarfile="helloworld.jar" includes="**/*.class" basedir="bin">
			<manifest>
				<attribute name="Built-By"               value="${user.name}" />
				<attribute name="Main-Class"             value="project.builder.example.HelloWorld" />
				<attribute name="Specification-Title"    value="Example that works" />
				<attribute name="Specification-Version"  value="1.0.0" />
				<attribute name="Specification-Vendor"   value="Example Organization" />
				<attribute name="Implementation-Title"   value="common" />
				<attribute name="Implementation-Version" value="1.0.0 today" />
				<attribute name="Implementation-Vendor"  value="Acme Corp." />
			</manifest>
		</jar>
	</target>

	<javac srcdir="src" destdir="bin" includeantruntime="false" />
</project>