Skip to content

Commit 1d2b764

Browse files
committed
Respect the order entries in a JAR file
The insertion order did not take into account that META-INF/ and/or META-INF/MANIFEST.MF can or should come first. The code has added it META-INF/ to directories, thus the actual order of a JAR file was: META-INF/MANIFEST.MF META-INF/ META-INF/maven which does not make sense. Test supplied by: Plamen Totev <[email protected]> This closes #189
1 parent ab01e2f commit 1d2b764

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/main/java/org/codehaus/plexus/archiver/zip/ConcurrentJarCreator.java

+18-16
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@ public class ConcurrentJarCreator
4444

4545
private final boolean compressAddedZips;
4646

47-
private final ScatterZipOutputStream directories;
48-
4947
private final ScatterZipOutputStream metaInfDir;
5048

5149
private final ScatterZipOutputStream manifest;
5250

51+
private final ScatterZipOutputStream directories;
52+
5353
private final ScatterZipOutputStream synchronousEntries;
5454

5555
private final ParallelScatterZipCreator parallelScatterZipCreator;
@@ -123,9 +123,9 @@ public ConcurrentJarCreator( boolean compressAddedZips, int nThreads ) throws IO
123123
{
124124
this.compressAddedZips = compressAddedZips;
125125
ScatterGatherBackingStoreSupplier defaultSupplier = new DeferredSupplier( 100000000 / nThreads );
126-
directories = createDeferred( defaultSupplier );
127-
manifest = createDeferred( defaultSupplier );
128126
metaInfDir = createDeferred( defaultSupplier );
127+
manifest = createDeferred( defaultSupplier );
128+
directories = createDeferred( defaultSupplier );
129129
synchronousEntries = createDeferred( defaultSupplier );
130130
parallelScatterZipCreator = new ParallelScatterZipCreator( Executors.newFixedThreadPool( nThreads ),
131131
defaultSupplier );
@@ -152,20 +152,10 @@ public void addArchiveEntry( final ZipArchiveEntry zipArchiveEntry, final InputS
152152
{
153153
throw new IllegalArgumentException( "Method must be set on the supplied zipArchiveEntry" );
154154
}
155-
if ( zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink() )
156-
{
157-
final ByteArrayInputStream payload = new ByteArrayInputStream( new byte[]
158-
{
159-
} );
160-
161-
directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, createInputStreamSupplier(
162-
payload ) ) );
163-
164-
payload.close();
165-
}
166-
else if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals( zipArchiveEntry.getName() ) )
155+
if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals( zipArchiveEntry.getName() ) )
167156
{
168157
InputStream payload = source.get();
158+
// TODO This should be enforced because META-INF non-directory does not make any sense?!
169159
if ( zipArchiveEntry.isDirectory() )
170160
{
171161
zipArchiveEntry.setMethod( ZipEntry.STORED );
@@ -178,6 +168,7 @@ else if ( "META-INF".equals( zipArchiveEntry.getName() ) || "META-INF/".equals(
178168
else if ( "META-INF/MANIFEST.MF".equals( zipArchiveEntry.getName() ) )
179169
{
180170
InputStream payload = source.get();
171+
// TODO This should be enforced because META-INF/MANIFEST as non-file does not make any sense?!
181172
if ( zipArchiveEntry.isDirectory() )
182173
{
183174
zipArchiveEntry.setMethod( ZipEntry.STORED );
@@ -187,6 +178,17 @@ else if ( "META-INF/MANIFEST.MF".equals( zipArchiveEntry.getName() ) )
187178

188179
payload.close();
189180
}
181+
else if ( zipArchiveEntry.isDirectory() && !zipArchiveEntry.isUnixSymlink() )
182+
{
183+
final ByteArrayInputStream payload = new ByteArrayInputStream( new byte[]
184+
{
185+
} );
186+
187+
directories.addArchiveEntry( createZipArchiveEntryRequest( zipArchiveEntry, createInputStreamSupplier(
188+
payload ) ) );
189+
190+
payload.close();
191+
}
190192
else if ( addInParallel )
191193
{
192194
parallelScatterZipCreator.addArchiveEntry( createEntrySupplier( zipArchiveEntry, source ) );

src/test/java/org/codehaus/plexus/archiver/jar/BaseJarArchiverTest.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@
2626
import java.io.InputStream;
2727
import java.nio.file.Files;
2828
import java.nio.file.Paths;
29+
import java.util.Enumeration;
2930
import java.util.zip.ZipEntry;
3031
import java.util.zip.ZipFile;
3132

33+
import static org.junit.Assert.assertEquals;
3234
import static org.junit.Assert.assertNotNull;
3335
import static org.junit.Assert.assertTrue;
3436

@@ -55,8 +57,11 @@ public void testCreateJar()
5557
// verify that the JAR file is created and contains the expected files
5658
try ( ZipFile resultingArchive = new ZipFile( jarFile ) )
5759
{
58-
// verify that the JAR file contains manifest file
59-
assertNotNull( resultingArchive.getEntry( "META-INF/MANIFEST.MF" ) );
60+
// verify that the JAR file contains manifest directory and file
61+
// and that those are the first two entries.
62+
Enumeration<? extends ZipEntry> resultingEntries = resultingArchive.entries();
63+
assertEquals( "META-INF/", resultingEntries.nextElement().getName() );
64+
assertEquals( "META-INF/MANIFEST.MF", resultingEntries.nextElement().getName() );
6065

6166
// verify the JAR contains the class and it is not corrupted
6267
ZipEntry classFileEntry = resultingArchive.getEntry( "com/example/app/Main.class" );

0 commit comments

Comments
 (0)