Skip to content

Commit 73ee048

Browse files
authored
File#lastModified on Linux JDK8 loses milliseconds precision (JDK-8177809)
2 parents b579cb1 + e55f320 commit 73ee048

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/main/java/org/codehaus/plexus/archiver/AbstractArchiver.java

+23-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.IOException;
2222
import java.lang.reflect.UndeclaredThrowableException;
2323
import java.nio.charset.Charset;
24+
import java.nio.file.Files;
2425
import java.util.ArrayList;
2526
import java.util.Comparator;
2627
import java.util.Date;
@@ -940,12 +941,12 @@ protected boolean isUptodate()
940941
throws ArchiverException
941942
{
942943
final File zipFile = getDestFile();
943-
final long destTimestamp = zipFile.lastModified();
944-
if ( destTimestamp == 0 )
944+
if ( !zipFile.exists() )
945945
{
946946
getLogger().debug( "isUp2date: false (Destination " + zipFile.getPath() + " not found.)" );
947947
return false; // File doesn't yet exist
948948
}
949+
final long destTimestamp = getFileLastModifiedTime(zipFile);
949950

950951
final Iterator it = resources.iterator();
951952
if ( !it.hasNext() )
@@ -994,6 +995,26 @@ else if ( o instanceof AddedResourceCollection )
994995
return true;
995996
}
996997

998+
/**
999+
* Returns the last modified time in milliseconds of a file.
1000+
* It avoids the bug where milliseconds precision is lost on File#lastModified (JDK-8177809) on JDK8 and Linux.
1001+
* @param file The file where the last modified time will be returned for.
1002+
* @return The last modified time in milliseconds of the file.
1003+
* @throws ArchiverException In the case of an IOException, for example when the file does not exists.
1004+
*/
1005+
private long getFileLastModifiedTime( File file )
1006+
throws ArchiverException
1007+
{
1008+
try
1009+
{
1010+
return Files.getLastModifiedTime( file.toPath() ).toMillis();
1011+
}
1012+
catch ( IOException e )
1013+
{
1014+
throw new ArchiverException( e.getMessage(), e );
1015+
}
1016+
}
1017+
9971018
protected boolean checkForced()
9981019
throws ArchiverException
9991020
{

0 commit comments

Comments
 (0)