Skip to content

Commit bde2703

Browse files
committed
Support AbstractArchiveTask.preserveFileTimestamps
1 parent 7a22ea5 commit bde2703

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowCopyAction.groovy

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import java.util.zip.ZipException
4040

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

4445
private final File zipFile
4546
private final ZipCompressor compressor
@@ -50,10 +51,12 @@ class ShadowCopyAction implements CopyAction {
5051
private final ShadowStats stats
5152
private final String encoding
5253
private final GradleVersionUtil versionUtil
54+
private final boolean preserveFileTimestamps
5355

5456
ShadowCopyAction(File zipFile, ZipCompressor compressor, DocumentationRegistry documentationRegistry,
5557
String encoding, List<Transformer> transformers, List<Relocator> relocators,
56-
PatternSet patternSet, ShadowStats stats, GradleVersionUtil util) {
58+
PatternSet patternSet, ShadowStats stats, GradleVersionUtil util,
59+
boolean preserveFileTimestamps) {
5760

5861
this.zipFile = zipFile
5962
this.compressor = compressor
@@ -64,6 +67,7 @@ class ShadowCopyAction implements CopyAction {
6467
this.stats = stats
6568
this.encoding = encoding
6669
this.versionUtil = util
70+
this.preserveFileTimestamps = preserveFileTimestamps
6771
}
6872

6973
@Override
@@ -109,6 +113,17 @@ class ShadowCopyAction implements CopyAction {
109113
}
110114
}
111115

116+
private long getArchiveTimeFor(long timestamp) {
117+
return preserveFileTimestamps ? timestamp : CONSTANT_TIME_FOR_ZIP_ENTRIES
118+
}
119+
120+
private ZipEntry setArchiveTimes(ZipEntry zipEntry) {
121+
if (!preserveFileTimestamps) {
122+
zipEntry.setTime(CONSTANT_TIME_FOR_ZIP_ENTRIES)
123+
}
124+
return zipEntry
125+
}
126+
112127
private static <T extends Closeable> void withResource(T resource, Action<? super T> action) {
113128
try {
114129
action.execute(resource)
@@ -176,7 +191,7 @@ class ShadowCopyAction implements CopyAction {
176191
if (!isTransformable(fileDetails)) {
177192
String mappedPath = remapper.map(fileDetails.relativePath.pathString)
178193
ZipEntry archiveEntry = new ZipEntry(mappedPath)
179-
archiveEntry.setTime(fileDetails.lastModified)
194+
archiveEntry.setTime(getArchiveTimeFor(fileDetails.lastModified))
180195
archiveEntry.unixMode = (UnixStat.FILE_FLAG | fileDetails.mode)
181196
zipOutStr.putNextEntry(archiveEntry)
182197
fileDetails.copyTo(zipOutStr)
@@ -248,7 +263,8 @@ class ShadowCopyAction implements CopyAction {
248263

249264
private void remapClass(RelativeArchivePath file, ZipFile archive) {
250265
if (file.classFile) {
251-
addParentDirectories(new RelativeArchivePath(new ZipEntry(remapper.mapPath(file) + '.class'), null))
266+
ZipEntry zipEntry = setArchiveTimes(new ZipEntry(remapper.mapPath(file) + '.class'))
267+
addParentDirectories(new RelativeArchivePath(zipEntry, null))
252268
InputStream is = archive.getInputStream(file.entry)
253269
try {
254270
remapClass(is, file.pathString, file.entry.time)
@@ -292,7 +308,7 @@ class ShadowCopyAction implements CopyAction {
292308
try {
293309
// Now we put it back on so the class file is written out with the right extension.
294310
ZipEntry archiveEntry = new ZipEntry(mappedName + ".class")
295-
archiveEntry.setTime(lastModified)
311+
archiveEntry.setTime(getArchiveTimeFor(lastModified))
296312
zipOutStr.putNextEntry(archiveEntry)
297313
IOUtils.copyLarge(bis, zipOutStr)
298314
zipOutStr.closeEntry()
@@ -306,7 +322,7 @@ class ShadowCopyAction implements CopyAction {
306322
private void copyArchiveEntry(RelativeArchivePath archiveFile, ZipFile archive) {
307323
String mappedPath = remapper.map(archiveFile.entry.name)
308324
ZipEntry entry = new ZipEntry(mappedPath)
309-
entry.setTime(archiveFile.entry.time)
325+
entry.setTime(getArchiveTimeFor(archiveFile.entry.time))
310326
RelativeArchivePath mappedFile = new RelativeArchivePath(entry, archiveFile.details)
311327
addParentDirectories(mappedFile)
312328
zipOutStr.putNextEntry(mappedFile.entry)
@@ -324,7 +340,7 @@ class ShadowCopyAction implements CopyAction {
324340
// Trailing slash in name indicates that entry is a directory
325341
String path = dirDetails.relativePath.pathString + '/'
326342
ZipEntry archiveEntry = new ZipEntry(path)
327-
archiveEntry.setTime(dirDetails.lastModified)
343+
archiveEntry.setTime(getArchiveTimeFor(dirDetails.lastModified))
328344
archiveEntry.unixMode = (UnixStat.DIR_FLAG | dirDetails.mode)
329345
zipOutStr.putNextEntry(archiveEntry)
330346
zipOutStr.closeEntry()
@@ -386,7 +402,7 @@ class ShadowCopyAction implements CopyAction {
386402
} else {
387403
//Parent is always a directory so add / to the end of the path
388404
String path = segments[0..-2].join('/') + '/'
389-
return new RelativeArchivePath(new ZipEntry(path), null)
405+
return new RelativeArchivePath(setArchiveTimes(new ZipEntry(path)), null)
390406
}
391407
}
392408
}

src/main/groovy/com/github/jengelman/gradle/plugins/shadow/tasks/ShadowJar.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected CopyAction createCopyAction() {
6363
DocumentationRegistry documentationRegistry = getServices().get(DocumentationRegistry.class);
6464
return new ShadowCopyAction(getArchivePath(), getInternalCompressor(), documentationRegistry,
6565
this.getMetadataCharset(), transformers, relocators, getRootPatternSet(), shadowStats,
66-
versionUtil);
66+
versionUtil, isPreserveFileTimestamps());
6767
}
6868

6969
@Internal

0 commit comments

Comments
 (0)