Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHADE-378] Shade plugin changes the compression level of nested jar… #73

Merged
merged 2 commits into from
Nov 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 98 additions & 9 deletions src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PushbackInputStream;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
Expand All @@ -42,6 +45,8 @@
import java.util.jar.JarOutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -71,6 +76,7 @@ public class DefaultShader
extends AbstractLogEnabled
implements Shader
{
private static final int BUFFER_SIZE = 32 * 1024;

public void shade( ShadeRequest shadeRequest )
throws IOException, MojoExecutionException
Expand Down Expand Up @@ -150,6 +156,73 @@ public void shade( ShadeRequest shadeRequest )
}
}

/**
* {@link InputStream} that can peek ahead at zip header bytes.
*/
private static class ZipHeaderPeekInputStream extends PushbackInputStream
{

private static final byte[] ZIP_HEADER = new byte[] {0x50, 0x4b, 0x03, 0x04};

private static final int HEADER_LEN = 4;

protected ZipHeaderPeekInputStream( InputStream in )
{
super( in, HEADER_LEN );
}

public boolean hasZipHeader() throws IOException
{
final byte[] header = new byte[HEADER_LEN];
super.read( header, 0, HEADER_LEN );
super.unread( header );
return Arrays.equals( header, ZIP_HEADER );
}
}

/**
* Data holder for CRC and Size.
*/
private static class CrcAndSize
{

private final CRC32 crc = new CRC32();

private long size;

CrcAndSize( File file ) throws IOException
{
try ( FileInputStream inputStream = new FileInputStream( file ) )
{
load( inputStream );
}
}

CrcAndSize( InputStream inputStream ) throws IOException
{
load( inputStream );
}

private void load( InputStream inputStream ) throws IOException
{
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ( ( bytesRead = inputStream.read( buffer ) ) != -1 )
{
this.crc.update( buffer, 0, bytesRead );
this.size += bytesRead;
}
}

public void setupStoredEntry( JarEntry entry )
{
entry.setSize( this.size );
entry.setCompressedSize( this.size );
entry.setCrc( this.crc.getValue() );
entry.setMethod( ZipEntry.STORED );
}
}

private void shadeJars( ShadeRequest shadeRequest, Set<String> resources, List<ResourceTransformer> transformers,
RelocatorRemapper remapper, JarOutputStream jos, Multimap<String, File> duplicates )
throws IOException, MojoExecutionException
Expand Down Expand Up @@ -255,7 +328,7 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
return;
}

addResource( resources, jos, mappedName, entry.getTime(), in );
addResource( resources, jos, mappedName, entry, jarFile );
}
else
{
Expand Down Expand Up @@ -584,19 +657,35 @@ private void addJavaSource( Set<String> resources, JarOutputStream jos, String n
resources.add( name );
}

private void addResource( Set<String> resources, JarOutputStream jos, String name, long time,
InputStream is )
throws IOException
private void addResource( Set<String> resources, JarOutputStream jos, String name, JarEntry originalEntry,
JarFile jarFile ) throws IOException
{
final JarEntry entry = new JarEntry( name );
ZipHeaderPeekInputStream inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream( originalEntry ) );
try
{
final JarEntry entry = new JarEntry( name );

entry.setTime( time );
// We should not change compressed level of uncompressed entries, otherwise JVM can't load these nested jars
if ( inputStream.hasZipHeader() && originalEntry.getMethod() == ZipEntry.STORED )
{
new CrcAndSize( inputStream ).setupStoredEntry( entry );
inputStream.close();
inputStream = new ZipHeaderPeekInputStream( jarFile.getInputStream( originalEntry ) );
}

jos.putNextEntry( entry );

IOUtil.copy( is, jos );
entry.setTime( originalEntry.getTime() );

resources.add( name );
jos.putNextEntry( entry );

IOUtil.copy( inputStream, jos );

resources.add( name );
}
finally
{
inputStream.close();
}
}

static class RelocatorRemapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
Expand All @@ -33,7 +34,10 @@
import java.util.List;
import java.util.Set;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.CRC32;
import java.util.zip.ZipEntry;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.shade.filter.Filter;
Expand All @@ -45,6 +49,8 @@
import org.codehaus.plexus.logging.AbstractLogger;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.logging.console.ConsoleLogger;
import org.codehaus.plexus.util.IOUtil;
import org.junit.Assert;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.objectweb.asm.ClassReader;
Expand Down Expand Up @@ -262,6 +268,67 @@ public void visitSource( String arg0, String arg1 )
}
}

@Test
public void testShaderWithNestedJar() throws Exception
{
TemporaryFolder temporaryFolder = new TemporaryFolder();

final String innerJarFileName = "inner.jar";

temporaryFolder.create();
File innerJar = temporaryFolder.newFile( innerJarFileName );
try ( JarOutputStream jos = new JarOutputStream( new FileOutputStream( innerJar ) ) )
{
jos.putNextEntry( new JarEntry( "foo.txt" ) );
jos.write( "c1".getBytes( StandardCharsets.UTF_8 ) );
jos.closeEntry();
}

File outerJar = temporaryFolder.newFile( "outer.jar" );
try ( JarOutputStream jos = new JarOutputStream( new FileOutputStream( outerJar ) ) )
{
FileInputStream innerStream = new FileInputStream( innerJar );
byte[] bytes = IOUtil.toByteArray( innerStream, 32 * 1024 );
innerStream.close();
writeEntryWithoutCompression( innerJarFileName, bytes, jos );
}


ShadeRequest shadeRequest = new ShadeRequest();
shadeRequest.setJars( new LinkedHashSet<>( Collections.singleton( outerJar ) ) );
shadeRequest.setFilters( new ArrayList<Filter>() );
shadeRequest.setRelocators( new ArrayList<Relocator>() );
shadeRequest.setResourceTransformers( new ArrayList<ResourceTransformer>() );
File shadedFile = temporaryFolder.newFile( "shaded.jar" );
shadeRequest.setUberJar( shadedFile );

DefaultShader shader = newShader();
shader.shade( shadeRequest );

JarFile shadedJarFile = new JarFile( shadedFile );
JarEntry entry = shadedJarFile.getJarEntry( innerJarFileName );

//After shading, entry compression method should not be changed.
Assert.assertEquals( entry.getMethod(), ZipEntry.STORED );

temporaryFolder.delete();
}

private void writeEntryWithoutCompression( String entryName, byte[] entryBytes, JarOutputStream jos ) throws IOException
{
final JarEntry entry = new JarEntry( entryName );
final int size = entryBytes.length;
final CRC32 crc = new CRC32();
crc.update( entryBytes, 0, size );
entry.setSize( size );
entry.setCompressedSize( size );
entry.setMethod( ZipEntry.STORED );
entry.setCrc( crc.getValue() );
jos.putNextEntry( entry );
jos.write( entryBytes );
jos.closeEntry();
}

private void shaderWithPattern( String shadedPattern, File jar, String[] excludes )
throws Exception
{
Expand Down