New Bee New Bee - 1 month ago 7
Java Question

Ant jar task include a single class file only

i have an ant task

<target name="create_jar" depends="compile">
<jar destfile="build/temp/MyClassJar.jar" basedir="build/classes/com/company/utils">
<manifest>
<attribute name="Main-Class" value="com.company.utils.MyClass"/>
</manifest>
</jar>
</target>


the folder build/classes has multiple packages and class files but in my jar i only want to include only two files MyClass.class and MyClass$1.class which are in com/company/utils folder.

if i have base-dir as com/company/utils when i run the task the jar does not have the package folders in like com/company/utils is not created inside the jar file but if i change my base-dir to build/classes then all the files are getting included.
what do to fix this.

Answer

The Jar task takes a nested fileset, so you can do this:

<jar destfile="build/temp/MyClassJar.jar">
    <fileset dir="build/classes" includes="**/MyClass*.class" />
    <manifest>
        <attribute name="Main-Class" value="com.company.utils.MyClass"/>
    </manifest>
</jar>
Comments