diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java index 9b1615f76..de2d4893d 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/main/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurge.java @@ -36,8 +36,13 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -400,9 +405,7 @@ public abstract class AbstractRepositoryPurge try { - Files.find( parentDir, 3, - ( path, basicFileAttributes ) -> path.getFileName( ).toString( ).startsWith( artifactName ) - && Files.isRegularFile( path ) ).forEach( this::deleteSilently ); + deleteArtifactFiles( parentDir, 3, artifactName ); } catch ( IOException e ) { @@ -411,6 +414,29 @@ public abstract class AbstractRepositoryPurge } + public static void deleteArtifactFiles(final Path directory, final int maxDepth, final String artifactName) throws IOException + { + Files.walkFileTree(directory, new HashSet( ), maxDepth, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile( Path file, BasicFileAttributes attrs) throws IOException { + if (file.getFileName().toString().startsWith(artifactName)) { + try { + Files.delete( file ); + } catch (IOException e) { + // We do not stop, if an error occurs. + } + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + }); + } + private void triggerAuditEvent( String repoId, String resource, String action ) { String msg = diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java index ac689847a..394093fc7 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/AbstractArtifactConsumerTest.java @@ -19,6 +19,7 @@ package org.apache.archiva.consumers.core; * under the License. */ +import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.common.utils.BaseFile; import org.apache.archiva.configuration.ArchivaConfiguration; import org.apache.archiva.configuration.FileType; diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java index 0e599833f..4df51b6f7 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/AbstractRepositoryPurgeTest.java @@ -20,9 +20,11 @@ package org.apache.archiva.consumers.core.repository; */ import org.apache.archiva.admin.model.beans.ManagedRepository; +import org.apache.archiva.common.plexusbridge.PlexusSisuBridge; import org.apache.archiva.metadata.model.ArtifactMetadata; import org.apache.archiva.metadata.repository.MetadataRepository; import org.apache.archiva.metadata.repository.RepositorySession; +import org.apache.archiva.metadata.repository.storage.maven2.ArtifactMappingProvider; import org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryPathTranslator; import org.apache.archiva.repository.ManagedRepositoryContent; import org.apache.archiva.repository.events.RepositoryListener; @@ -43,12 +45,16 @@ import org.springframework.test.context.ContextConfiguration; import javax.inject.Inject; import java.io.File; import java.io.IOException; +import java.nio.file.FileVisitOption; +import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; -import java.util.stream.Collectors; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -231,13 +237,27 @@ public abstract class AbstractRepositoryPurgeTest return StringUtils.substringAfterLast( getClass().getName(), "." ); } - protected List getArtifactMetadataFromDir( String repoId, String projectName, Path repoDir, Path vDir ) throws IOException + protected List getArtifactMetadataFromDir( final String repoId, final String projectName, final Path repoDir, final Path vDir ) throws IOException { - Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList<>( ) ); - return Files.find(vDir, 1, - (path, basicFileAttributes) -> basicFileAttributes.isRegularFile() && path.getFileName().toString().startsWith(projectName)) - .map( path -> - translator.getArtifactForPath( repoId, repoDir.relativize( path ).toString() ) - ).collect( Collectors.toList()); + final Maven2RepositoryPathTranslator translator = new Maven2RepositoryPathTranslator( new ArrayList( ) ); + final List result = new ArrayList<>( ); + Files.walkFileTree(vDir, new HashSet( ), 1, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile( Path file, BasicFileAttributes attrs) throws IOException { + if (file.getFileName().toString().startsWith(projectName)) { + ArtifactMetadata m = translator.getArtifactForPath( repoId, repoDir.relativize( file ).toString() ); + result.add(m); + } + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { + return FileVisitResult.CONTINUE; + } + + }); + return result; } + } diff --git a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java index b4ef96da5..f444e5abc 100644 --- a/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java +++ b/archiva-modules/archiva-base/archiva-consumers/archiva-core-consumers/src/test/java/org/apache/archiva/consumers/core/repository/RepositoryPurgeConsumerTest.java @@ -386,8 +386,8 @@ public class RepositoryPurgeConsumerTest verify(metadataRepository, never()).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion)); ArgumentCaptor metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, never()).removeArtifact(any(), any()); - verify(metadataRepository, never()).removeArtifact( any(), any(), any(), any(), any(MetadataFacet.class) ); + verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class)); + verify(metadataRepository, never()).removeArtifact( any(String.class), any(String.class), any(String.class), any(String.class), any(MetadataFacet.class) ); // check if the snapshot wasn't removed @@ -447,7 +447,7 @@ public class RepositoryPurgeConsumerTest verify(metadataRepository, times(1)).removeProjectVersion(eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion)); ArgumentCaptor metadataArg = ArgumentCaptor.forClass(ArtifactMetadata.class); - verify(metadataRepository, never()).removeArtifact(any(), any()); + verify(metadataRepository, never()).removeArtifact(any(ArtifactMetadata.class), any(String.class)); // check if the snapshot was removed assertDeleted( projectRoot + "/2.3-SNAPSHOT" );