March 23, 2009

how to create different project jar file and exclude some files or folders

In java projects sometimes we need setup dependencies among modules by sharing code via JAR files. In some cases its required that the some module for ex module_a is a dependency for two other modules, lets say module_b and module_c. module_b requires all the code in module_a except for the files in package /org/, similarly module_c requires all files/classes in module_a except in package /net/

Here is the sample ANT code to do that

Following ANT target creates a trimmed version of module_a classes, by excluding the org folder

<target name="make-jar-for-module-b" depends="compile-classes ,clean-jars">
<jar index="yes" jarfile="${dir.build.jar}/module_a_for_b.jar" compress="true">
<fileset dir="${dir.build.classes}">
<exclude name="**/org/**" />
</fileset>
</jar>
</target>


Following ANT target creates a trimmed version of module_a classes, by excluding the net folder

<target name="make-jar-for-module-c" depends="compile-classes ,clean-jars">
<jar index="yes" jarfile="${dir.build.jar}/module_a_for_c.jar" compress="true">
<fileset dir="${dir.build.classes}">
<exclude name="**/net/**" />
</fileset>
</jar>
</target>


Here we assume that you are already done with ANT targets for compiling classes and cleaning the jar folders.

On executing these targets you will get the required dependency jars by name module_a_for_c.jar and module_a_for_b.jar