Building a jar file with exclusions.

R

Roedy Green

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

Mickey Segal

Roedy Green 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.

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

Roedy Green

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

Roedy Green

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?
 
S

Sudsy

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!
 
R

Roedy Green

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

Malcolm Dew-Jones

Roedy Green ([email protected]) 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.
 
T

Tony Morris

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
 
T

Thomas Weidenfeller

Malcolm said:
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
 
R

Roedy Green

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

Oscar kind

Tony Morris said:

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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top