From e55f320f190bc5fe2b1029c4fd8bb3e294a299bc Mon Sep 17 00:00:00 2001 From: Martin Kanters Date: Thu, 24 Sep 2020 21:33:43 +0200 Subject: [PATCH] File#lastModified on Linux JDK8 loses milliseconds precision (JDK-8177809). --- .../plexus/archiver/AbstractArchiver.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java b/src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java index 7b38a937f..5cdecfa8c 100755 --- a/src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java +++ b/src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.lang.reflect.UndeclaredThrowableException; import java.nio.charset.Charset; +import java.nio.file.Files; import java.util.ArrayList; import java.util.Comparator; import java.util.Date; @@ -940,12 +941,12 @@ protected boolean isUptodate() throws ArchiverException { final File zipFile = getDestFile(); - final long destTimestamp = zipFile.lastModified(); - if ( destTimestamp == 0 ) + if ( !zipFile.exists() ) { getLogger().debug( "isUp2date: false (Destination " + zipFile.getPath() + " not found.)" ); return false; // File doesn't yet exist } + final long destTimestamp = getFileLastModifiedTime(zipFile); final Iterator it = resources.iterator(); if ( !it.hasNext() ) @@ -994,6 +995,26 @@ else if ( o instanceof AddedResourceCollection ) return true; } + /** + * Returns the last modified time in milliseconds of a file. + * It avoids the bug where milliseconds precision is lost on File#lastModified (JDK-8177809) on JDK8 and Linux. + * @param file The file where the last modified time will be returned for. + * @return The last modified time in milliseconds of the file. + * @throws ArchiverException In the case of an IOException, for example when the file does not exists. + */ + private long getFileLastModifiedTime( File file ) + throws ArchiverException + { + try + { + return Files.getLastModifiedTime( file.toPath() ).toMillis(); + } + catch ( IOException e ) + { + throw new ArchiverException( e.getMessage(), e ); + } + } + protected boolean checkForced() throws ArchiverException {