Roedy said:
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!