Skip to content

Commit

Permalink
[MSHADE-420] fix time when read from extra field
Browse files Browse the repository at this point in the history
  • Loading branch information
hboutemy committed Jan 30, 2024
1 parent 0fe7700 commit 9e53c7e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,11 @@
<version>3.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.23.0</version>
</dependency>
</dependencies>

<build>
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/org/apache/maven/plugins/shade/DefaultShader.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TimeZone;
import java.util.concurrent.Callable;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand All @@ -55,6 +56,9 @@

import org.apache.commons.collections4.MultiValuedMap;
import org.apache.commons.collections4.multimap.HashSetValuedHashMap;
import org.apache.commons.compress.archivers.zip.ExtraFieldUtils;
import org.apache.commons.compress.archivers.zip.X5455_ExtendedTimestamp;
import org.apache.commons.compress.archivers.zip.ZipExtraField;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugins.shade.filter.Filter;
import org.apache.maven.plugins.shade.relocation.Relocator;
Expand Down Expand Up @@ -89,6 +93,25 @@ public DefaultShader(final Logger logger) {
this.logger = Objects.requireNonNull(logger);
}

// workaround for MSHADE-420
private long getTime(ZipEntry entry) {
if (entry.getExtra() != null) {
try {
ZipExtraField[] fields =
ExtraFieldUtils.parse(entry.getExtra(), true, ExtraFieldUtils.UnparseableExtraField.SKIP);
for (ZipExtraField field : fields) {
if (X5455_ExtendedTimestamp.HEADER_ID.equals(field.getHeaderId())) {
// extended timestamp extra field: need to translate UTC to local time for Reproducible Builds
return entry.getTime() - TimeZone.getDefault().getRawOffset();
}
}
} catch (ZipException ze) {
// ignore
}
}
return entry.getTime();
}

public void shade(ShadeRequest shadeRequest) throws IOException, MojoExecutionException {
Set<String> resources = new HashSet<>();

Expand Down Expand Up @@ -332,7 +355,7 @@ public InputStream call() throws Exception {
}
},
name,
entry.getTime(),
getTime(entry),
entry.getMethod());
} catch (Exception e) {
throw new IOException(String.format("Problem shading JAR %s entry %s: %s", jar, name, e), e);
Expand Down Expand Up @@ -423,7 +446,7 @@ private void goThroughAllJarEntriesForManifestTransformer(
resources.add(resource);
try (InputStream inputStream = jarFile.getInputStream(entry)) {
manifestTransformer.processResource(
resource, inputStream, shadeRequest.getRelocators(), entry.getTime());
resource, inputStream, shadeRequest.getRelocators(), getTime(entry));
}
break;
}
Expand Down

0 comments on commit 9e53c7e

Please sign in to comment.