@@ -40,6 +40,7 @@ import java.util.zip.ZipException
40
40
41
41
@Slf4j
42
42
class ShadowCopyAction implements CopyAction {
43
+ private static final long CONSTANT_TIME_FOR_ZIP_ENTRIES = (new GregorianCalendar (1980 , 1 , 1 , 0 , 0 , 0 )). getTimeInMillis()
43
44
44
45
private final File zipFile
45
46
private final ZipCompressor compressor
@@ -50,10 +51,12 @@ class ShadowCopyAction implements CopyAction {
50
51
private final ShadowStats stats
51
52
private final String encoding
52
53
private final GradleVersionUtil versionUtil
54
+ private final boolean preserveFileTimestamps
53
55
54
56
ShadowCopyAction (File zipFile , ZipCompressor compressor , DocumentationRegistry documentationRegistry ,
55
57
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 ) {
57
60
58
61
this . zipFile = zipFile
59
62
this . compressor = compressor
@@ -64,6 +67,7 @@ class ShadowCopyAction implements CopyAction {
64
67
this . stats = stats
65
68
this . encoding = encoding
66
69
this . versionUtil = util
70
+ this . preserveFileTimestamps = preserveFileTimestamps
67
71
}
68
72
69
73
@Override
@@ -109,6 +113,17 @@ class ShadowCopyAction implements CopyAction {
109
113
}
110
114
}
111
115
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
+
112
127
private static <T extends Closeable > void withResource (T resource , Action<? super T> action ) {
113
128
try {
114
129
action. execute(resource)
@@ -176,7 +191,7 @@ class ShadowCopyAction implements CopyAction {
176
191
if (! isTransformable(fileDetails)) {
177
192
String mappedPath = remapper. map(fileDetails. relativePath. pathString)
178
193
ZipEntry archiveEntry = new ZipEntry (mappedPath)
179
- archiveEntry. setTime(fileDetails. lastModified)
194
+ archiveEntry. setTime(getArchiveTimeFor( fileDetails. lastModified) )
180
195
archiveEntry. unixMode = (UnixStat . FILE_FLAG | fileDetails. mode)
181
196
zipOutStr. putNextEntry(archiveEntry)
182
197
fileDetails. copyTo(zipOutStr)
@@ -248,7 +263,8 @@ class ShadowCopyAction implements CopyAction {
248
263
249
264
private void remapClass (RelativeArchivePath file , ZipFile archive ) {
250
265
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 ))
252
268
InputStream is = archive. getInputStream(file. entry)
253
269
try {
254
270
remapClass(is, file. pathString, file. entry. time)
@@ -292,7 +308,7 @@ class ShadowCopyAction implements CopyAction {
292
308
try {
293
309
// Now we put it back on so the class file is written out with the right extension.
294
310
ZipEntry archiveEntry = new ZipEntry (mappedName + " .class" )
295
- archiveEntry. setTime(lastModified)
311
+ archiveEntry. setTime(getArchiveTimeFor( lastModified) )
296
312
zipOutStr. putNextEntry(archiveEntry)
297
313
IOUtils . copyLarge(bis, zipOutStr)
298
314
zipOutStr. closeEntry()
@@ -306,7 +322,7 @@ class ShadowCopyAction implements CopyAction {
306
322
private void copyArchiveEntry (RelativeArchivePath archiveFile , ZipFile archive ) {
307
323
String mappedPath = remapper. map(archiveFile. entry. name)
308
324
ZipEntry entry = new ZipEntry (mappedPath)
309
- entry. setTime(archiveFile. entry. time)
325
+ entry. setTime(getArchiveTimeFor( archiveFile. entry. time) )
310
326
RelativeArchivePath mappedFile = new RelativeArchivePath (entry, archiveFile. details)
311
327
addParentDirectories(mappedFile)
312
328
zipOutStr. putNextEntry(mappedFile. entry)
@@ -324,7 +340,7 @@ class ShadowCopyAction implements CopyAction {
324
340
// Trailing slash in name indicates that entry is a directory
325
341
String path = dirDetails. relativePath. pathString + ' /'
326
342
ZipEntry archiveEntry = new ZipEntry (path)
327
- archiveEntry. setTime(dirDetails. lastModified)
343
+ archiveEntry. setTime(getArchiveTimeFor( dirDetails. lastModified) )
328
344
archiveEntry. unixMode = (UnixStat . DIR_FLAG | dirDetails. mode)
329
345
zipOutStr. putNextEntry(archiveEntry)
330
346
zipOutStr. closeEntry()
@@ -386,7 +402,7 @@ class ShadowCopyAction implements CopyAction {
386
402
} else {
387
403
// Parent is always a directory so add / to the end of the path
388
404
String path = segments[0 .. -2 ]. join(' /' ) + ' /'
389
- return new RelativeArchivePath (new ZipEntry (path), null )
405
+ return new RelativeArchivePath (setArchiveTimes( new ZipEntry (path) ), null )
390
406
}
391
407
}
392
408
}
0 commit comments