in src/main/java/org/apache/maven/buildcache/LocalCacheRepositoryImpl.java [185:219]
public void clearCache(CacheContext context) {
try {
final Path buildCacheDir = buildCacheDir(context);
Path artifactCacheDir = buildCacheDir.getParent();
if (!Files.exists(artifactCacheDir)) {
return;
}
List<Path> cacheDirs = new ArrayList<>();
try (DirectoryStream<Path> paths = Files.newDirectoryStream(artifactCacheDir)) {
for (Path dir : paths) {
if (Files.isDirectory(dir)) {
cacheDirs.add(dir);
}
}
}
int maxLocalBuildsCached = cacheConfig.getMaxLocalBuildsCached() - 1;
if (cacheDirs.size() > maxLocalBuildsCached) {
cacheDirs.sort(Comparator.comparing(LocalCacheRepositoryImpl::lastModifiedTime));
for (Path dir : cacheDirs.subList(0, cacheDirs.size() - maxLocalBuildsCached)) {
FileUtils.deleteDirectory(dir.toFile());
}
}
final Path path = localBuildDir(context);
if (Files.exists(path)) {
FileUtils.deleteDirectory(path.toFile());
}
} catch (IOException e) {
final String artifactId = context.getProject().getArtifactId();
throw new RuntimeException(
"Failed to cleanup local cache of " + artifactId + " on build failure, it might be inconsistent",
e);
}
}