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.
     
    Roedy Green, Jun 6, 2004
    #1
    1. Advertisements

  2. Roedy Green

    Mickey Segal Guest

    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. Advertisements

  3. Roedy Green

    Roedy Green Guest

    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.
     
    Roedy Green, Jun 7, 2004
    #3
  4. Roedy Green

    Roedy Green Guest

    How does renaming hide them? Do you move them to a different
    directory?
     
    Roedy Green, Jun 7, 2004
    #4
  5. Roedy Green

    Sudsy Guest

    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

    I think it is time that jar.exe were beefed up a little to allow
    exclusion and inclusion list files with wildcards.
     
    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.
     
    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
    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. 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

    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.
     
    Roedy Green, Jun 7, 2004
    #10
  11. Roedy Green

    Oscar kind Guest

    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, Jun 7, 2004
    #11
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.