erik-meuwese-topicus commented on code in PR #394:
URL: 
https://github.com/apache/maven-build-cache-extension/pull/394#discussion_r2567827336


##########
src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java:
##########
@@ -567,6 +660,58 @@ public void save(
             } catch (Exception ex) {
                 LOGGER.error("Failed to clean cache due to unexpected error:", 
ex);
             }
+        } finally {
+            // Cleanup project state to free memory, but preserve 
stagingDirectory for restore
+            // Note: stagingDirectory must persist until 
restoreStagedArtifacts() is called
+            state.attachedResourcesPathsById.clear();
+            state.attachedResourceCounter = 0;
+            state.restoredOutputClassifiers.clear();
+            // stagingDirectory is NOT cleared here - it's cleared in 
restoreStagedArtifacts()
+        }
+    }
+
+    /**
+     * Saves a project artifact to cache, handling both regular files and 
directory artifacts.
+     * Directory artifacts (e.g., target/classes from compile-only builds) are 
zipped before saving
+     * since Files.copy() cannot handle directories.
+     */
+    private void saveProjectArtifact(
+            CacheResult cacheResult, org.apache.maven.artifact.Artifact 
projectArtifact, MavenProject project)
+            throws IOException {
+        File originalFile = projectArtifact.getFile();
+        try {
+            if (originalFile.isDirectory()) {
+                saveDirectoryArtifact(cacheResult, projectArtifact, project, 
originalFile);
+            } else {
+                // Regular file (JAR/WAR) - save directly
+                localCache.saveArtifactFile(cacheResult, projectArtifact);
+            }
+        } finally {
+            // Restore original file reference in case it was temporarily 
changed
+            projectArtifact.setFile(originalFile);
+        }
+    }
+
+    /**
+     * Saves a directory artifact by zipping it first, then saving the zip to 
cache.
+     */
+    private void saveDirectoryArtifact(
+            CacheResult cacheResult,
+            org.apache.maven.artifact.Artifact projectArtifact,
+            MavenProject project,
+            File originalFile)
+            throws IOException {
+        Path tempZip = Files.createTempFile("maven-cache-", "-" + 
project.getArtifactId() + ".zip");
+        boolean hasFiles = CacheUtils.zip(originalFile.toPath(), tempZip, "*");

Review Comment:
   `CacheUtils.zip(originalFile.toPath(), tempZip, "*")` misses the last 
parameter `preservePermissions` whether to preserve Unix file permissions in 
the zip.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to