Building a jar file with exclusions.

Discussion in 'Java' started by Roedy Green, Jun 6, 2004.

  1. Roedy Green

    Roedy Green Guest

    Sometimes you want to build a jar file and exclude a few files. Is
    there a way to do that and still get a correct manifest?

    Maintaining an explicit list of files to include is error prone since
    you can't use wildcards, and you need to mention every internal class
    explicitly.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 6, 2004
    #1
    1. Advertising

  2. Roedy Green

    Mickey Segal Guest

    "Roedy Green" <> wrote in message
    news:...
    > Sometimes you want to build a jar file and exclude a few files. Is
    > there a way to do that and still get a correct manifest?
    >
    > Maintaining an explicit list of files to include is error prone since
    > you can't use wildcards, and you need to mention every internal class
    > explicitly.


    I have a batch file that renames the files to exclude, makes the JAR and
    CAB, and then re-renames the files.
    Mickey Segal, Jun 6, 2004
    #2
    1. Advertising

  3. Roedy Green

    Roedy Green Guest

    On Sun, 06 Jun 2004 21:23:44 GMT, Roedy Green
    <> wrote or quoted :

    >Sometimes you want to build a jar file and exclude a few files. Is
    >there a way to do that and still get a correct manifest?
    >
    >Maintaining an explicit list of files to include is error prone since
    >you can't use wildcards, and you need to mention every internal class
    >explicitly.


    The way I decided to handle this was to split the project in two so I
    would not need exclusions, only inclusions from the other project.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 7, 2004
    #3
  4. Roedy Green

    Roedy Green Guest

    On Sun, 6 Jun 2004 18:10:06 -0400, "Mickey Segal"
    <> wrote or quoted :

    >I have a batch file that renames the files to exclude, makes the JAR and
    >CAB, and then re-renames the files.


    How does renaming hide them? Do you move them to a different
    directory?

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 7, 2004
    #4
  5. Roedy Green

    Sudsy Guest

    Roedy Green wrote:
    > Sometimes you want to build a jar file and exclude a few files. Is
    > there a way to do that and still get a correct manifest?
    >
    > Maintaining an explicit list of files to include is error prone since
    > you can't use wildcards, and you need to mention every internal class
    > explicitly.


    Strange, but I just ran into a variant of this same issue recently. I
    needed to backup a source tree but not include object files, executables
    or Java class files. I ended up using ant! Even though I actually use
    the tar task (rather than jar), the approach is the same. Here's a
    sample task:

    <jar destfile="${dist}/lib/app.jar">
    <fileset dir="${build}/classes">
    <custom classname="net.sudsy.ant.util.FileFilter" />
    </fileset>
    </jar>

    Here's part of my FileFilter.java source:

    package net.sudsy.ant.util;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.DataInputStream;
    import java.io.IOException;

    public class FileFilter
    implements org.apache.tools.ant.types.selectors.FileSelector {
    public boolean isSelected( File baseDir, String fileName,
    File f ) {
    DataInputStream dis = null;
    try {
    if( ! f.exists() ) // should not happen
    return( false );
    if( f.isDirectory() ) // don't include directories
    return( false );
    dis = new DataInputStream( new FileInputStream( f ) );
    int fileType = dis.readInt(); // get "magic" #
    if( fileType == 0x7f454c46 ) // ELF format
    return( false );
    if( fileType == 0xcafebabe ) // Java class
    return( false );
    }
    catch( IOException e ) {
    }
    finally {
    if( dis != null ) {
    try {
    dis.close();
    }
    catch( IOException e ) {
    }
    }
    }
    return( true );
    }
    }

    Easier to do it this way than try to come up with the correct
    list of extensions/lack of extensions to include/exclude.
    I thought it was quite neat!
    Sudsy, Jun 7, 2004
    #5
  6. Roedy Green

    Roedy Green Guest

    On 6 Jun 2004 19:16:44 -0800, (Malcolm
    Dew-Jones) wrote or quoted :

    >You can use zip, but it doesn't create a manifest. A batch file or shell
    >script could use zip, then list the zip contents as the basis to create a
    >manifest file, and add the manifest back to the zip.


    I think it is time that jar.exe were beefed up a little to allow
    exclusion and inclusion list files with wildcards.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 7, 2004
    #6
  7. Roedy Green () wrote:
    : Sometimes you want to build a jar file and exclude a few files. Is
    : there a way to do that and still get a correct manifest?

    You can use zip, but it doesn't create a manifest. A batch file or shell
    script could use zip, then list the zip contents as the basis to create a
    manifest file, and add the manifest back to the zip.

    --

    (Paying) telecommute programming projects wanted. Simply reply to this.
    Malcolm Dew-Jones, Jun 7, 2004
    #7
  8. Roedy Green

    Tony Morris Guest

    > Sometimes you want to build a jar file and exclude a few files. Is
    > there a way to do that and still get a correct manifest?
    >
    > Maintaining an explicit list of files to include is error prone since
    > you can't use wildcards, and you need to mention every internal class
    > explicitly.


    http://ant.apache.org/

    --
    Tony Morris
    (BInfTech, Cert 3 I.T.)
    Software Engineer
    (2003 VTR1000F)
    Sun Certified Programmer for the Java 2 Platform (1.4)
    Sun Certified Developer for the Java 2 Platform
    Tony Morris, Jun 7, 2004
    #8
  9. Malcolm Dew-Jones wrote:
    > You can use zip, but it doesn't create a manifest. A batch file or shell
    > script could use zip, then list the zip contents as the basis to create a
    > manifest file, and add the manifest back to the zip.


    This is dangerous, since the manifest file must be the first file in the
    archive, otherwise JarInputStream fails to find and read it. If you add
    the manifest file manually later, chances are, that it will not be where
    it is supposed to be.

    /Thomas
    Thomas Weidenfeller, Jun 7, 2004
    #9
  10. Roedy Green

    Roedy Green Guest

    On Mon, 07 Jun 2004 08:36:22 +0200, Thomas Weidenfeller
    <> wrote or quoted :

    >This is dangerous, since the manifest file must be the first file in the
    >archive, otherwise JarInputStream fails to find and read it. If you add
    >the manifest file manually later, chances are, that it will not be where
    >it is supposed to be.


    The other thing you have to watch for is making sure Winzip/PkZip uses
    none of the fancy compression algorithms that jar/ZipFile does not
    understand.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Jun 7, 2004
    #10
  11. Roedy Green

    Oscar kind Guest

    Tony Morris <> wrote:
    >> Sometimes you want to build a jar file and exclude a few files. Is
    >> there a way to do that and still get a correct manifest?
    >>

    >
    > http://ant.apache.org/


    Although perfectly correct, I like examples. So here is one that defines a
    target for a build file:

    <target name="build">
    <jar destfile="archive.jar">
    <fileset dir="classes" excludes="**/Test*.class"/>
    <fileset dir="sources" includes="**/*.properties *.xml"/>
    </jar>
    </target>


    kind regards,
    Oscar

    --
    Oscar Kind http://home.hccnet.nl/okind/
    Software Developer for contact information, see website

    PGP Key fingerprint: 91F3 6C72 F465 5E98 C246 61D9 2C32 8E24 097B B4E2
    Oscar kind, Jun 7, 2004
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arnold Peters
    Replies:
    0
    Views:
    567
    Arnold Peters
    Jan 5, 2005
  2. muttley
    Replies:
    0
    Views:
    2,718
    muttley
    Oct 20, 2005
  3. cyberco
    Replies:
    4
    Views:
    3,752
    Roedy Green
    Feb 14, 2006
  4. Arnold Peters
    Replies:
    0
    Views:
    657
    Arnold Peters
    Jan 5, 2005
  5. Replies:
    7
    Views:
    446
    Joe Kesselman
    Aug 10, 2006
Loading...

Share This Page