Fix timestamp preservation when extracting cached files
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
CacheUtils.zip()did not store directory entries with their timestamps - only files were added to the ZIPCacheUtils.unzip()set directory timestamps immediately during extraction, but these timestamps were subsequently overwritten whenFiles.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 fromBasicFileAttributes
CacheUtils.unzip()
- Introduced
Map<Path, Long> directoryTimestampsto 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
HashMapandMapimports 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.
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?
feedback provided in #388
@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.
Can you resolve conflicts and repush?
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.