maven-build-cache-extension icon indicating copy to clipboard operation
maven-build-cache-extension copied to clipboard

Fix timestamp preservation when extracting cached files

Open cowwoc opened this issue 3 months ago • 5 comments

Problem

The Maven Build Cache Extension was not properly preserving file and directory timestamps when restoring attachedOutputs from cache. This caused Maven to warn about files being 'more recent than the packaged artifact' even after running mvnw verify.

Root Causes

  1. CacheUtils.zip() did not store directory entries with their timestamps - only files were added to the ZIP
  2. CacheUtils.unzip() set directory timestamps immediately during extraction, but these timestamps were subsequently overwritten when Files.copy() created files within those directories

Changes

CacheUtils.zip()

  • Added preVisitDirectory() override to store directory entries in the ZIP with their original timestamps
  • Modified visitFile() to explicitly set file entry timestamps from BasicFileAttributes

CacheUtils.unzip()

  • Introduced Map<Path, Long> directoryTimestamps to defer directory timestamp updates
  • Directory timestamps are now set after all files have been extracted, preventing them from being modified by subsequent file operations
  • Added HashMap and Map imports to support this functionality

Testing

Created comprehensive test that verifies:

  • Deep directory structures (a/b/c/)
  • Directory timestamp preservation
  • File timestamp preservation
  • Round-trip zip/unzip consistency

Impact

This fix ensures that cached build outputs maintain their original timestamps, eliminating spurious Maven warnings and improving build cache consistency across multi-module projects.

Fixes timestamp-related warnings like:

[WARNING] File 'formatter/api/target/classes/...' is more recent than the packaged artifact for 'module', please run a full 'mvn package' build

Related Issues and PRs

This PR improves the ZIP archive handling in CacheUtils:

  • #214 - Permissions lost when using attachedOutputs (similar ZIP limitation concerns)
  • #269 (MBUILDCACHE-116) - Extension fails to regenerate all files in target directory (restoration issues)
  • #318 (MBUILDCACHE-86) - Bugfix and enhancements with restoration of outputs on disk (file restoration improvements)
  • #330 (MBUILDCACHE-67) - Error handling during cache restoration (restoration reliability)
  • PR #23 - [SECURITY] Fix Zip Slip Vulnerability (previous security fix in ZIP handling)
  • PR #104 - Original implementation of attachedOutputs restoration

This fix specifically addresses timestamp preservation which was not covered by previous restoration improvements.

cowwoc avatar Oct 05 '25 21:10 cowwoc

This caused Maven to warn about files being 'more recent than the packaged artifact' even after running mvnw verify.

how do you have this error? is it possible to have a test for this change?

olamy avatar Oct 05 '25 22:10 olamy

feedback provided in #388

AlexanderAshitkin avatar Oct 06 '25 02:10 AlexanderAshitkin

@olamy @AlexanderAshitkin Please see the updated commit.

@AlexanderAshitkin Sorry for basing https://github.com/apache/maven-build-cache-extension/pull/388 on top of this PR. I have corrected the problem so they are now independent of each other.

@olamy https://github.com/apache/maven-build-cache-extension/pull/387/files#diff-090be5e498346368594f96199eab418ac5486df7368b0825ad1db7f63ef5fff1R214 contains instructions for reproducing the Maven warning message. Please let me know if you are able to reproduce it on your end. Thanks.

cowwoc avatar Oct 06 '25 15:10 cowwoc

Can you resolve conflicts and repush?

elharo avatar Oct 28 '25 11:10 elharo

Conflicts have been resolved and the branch has been rebased onto the latest master. The PR now includes both timestamp preservation (from this PR) and Unix permission preservation (from master) working together.

cowwoc avatar Oct 29 '25 17:10 cowwoc