Skip to content

Commit

Permalink
(fix) pass preserveFileTimestamps to Transformer and set entry time b…
Browse files Browse the repository at this point in the history
…ased on that.
  • Loading branch information
John Engelman committed Sep 30, 2018
1 parent 78356c1 commit f6ff652
Show file tree
Hide file tree
Showing 17 changed files with 59 additions and 26 deletions.
7 changes: 7 additions & 0 deletions src/docs/asciidoc/90-changes.adoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
== Change Log
[discrete]
=== v4.0.1 (2018-09-30)
* **Breaking Change!** `Transform.modifyOutputStream(ZipOutputStream os)` to `Transform.modifyOutputStream(ZipOutputStream jos, boolean preserveFileTimestamps)`.
Typically breaking changes are reserved for major version releases, but this change was necessary for `preserverFileTimestamps` (introduced in v4.0.0) to work correctly
in the presence of transformers. https://github.com/johnrengelman/shadow/issues/404[#404]
* Fix regression in support Java 10+ during relocation https://github.com/johnrengelman/shadow/issues/403[#403]

[discrete]
=== v4.0.0 (2018-09-25)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import java.util.zip.ZipException

@Slf4j
class ShadowCopyAction implements CopyAction {
private static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = (new GregorianCalendar(1980, 1, 1, 0, 0, 0)).getTimeInMillis()
static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = (new GregorianCalendar(1980, 1, 1, 0, 0, 0)).getTimeInMillis()

private final File zipFile
private final ZipCompressor compressor
Expand Down Expand Up @@ -130,7 +130,7 @@ class ShadowCopyAction implements CopyAction {
private void processTransformers(ZipOutputStream stream) {
transformers.each { Transformer transformer ->
if (transformer.hasTransformedResource()) {
transformer.modifyOutputStream(stream)
transformer.modifyOutputStream(stream, preserveFileTimestamps)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ class ApacheLicenseResourceTransformer implements Transformer {
return false
}

void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,10 @@ class ApacheNoticeResourceTransformer implements Transformer {
return true
}

void modifyOutputStream(ZipOutputStream os) {
os.putNextEntry(new ZipEntry(NOTICE_PATH))
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry zipEntry = new ZipEntry(NOTICE_PATH)
zipEntry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, zipEntry.time)
os.putNextEntry(zipEntry)

Writer pow
if (StringUtils.isNotEmpty(encoding)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ class AppendingTransformer implements Transformer {
return data.size() > 0
}

void modifyOutputStream(ZipOutputStream os) {
os.putNextEntry(new ZipEntry(resource))
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry entry = new ZipEntry(resource)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)

IOUtil.copy(new ByteArrayInputStream(data.toByteArray()), os)
data.reset()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,13 @@ class ComponentsXmlResourceTransformer implements Transformer {
}
}

void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
byte[] data = getTransformedResource()

os.putNextEntry(new ZipEntry(COMPONENTS_XML_PATH))
ZipEntry entry = new ZipEntry(COMPONENTS_XML_PATH)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)

os.putNextEntry(entry)

IOUtil.copy(data, os)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DontIncludeResourceTransformer implements Transformer {
return false
}

void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
// no op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ class GroovyExtensionModuleTransformer implements Transformer {
}

@Override
void modifyOutputStream(ZipOutputStream os) {
os.putNextEntry(new ZipEntry(GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATH))
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry entry = new ZipEntry(GROOVY_EXTENSION_MODULE_DESCRIPTOR_PATH)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
IOUtil.copy(toInputStream(module), os)
os.closeEntry()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ public class IncludeResourceTransformer implements Transformer {
return file != null ? file.exists() : false
}

public void modifyOutputStream(ZipOutputStream os) {
os.putNextEntry(new ZipEntry(resource))
public void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry entry = new ZipEntry(resource)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)

InputStream is = new FileInputStream(file)
IOUtil.copy(is, os)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ class Log4j2PluginsCacheFileTransformer implements Transformer {
}

@Override
void modifyOutputStream(ZipOutputStream zipOutputStream) {
void modifyOutputStream(ZipOutputStream zipOutputStream, boolean preserveFileTimestamps) {
PluginCache pluginCache = new PluginCache()
pluginCache.loadCacheFiles(getUrlEnumeration())
relocatePlugins(pluginCache)
zipOutputStream.putNextEntry(new ZipEntry(PLUGIN_CACHE_FILE))
ZipEntry entry = new ZipEntry(PLUGIN_CACHE_FILE)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
zipOutputStream.putNextEntry(entry)
pluginCache.writeCache(new CloseShieldOutputStream(zipOutputStream))
temporaryFiles.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ManifestResourceTransformer implements Transformer {
return true
}

void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
// If we didn't find a manifest, then let's create one.
if (manifest == null) {
manifest = new Manifest()
Expand All @@ -90,7 +90,9 @@ class ManifestResourceTransformer implements Transformer {
}
}

os.putNextEntry(new ZipEntry(JarFile.MANIFEST_NAME))
ZipEntry entry = new ZipEntry(JarFile.MANIFEST_NAME)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
manifest.write(os)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ class PropertiesFileTransformer implements Transformer {
}

@Override
void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
propertiesEntries.each { String path, Properties props ->
os.putNextEntry(new ZipEntry(path))
ZipEntry entry = new ZipEntry(path)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
IOUtil.copy(toInputStream(props), os)
os.closeEntry()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,11 @@ class ServiceFileTransformer implements Transformer, PatternFilterable {
}

@Override
void modifyOutputStream(ZipOutputStream os) {
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
serviceEntries.each { String path, ServiceStream stream ->
os.putNextEntry(new ZipEntry(path))
ZipEntry entry = new ZipEntry(path)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
IOUtil.copy(stream.toInputStream(), os)
os.closeEntry()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@ interface Transformer {

boolean hasTransformedResource()

void modifyOutputStream(ZipOutputStream jos)
void modifyOutputStream(ZipOutputStream jos, boolean preserveFileTimestamps)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.jengelman.gradle.plugins.shadow.transformers

import com.github.jengelman.gradle.plugins.shadow.ShadowStats
import com.github.jengelman.gradle.plugins.shadow.relocation.Relocator
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowCopyAction
import groovy.transform.Canonical
import groovy.transform.builder.Builder

Expand All @@ -14,4 +15,8 @@ class TransformerContext {
InputStream is
List<Relocator> relocators
ShadowStats stats

static long getEntryTimestamp(boolean preserveFileTimestamps, long entryTime) {
preserveFileTimestamps ? entryTime : ShadowCopyAction.CONSTANT_TIME_FOR_ZIP_ENTRIES
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ class XmlAppendingTransformer implements Transformer {
return doc != null
}

void modifyOutputStream(ZipOutputStream os) {
os.putNextEntry(new ZipEntry(resource))
void modifyOutputStream(ZipOutputStream os, boolean preserveFileTimestamps) {
ZipEntry entry = new ZipEntry(resource)
entry.time = TransformerContext.getEntryTimestamp(preserveFileTimestamps, entry.time)
os.putNextEntry(entry)
new XMLOutputter(Format.getPrettyFormat()).output(doc, os)

doc = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Log4j2PluginsCacheFileTransformerTest {
def fileOutputStream = new FileOutputStream(testableZipFile)
def bufferedOutputStream = new BufferedOutputStream(fileOutputStream)
def zipOutputStream = new ZipOutputStream(bufferedOutputStream)
transformer.modifyOutputStream(zipOutputStream)
transformer.modifyOutputStream(zipOutputStream, true)
zipOutputStream.close()
bufferedOutputStream.close()
fileOutputStream.close()
Expand Down

0 comments on commit f6ff652

Please sign in to comment.