3

How to build jar using ant without META-INF folder? I have not specified any manifest attribute in:

<jar destfile="${build}/${jar}/${client}.jar" basedir="${build}/${classes}/${main}">
    <fileset file="${src}/${main}/application-context.xml" />
    <fileset file="${src}/${main}/log4j.xml" />
</jar>

But the jar file containing META-INF folder. Is there any way to exclude this folder's creation?


Edit:

The complete build.xml is:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     Aug 14, 2011 6:25:15 PM                                                                           
     Tanmoy                                                                
    ====================================================================== -->
<project name="Number">
    <description>
        Number
    </description>

    <property name="lib" value="lib" />
    <property name="src" value="src" />
    <property name="build" value="build" />
    <property name="classes" value="classes" />
    <property name="jar" value="jar" />
    <property name="jarfilename" value="Number" />
    <property name="client" value="client" />
    <property name="main" value="main" />
    <property name="loader" value="loader" />

    <target name="clean">
        <delete dir="${build}" />
    </target>

    <path id="classpath">
        <fileset dir="${lib}" includes="*.jar" />
    </path> 

    <target name="compile">
        <delete dir="${lib}" />
        <mkdir dir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\swinglabs\swingx-core\1.6.2-2\swingx-core-1.6.2-2.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-core\3.0.5.RELEASE\spring-core-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-asm\3.0.5.RELEASE\spring-asm-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\commons-logging\commons-logging\1.1.1\commons-logging-1.1.1.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-context\3.0.5.RELEASE\spring-context-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-aop\3.0.5.RELEASE\spring-aop-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-beans\3.0.5.RELEASE\spring-beans-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\org\springframework\spring-expression\3.0.5.RELEASE\spring-expression-3.0.5.RELEASE.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\log4j\apache-log4j-extras\1.0\apache-log4j-extras-1.0.jar" todir="${lib}" />
        <copy file="C:\Users\Tanmoy\.m2\repository\log4j\log4j\1.2.9\log4j-1.2.9.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\dj-nativeswing-0.9.8.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\dj-nativeswing-swt-0.9.8.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\dj-swingsuite-0.9.1.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-linux32-3.6.2.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-linux64-3.6.2.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-osx32-3.6.2.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-osx64-3.6.2.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-win32-3.6.2.jar" todir="${lib}" />
        <copy file="F:\Number\Number\jars\swt-win64-3.6.2.jar" todir="${lib}" />

        <mkdir dir="${build}/${classes}/${main}" />
        <javac srcdir="${src}/${main}" destdir="${build}/${classes}/${main}" classpathref="classpath" />

        <mkdir dir="${build}/${classes}/${loader}" />
        <javac srcdir="${src}/${loader}" destdir="${build}/${classes}/${loader}" />
    </target>

    <target name="jar" depends="clean, compile">
        <mkdir dir="${build}/${jar}" />

        <jar destfile="${build}/${jar}/${client}.jar" basedir="${build}/${classes}/${main}">
            <fileset file="${src}/${main}/application-context.xml" />
            <fileset file="${src}/${main}/log4j.xml" />
        </jar>

        <jar destfile="${build}/${jar}/${jarfilename}.${jar}" basedir="${build}/${classes}/${loader}">
            <manifest>
                <attribute name="Main-Class" value="in.res.num.tpb.loader.ClientLoader" />
            </manifest>
            <fileset dir="${build}/${jar}" includes="${client}.jar" />
            <fileset dir="${lib}" includes="*.jar" />
        </jar>

        <delete file="${build}/${jar}/${client}.jar" />
    </target>

    <target name="run" depends="clean, compile, jar">
        <java jar="${build}/${jar}/${jarfilename}.${jar}" fork="true" />
    </target>
</project>
Tapas Bose
  • 28,796
  • 74
  • 215
  • 331

3 Answers3

2

Use zip instead of jar. Why do you want to avoid a META-INF folder?

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I am building a jar using method discussed in http://stackoverflow.com/questions/2706222/create-cross-platform-java-swt-application/3204032#3204032. I gave the ant script in my original post. It is creating to META-INF jar. One inside Number.jar and another inside Number.jar/client.jar. I think there is no need to have a META-INF folder inside client.jar. – Tapas Bose Aug 21 '11 at 12:46
  • 1
    My point was: even if the META-INF folder is not necessary, why do you care about it? Having one won't make your application crash. It won't make the jar twice as big. You just shouldn't care about it. – JB Nizet Aug 21 '11 at 12:56
  • +1 and thank you but unfortunately I cannot accept the answer. The answer given by @Stephen C is equally helpful. But Stackoverflow has rule to accept only one answer, (don't know why I cannot accept two or more same or different equally acceptable answer). I had a little chat with [camickr](http://stackoverflow.com/users/131872/camickr) in this [thread](http://stackoverflow.com/questions/7059278/change-panel-size-on-button-click) and it was not so good. I am not complaining about anything but it will be better in my opinion to change the rule of acceptance of the answers. – Tapas Bose Aug 22 '11 at 15:47
  • @Tapas: The rules are not mine (and not camickr's). I'm just a simple user as yourself. If you want to complain or ask a question abount StackOverflow, just post it in http://meta.stackoverflow.com/. If you think Stephen's answer is the best one, then accept it. You don't have to give excuse. We all know the rules of the game, and are here to help, more than to get points. – JB Nizet Aug 22 '11 at 16:08
  • Thank you for response, I will post in meta. I am not giving any excuse but some people are here to get points more than to help. – Tapas Bose Aug 22 '11 at 16:28
2

Is there any way to exclude this folder's creation?

A JAR file without a META-INF folder is really just a ZIP file, so the simple solution is to use the Ant <zip> task.

If you run the jar command from the command line, it looks like you can suppress the creation of a MANIFEST.MF file and (presumably) the folder using the M command option; see the Jar command manual. But I don't know if this actually does what I think it does, and there doesn't seem to be a way to get the Ant <jar> task to do the same thing.

But, just like @JB Nizet, I don't see why you would care if there is one redundant folder in your JAR file. It seems too trivial an issue to spend time on.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • Just using StackOverflow as a discussion of record four years later, one reason to care is that MSOffice cares. You can unzip a .pptx file, modify it programmatically and rezip it, and it opens fine in PowerPoint ... but only if you use the `-M` option., – prototype Oct 06 '15 at 01:03
  • Well, hypothetically, if you were trying to use Ant to build a ".pptx" file then that would be a reason not to use the ``. However, that's pretty far fetched. Is there a more plausible use-case? – Stephen C Oct 06 '15 at 07:30
-1

A Jar always contains a META-INF-folder, otherwise it is no valid jar. But as a jar is actually a ZIP-file with additional metainformations (included in the META-INF-folder) you can simply use the ZIP-task (also giving the resulting file the suffix .jar). Note, that only with META-INF you can have executable-jars (with a main-class) or other JAR-features like sealed packages. But you can add ZIPs to the classpath the same way as JARs.

Mnementh
  • 50,487
  • 48
  • 148
  • 202
  • 3
    A jar witout META-INF IS a valid jar file. See http://download.oracle.com/javase/1.3/docs/guide/jar/jar.html#Intro: "A JAR file is essentially a zip file that contains an **optional** META-INF directory" – JB Nizet Aug 21 '11 at 16:17
  • 3
    jar -cMf xxx.jar creates a jar without META-INF/MANIFEST – Aerospace Nov 27 '13 at 14:33