Skip to content

Commit

Permalink
[MSHADE-352] make timestamp of transformed resources reproducible
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Apr 8, 2020
1 parent 0e729d4 commit 0d7bfb5
Show file tree
Hide file tree
Showing 21 changed files with 161 additions and 63 deletions.
21 changes: 11 additions & 10 deletions src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,8 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
}
else
{
if ( !resourceTransformed( transformers, mappedName, in, shadeRequest.getRelocators() ) )
if ( !resourceTransformed( transformers, mappedName, in, shadeRequest.getRelocators(),
entry.getTime() ) )
{
// Avoid duplicates that aren't accounted for by the resource transformers
if ( resources.contains( mappedName ) )
Expand All @@ -261,11 +262,11 @@ else if ( shadeRequest.isShadeSourcesContent() && name.endsWith( ".java" ) )
}

private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeRequest, Set<String> resources,
ResourceTransformer manifestTransformer,
ResourceTransformer resourceTransformer,
JarOutputStream jos )
throws IOException
{
if ( manifestTransformer != null )
if ( resourceTransformer != null )
{
for ( File jar : shadeRequest.getJars() )
{
Expand All @@ -275,22 +276,22 @@ private void goThroughAllJarEntriesForManifestTransformer( ShadeRequest shadeReq
{
JarEntry entry = en.nextElement();
String resource = entry.getName();
if ( manifestTransformer.canTransformResource( resource ) )
if ( resourceTransformer.canTransformResource( resource ) )
{
resources.add( resource );
try ( InputStream inputStream = jarFile.getInputStream( entry ) )
{
manifestTransformer.processResource( resource, inputStream,
shadeRequest.getRelocators() );
resourceTransformer.processResource( resource, inputStream,
shadeRequest.getRelocators(), entry.getTime() );
}
break;
}
}
}
}
if ( manifestTransformer.hasTransformedResource() )
if ( resourceTransformer.hasTransformedResource() )
{
manifestTransformer.modifyOutputStream( jos );
resourceTransformer.modifyOutputStream( jos );
}
}
}
Expand Down Expand Up @@ -515,7 +516,7 @@ private boolean isFiltered( List<Filter> filters, String name )
}

private boolean resourceTransformed( List<ResourceTransformer> resourceTransformers, String name, InputStream is,
List<Relocator> relocators )
List<Relocator> relocators, long time )
throws IOException
{
boolean resourceTransformed = false;
Expand All @@ -526,7 +527,7 @@ private boolean resourceTransformed( List<ResourceTransformer> resourceTransform
{
getLogger().debug( "Transforming " + name + " using " + transformer.getClass().getName() );

transformer.processResource( name, is, relocators );
transformer.processResource( name, is, relocators, time );

resourceTransformed = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
public class ApacheLicenseResourceTransformer
implements ResourceTransformer
{

private static final String LICENSE_PATH = "META-INF/LICENSE";

private static final String LICENSE_TXT_PATH = "META-INF/LICENSE.txt";
Expand All @@ -43,7 +42,7 @@ public boolean canTransformResource( String resource )
|| LICENSE_TXT_PATH.regionMatches( true, 0, resource, 0, LICENSE_TXT_PATH.length() );
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
// no op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class ApacheNoticeResourceTransformer
*/
String encoding;

private long time = Long.MIN_VALUE;

private static final String NOTICE_PATH = "META-INF/NOTICE";

private static final String NOTICE_TXT_PATH = "META-INF/NOTICE.txt";
Expand All @@ -86,7 +88,7 @@ public boolean canTransformResource( String resource )

}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
if ( entries.isEmpty() )
Expand Down Expand Up @@ -191,6 +193,10 @@ else if ( sb.length() > 0 && currentOrg != null )
currentOrg.add( sb.toString() );
}
}
if ( time > this.time )
{
this.time = time;
}
}

public boolean hasTransformedResource()
Expand All @@ -201,7 +207,9 @@ public boolean hasTransformedResource()
public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
jos.putNextEntry( new JarEntry( NOTICE_PATH ) );
JarEntry jarEntry = new JarEntry( NOTICE_PATH );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );

Writer pow;
if ( StringUtils.isNotEmpty( encoding ) )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class AppendingTransformer

ByteArrayOutputStream data = new ByteArrayOutputStream();

private long time = Long.MIN_VALUE;

public boolean canTransformResource( String r )
{
if ( resource != null && resource.equalsIgnoreCase( r ) )
Expand All @@ -49,11 +51,15 @@ public boolean canTransformResource( String r )
return false;
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
IOUtil.copy( is, data );
data.write( '\n' );
if ( time > this.time )
{
this.time = time;
}
}

public boolean hasTransformedResource()
Expand All @@ -64,7 +70,9 @@ public boolean hasTransformedResource()
public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
jos.putNextEntry( new JarEntry( resource ) );
JarEntry jarEntry = new JarEntry( resource );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );

jos.write( data.toByteArray() );
data.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ public class ComponentsXmlResourceTransformer
{
private Map<String, Xpp3Dom> components = new LinkedHashMap<>();

private long time = Long.MIN_VALUE;

public static final String COMPONENTS_XML_PATH = "META-INF/plexus/components.xml";

public boolean canTransformResource( String resource )
{
return COMPONENTS_XML_PATH.equals( resource );
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
Xpp3Dom newDom;
Expand Down Expand Up @@ -126,14 +128,22 @@ public void close()

components.put( key, component );
}

if ( time > this.time )
{
this.time = time;
}
}

public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
JarEntry jarEntry = new JarEntry( COMPONENTS_XML_PATH );
jarEntry.setTime( time );

byte[] data = getTransformedResource();

jos.putNextEntry( new JarEntry( COMPONENTS_XML_PATH ) );
jos.putNextEntry( jarEntry );

jos.write( data );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public boolean canTransformResource( String r )
return false;
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
// no op
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ public class GroovyResourceTransformer

private String extModuleVersion = "1.0";

private long time = Long.MIN_VALUE;

@Override
public boolean canTransformResource( String resource )
{
return EXT_MODULE_NAME.equals( resource ) || EXT_MODULE_NAME_LEGACY.equals( resource );
}

@Override
public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
Properties out = new Properties();
Expand All @@ -77,6 +79,10 @@ public void processResource( String resource, InputStream is, List<Relocator> re
{
append( staticExtensionClasses, staticExtensionClassesList );
}
if ( time > this.time )
{
this.time = time;
}
}

private void append( String entry, List<String> list )
Expand All @@ -99,7 +105,10 @@ public void modifyOutputStream( JarOutputStream os )
{
if ( hasTransformedResource() )
{
os.putNextEntry( new JarEntry( EXT_MODULE_NAME ) );
JarEntry jarEntry = new JarEntry( EXT_MODULE_NAME );
jarEntry.setTime( time );
os.putNextEntry( jarEntry );

Properties desc = new Properties();
desc.put( "moduleName", extModuleName );
desc.put( "moduleVersion", extModuleVersion );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,20 @@ public class IncludeResourceTransformer

String resource;

private long time = Long.MIN_VALUE;

public boolean canTransformResource( String r )
{
return false;
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
// no op
if ( time > this.time )
{
this.time = time;
}
}

public boolean hasTransformedResource()
Expand All @@ -60,9 +65,12 @@ public boolean hasTransformedResource()
public void modifyOutputStream( JarOutputStream jos )
throws IOException
{
JarEntry jarEntry = new JarEntry( resource );
jarEntry.setTime( time );

try ( InputStream in = new FileInputStream( file ) )
{
jos.putNextEntry( new JarEntry( resource ) );
jos.putNextEntry( jarEntry );
IOUtil.copy( in, jos );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public class ManifestResourceTransformer
private boolean manifestDiscovered;

private Manifest manifest;


private long time = Long.MIN_VALUE;

public void setMainClass( String mainClass )
{
this.mainClass = mainClass;
Expand Down Expand Up @@ -87,7 +89,7 @@ public boolean canTransformResource( String resource )
}

@Override
public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
// We just want to take the first manifest we come across as that's our project's manifest. This is the behavior
Expand Down Expand Up @@ -126,6 +128,11 @@ public void processResource( String resource, InputStream is, List<Relocator> re
}

manifestDiscovered = true;

if ( time > this.time )
{
this.time = time;
}
}
}

Expand Down Expand Up @@ -160,7 +167,9 @@ public void modifyOutputStream( JarOutputStream jos )
}
}

jos.putNextEntry( new JarEntry( JarFile.MANIFEST_NAME ) );
JarEntry jarEntry = new JarEntry( JarFile.MANIFEST_NAME );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );
manifest.write( jos );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ public class PluginXmlResourceTransformer
{
private List<Xpp3Dom> mojos = new ArrayList<>();

private long time = Long.MIN_VALUE;

public static final String PLUGIN_XML_PATH = "META-INF/maven/plugin.xml";

public boolean canTransformResource( String resource )
{
return PLUGIN_XML_PATH.equals( resource );
}

public void processResource( String resource, InputStream is, List<Relocator> relocators )
public void processResource( String resource, InputStream is, List<Relocator> relocators, long time )
throws IOException
{
Xpp3Dom newDom;
Expand Down Expand Up @@ -86,6 +88,11 @@ public void close()
return;
}

if ( time > this.time )
{
this.time = time;
}

for ( Xpp3Dom mojo : newDom.getChild( "mojos" ).getChildren( "mojo" ) )
{

Expand Down Expand Up @@ -134,7 +141,9 @@ public void modifyOutputStream( JarOutputStream jos )
{
byte[] data = getTransformedResource();

jos.putNextEntry( new JarEntry( PLUGIN_XML_PATH ) );
JarEntry jarEntry = new JarEntry( PLUGIN_XML_PATH );
jarEntry.setTime( time );
jos.putNextEntry( jarEntry );

jos.write( data );

Expand Down
Loading

0 comments on commit 0d7bfb5

Please sign in to comment.