Skip to content

Commit d75ded5

Browse files
committed
Add explicit isDirectory flag to buildinfo.xml schema for robust artifact detection
Replaces path-based heuristics with an explicit boolean flag to determine if a cached artifact is a directory (e.g., target/classes) or regular file. Changes: - build-cache-build.mdo: Added isDirectory field to Artifact class - CacheControllerImpl.java: * artifactDto(): Set isDirectory=true for directory artifacts * restoreArtifactToDisk(): Use artifact.isIsDirectory() instead of path heuristics Benefits: - More robust: Works with any custom packaging type or path structure - More maintainable: Single source of truth instead of maintaining heuristic rules - Backward compatible: Missing flag defaults to false (regular file) - Cleaner code: Replaced 10 lines of path-checking logic with 1-line boolean check Example buildinfo.xml output: <artifact> ... <filePath>target/classes</filePath> <isDirectory>true</isDirectory> </artifact> All tests passing: 74 unit tests + 26 integration tests
1 parent 11cd70b commit d75ded5

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

src/main/java/org/apache/maven/buildcache/CacheControllerImpl.java

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,9 @@ private UnaryOperator<File> createRestorationToDiskConsumer(final MavenProject p
358358
* Directory artifacts (cached as zips) are unzipped back to their original directory structure.
359359
*/
360360
private void restoreArtifactToDisk(File cachedFile, Artifact artifact, Path restorationPath) throws IOException {
361-
// Check if this is a directory artifact by examining the filePath from buildinfo.
362-
// Directory artifacts have filePath like "target/classes" instead of "target/foo.jar"
363-
// They are saved as zips but named with .jar extension (e.g., duplicate.jar)
364-
String filePath = artifact.getFilePath();
365-
boolean isDirectoryArtifact = filePath != null
366-
&& (filePath.equals("target/classes")
367-
|| filePath.equals("target/test-classes")
368-
|| (!filePath.endsWith(".jar")
369-
&& !filePath.endsWith(".zip")
370-
&& !filePath.endsWith(".war")
371-
&& !filePath.endsWith(".ear")
372-
&& !filePath.endsWith(".pom")));
373-
374-
if (isDirectoryArtifact) {
361+
// Check the explicit isDirectory flag set during save.
362+
// Directory artifacts (e.g., target/classes) are saved as zips and need to be unzipped on restore.
363+
if (artifact.isIsDirectory()) {
375364
restoreDirectoryArtifact(cachedFile, artifact, restorationPath);
376365
} else {
377366
restoreRegularFileArtifact(cachedFile, artifact, restorationPath);
@@ -809,6 +798,9 @@ private Artifact artifactDto(
809798
if (Files.isRegularFile(file)) {
810799
dto.setFileHash(algorithm.hash(file));
811800
dto.setFileSize(Files.size(file));
801+
} else if (Files.isDirectory(file)) {
802+
// Mark directory artifacts explicitly so we can unzip them on restore
803+
dto.setIsDirectory(true);
812804
}
813805

814806
// Always set filePath (needed for artifact restoration)

src/main/mdo/build-cache-build.mdo

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,12 @@ under the License.
244244
<name>filePath</name>
245245
<type>String</type>
246246
</field>
247+
<!--xs:element name="isDirectory" type="xs:boolean" minOccurs="0"/-->
248+
<field>
249+
<name>isDirectory</name>
250+
<type>boolean</type>
251+
<description>Indicates if this artifact represents a directory (e.g., target/classes) that was zipped for caching</description>
252+
</field>
247253
<!--/xs:sequence-->
248254
</fields>
249255
<!--/xs:complexType-->

0 commit comments

Comments
 (0)