Skip to content

Commit

Permalink
Jar entries should not depend on user TZ (#185)
Browse files Browse the repository at this point in the history
This is a reproducibility related fix, as previous versions
of takari-lifecycle produced JARs were reproducible only in
same TZ as original. Fix was to use latest takari-archiver
1.0.3 that contains the fix.

Fixes #172
  • Loading branch information
cstamas authored Sep 9, 2024
1 parent 38c703c commit 79cf56c
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion takari-lifecycle-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@
<dependency>
<groupId>io.takari</groupId>
<artifactId>takari-archiver</artifactId>
<version>1.0.0</version>
<version>1.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public AggregateSource(List<Iterable<ExtendedArchiveEntry>> sources) {

@Override
public Iterable<ExtendedArchiveEntry> entries() {
final Predicate<ExtendedArchiveEntry> uniquePathFilter = new Predicate<ExtendedArchiveEntry>() {
final Predicate<ExtendedArchiveEntry> uniquePathFilter = new Predicate<>() {
private final Set<String> entryNames = new HashSet<>();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private Iterable<ExtendedArchiveEntry> jarManifestSource(MavenProject project) t
ByteArrayOutputStream buf = new ByteArrayOutputStream();
manifest.write(buf);

return singleton((ExtendedArchiveEntry) new BytesEntry(MANIFEST_PATH, buf.toByteArray()));
return singleton(new BytesEntry(MANIFEST_PATH, buf.toByteArray()));
}

protected ExtendedArchiveEntry pomPropertiesSource(MavenProject project) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import java.net.URLClassLoader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;
import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
Expand Down Expand Up @@ -204,7 +207,7 @@ private static void assertZipEntries(File zipFile, String... expected) throws IO
actualSb.append(entry.isDirectory() ? "D " : "F ")
.append(entry.getName())
.append(' ')
.append(entry.getTime())
.append(dosToJavaTime(entry.getTime()))
.append('\n');
}

Expand All @@ -217,6 +220,18 @@ private static void assertZipEntries(File zipFile, String... expected) throws IO
}
}

/**
* The method {@link ZipEntry#getTime()} "adjusts" the DOS timestamp ("local time") to Java time ("UTC") by using
* "default timezone" of the user. This means, that the method returns different timestamps in different Timezones.
* while the value to assert against is Java UTC time. Meaning, test would pass in some TZ but fail in others.
* Same applies to DST as well. This method merely "undoes" the adjustment, making it into UTC.
*/
private static long dosToJavaTime(long time) {
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.ROOT);
cal.setTimeInMillis(time);
return time + ((cal.get(Calendar.ZONE_OFFSET) + (cal.get(Calendar.DST_OFFSET))));
}

private static <T> List<T> toList(Enumeration<T> e) {
ArrayList<T> l = new ArrayList<T>();
while (e.hasMoreElements()) {
Expand Down

0 comments on commit 79cf56c

Please sign in to comment.