This is an automated email from the ASF dual-hosted git repository.
slachiewicz pushed a commit to branch maven-resolver-1.9.x
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/maven-resolver-1.9.x by this
push:
new 00164f1a9 [1.9.x] Keep the last modified time of files downloaded on
Windows
00164f1a9 is described below
commit 00164f1a9e6bd38abaedf5764dfb0361a3b95c54
Author: Alexander Bartash <[email protected]>
AuthorDate: Sun Nov 15 18:59:38 2026 +0200
[1.9.x] Keep the last modified time of files downloaded on Windows
On Windows a collocated temp file is copied over its target with a stream
copy instead of being moved, so the target got the current time. The time
that the HTTP transport had set on the temp file from the "Last-Modified"
header (MRESOLVER-393) was lost, and every file in the local repository
had its download time. On other systems the move keeps the time.
The copy now sets the last modified time of the target to that of the
temp file, so the local repository holds the same times on all systems.
If the time cannot be set, the download does not fail and the target
keeps the current time, as before. The way the content is written on
Windows does not change.
Tools that read the time are affected by the old behaviour, for example
the minimumReleaseAge filter of versions-maven-plugin treated every newly
downloaded version as published at the moment of the download.
Fixes #2317
Generated-by: Claude Code (claude-fable-5-2)
---
.../java/org/eclipse/aether/util/FileUtils.java | 13 ++-
.../org/eclipse/aether/util/FileUtilsTest.java | 120 +++++++++++++++++++++
2 files changed, 132 insertions(+), 1 deletion(-)
diff --git
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java
index 0a430f89f..0c595a029 100644
--- a/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java
+++ b/maven-resolver-util/src/main/java/org/eclipse/aether/util/FileUtils.java
@@ -142,8 +142,14 @@ public final class FileUtils {
/**
* On Windows we use pre-NIO2 way to copy files, as for some reason it
works. Beat me why.
+ * <p>
+ * The target gets the last modified time of the source,
+ * which the transports set from the remote "Last-Modified" header,
+ * as it does with the move used on other systems.
+ * <p>
+ * Package-private for tests, which call it on every system.
*/
- private static void copy(Path source, Path target) throws IOException {
+ static void copy(Path source, Path target) throws IOException {
ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
byte[] array = buffer.array();
try (InputStream is = Files.newInputStream(source);
@@ -156,6 +162,11 @@ public final class FileUtils {
os.write(array, 0, bytes);
}
}
+ try {
+ Files.setLastModifiedTime(target,
Files.getLastModifiedTime(source));
+ } catch (IOException e) {
+ // the content is copied: the target keeps the current time, as it
did before the time was copied
+ }
}
/**
diff --git
a/maven-resolver-util/src/test/java/org/eclipse/aether/util/FileUtilsTest.java
b/maven-resolver-util/src/test/java/org/eclipse/aether/util/FileUtilsTest.java
new file mode 100644
index 000000000..8902e3ff5
--- /dev/null
+++
b/maven-resolver-util/src/test/java/org/eclipse/aether/util/FileUtilsTest.java
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.eclipse.aether.util;
+
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.attribute.FileTime;
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+public class FileUtilsTest {
+ @Rule
+ public TemporaryFolder temporaryFolder = new TemporaryFolder();
+
+ /**
+ * A time in the past, in whole seconds, as file systems differ in the
precision they store.
+ */
+ private static final FileTime LAST_MODIFIED =
+ FileTime.from(System.currentTimeMillis() / 1000 -
TimeUnit.DAYS.toSeconds(12), TimeUnit.SECONDS);
+
+ @Test
+ public void collocatedTempFileMoveKeepsLastModifiedOfNewFile() throws
IOException {
+ Path target = temporaryFolder.getRoot().toPath().resolve("target.txt");
+
+ moveWithLastModified(target, "content");
+
+ assertEquals("content", read(target));
+ assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+ }
+
+ @Test
+ public void collocatedTempFileMoveKeepsLastModifiedOfReplacedFile() throws
IOException {
+ Path target = temporaryFolder.newFile("target.txt").toPath();
+ Files.write(target, "old content".getBytes(StandardCharsets.UTF_8));
+
+ moveWithLastModified(target, "new content");
+
+ assertEquals("new content", read(target));
+ assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+ }
+
+ /**
+ * The move uses the copy only on Windows: this test calls it directly to
cover it on every system.
+ */
+ @Test
+ public void copyKeepsLastModifiedOfNewFile() throws IOException {
+ Path source = sourceWithLastModified("content");
+ Path target = temporaryFolder.getRoot().toPath().resolve("target.txt");
+
+ FileUtils.copy(source, target);
+
+ assertEquals("content", read(target));
+ assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+ }
+
+ /**
+ * The move uses the copy only on Windows: this test calls it directly to
cover it on every system.
+ */
+ @Test
+ public void copyKeepsLastModifiedOfReplacedFile() throws IOException {
+ Path source = sourceWithLastModified("new content");
+ Path target = temporaryFolder.newFile("target.txt").toPath();
+ Files.write(target, "old content".getBytes(StandardCharsets.UTF_8));
+
+ FileUtils.copy(source, target);
+
+ assertEquals("new content", read(target));
+ assertEquals(LAST_MODIFIED, Files.getLastModifiedTime(target));
+ }
+
+ /**
+ * Writes the content the way the connector and the transports do: into a
collocated temp file, that gets the
+ * remote last modified time, and is then moved onto the target.
+ */
+ private static void moveWithLastModified(Path target, String content)
throws IOException {
+ Path tempPath;
+ try (FileUtils.CollocatedTempFile tempFile =
FileUtils.newTempFile(target)) {
+ tempPath = tempFile.getPath();
+ Files.write(tempPath, content.getBytes(StandardCharsets.UTF_8));
+ Files.setLastModifiedTime(tempPath, LAST_MODIFIED);
+ tempFile.move();
+ }
+ assertFalse(Files.exists(tempPath));
+ }
+
+ private Path sourceWithLastModified(String content) throws IOException {
+ Path source = temporaryFolder.newFile("source.txt").toPath();
+ Files.write(source, content.getBytes(StandardCharsets.UTF_8));
+ Files.setLastModifiedTime(source, LAST_MODIFIED);
+ return source;
+ }
+
+ private static String read(Path file) throws IOException {
+ return new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
+ }
+}