diff --git a/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java b/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java index 9ec8f55..5595a91 100644 --- a/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java +++ b/src/main/java/com/inteligr8/maven/ban/PurgeRepoMojo.java @@ -158,14 +158,14 @@ public class PurgeRepoMojo extends AbstractMojo { this.getLog().info("DRYRUN: Would have deleted certain paths from local Maven cache: " + repoPath); this.getLog().info("DRYRUN: Would have deleted these paths: " + includePaths); } else { - for (Path path : includePaths) { - Path fullpath = repoPath.resolve(path); - if (Files.exists(fullpath)) { - this.getLog().info("Deleting version from Maven cache: " + path); - Files.walkFileTree(fullpath, new DeleteDirectoryVisitor()); + for (Path versionPath : includePaths) { + Path fullVersionPath = repoPath.resolve(versionPath); + if (Files.exists(fullVersionPath)) { + this.getLog().info("Deleting version from Maven cache: " + versionPath); + Files.walkFileTree(fullVersionPath, new DeleteNonMetadataVisitor()); } else { // this will probably never happen - this.getLog().debug("Maven cache does not exist: " + path); + this.getLog().debug("Maven cache does not exist: " + versionPath); } } } @@ -279,7 +279,9 @@ public class PurgeRepoMojo extends AbstractMojo { - private class DeleteDirectoryVisitor implements FileVisitor { + private class DeleteNonMetadataVisitor implements FileVisitor { + + private final Pattern versionPathPattern = Pattern.compile("/([^/]+)/([^/]+)$"); @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { @@ -288,12 +290,30 @@ public class PurgeRepoMojo extends AbstractMojo { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - try { - if (!attrs.isDirectory()) + if (attrs.isDirectory()) { + getLog().warn("An unexpected directory was found: " + file); + return FileVisitResult.SKIP_SUBTREE; + } + + Matcher matcher = this.versionPathPattern.matcher(file.toString()); + if (!matcher.find()) { + getLog().debug("Ignoring file from purge: " + file); + return FileVisitResult.CONTINUE; + } + + String artifactId = matcher.group(1); + String version = matcher.group(2); + String includeName = artifactId + "-" + version; + String excludeName = artifactId + "-" + version + ".pom"; + if (file.getFileName().toString().startsWith(includeName) && + !file.getFileName().toString().startsWith(excludeName)) { + try { Files.delete(file); - } catch (IOException ie) { - getLog().debug(ie); - getLog().warn("The file failed to delete: " + file); + } catch (IOException ie) { + getLog().debug(ie); + getLog().warn("The file failed to delete: " + file); + return FileVisitResult.SKIP_SIBLINGS; + } } return FileVisitResult.CONTINUE;