@Test public void testReleasedSnapshotsExistsInDifferentRepo() throws Exception { RepositoryRegistry repositoryRegistry = applicationContext.getBean( ArchivaRepositoryRegistry.class); ManagedRepository managedRepository = repositoryRegistry.getManagedRepository( TEST_REPO_ID ); repositoryRegistry.removeRepository( TEST_REPO_ID ); repositoryRegistry.putRepository( getRepoConfiguration( TEST_REPO_ID, TEST_REPO_NAME )); repositoryRegistry.putRepository( getRepoConfiguration( RELEASES_TEST_REPO_ID, RELEASES_TEST_REPO_NAME )); String repoRoot = prepareTestRepos(); String projectNs = "org.apache.archiva"; String projectPath = projectNs.replaceAll("\\.","/"); String projectName = "released-artifact-in-diff-repo"; String projectVersion = "1.0-SNAPSHOT"; String releaseVersion = "1.0"; String projectRoot = repoRoot + "/" + projectPath+"/"+projectName; Path repo = getTestRepoRootPath(); Path vDir = repo.resolve(projectPath).resolve(projectName).resolve(projectVersion); Path releaseDir = repo.getParent().resolve(RELEASES_TEST_REPO_ID).resolve(projectPath).resolve(projectName).resolve(releaseVersion); Set<String> deletedVersions = new HashSet<>(); deletedVersions.add("1.0-SNAPSHOT"); listener.deleteArtifact( metadataRepository, getRepository().getId(), "org.apache.archiva", "released-artifact-in-diff-repo", "1.0-SNAPSHOT", "released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" ); listenerControl.replay(); List<ArtifactMetadata> ml = getArtifactMetadataFromDir(TEST_REPO_ID , projectName, repo.getParent(), vDir ); when(metadataRepository.getArtifacts(repositorySession , TEST_REPO_ID, projectNs, projectName, projectVersion )).thenReturn(ml); List<ArtifactMetadata> ml2 = getArtifactMetadataFromDir(RELEASES_TEST_REPO_ID , projectName, repo.getParent(), releaseDir ); when(metadataRepository.getArtifacts(repositorySession , RELEASES_TEST_REPO_ID, projectNs, projectName, releaseVersion )).thenReturn(ml2); repoPurge.process( PATH_TO_RELEASED_SNAPSHOT_IN_DIFF_REPO ); listenerControl.verify(); verify(metadataRepository, times(1)).removeProjectVersion(eq(repositorySession) , eq(TEST_REPO_ID), eq(projectNs), eq(projectName), eq(projectVersion) ); verify(metadataRepository, never()).removeProjectVersion(eq(repositorySession) , eq(RELEASES_TEST_REPO_ID), eq(projectNs), eq(projectName), eq(releaseVersion) ); assertDeleted( projectRoot + "/1.0-SNAPSHOT" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.md5" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.jar.sha1" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.md5" ); assertDeleted( projectRoot + "/1.0-SNAPSHOT/released-artifact-in-diff-repo-1.0-SNAPSHOT.pom.sha1" ); String releasesProjectRoot = AbstractRepositoryPurgeTest.fixPath( Paths.get( "target/test-" + getName() + "/releases-test-repo-one" ).toAbsolutePath().toString() + "/org/apache/archiva/released-artifact-in-diff-repo" ); assertExists( releasesProjectRoot + "/1.0" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.md5" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.jar.sha1" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.md5" ); assertExists( releasesProjectRoot + "/1.0/released-artifact-in-diff-repo-1.0.pom.sha1" ); applicationContext.getBean( ManagedRepositoryAdmin.class ).deleteManagedRepository( RELEASES_TEST_REPO_ID, null, false ); }
@Override public void process( String path ) throws RepositoryPurgeException { try { StorageAsset artifactFile = repository.getRepository( ).getRoot().resolve( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); if ( !artifactFile.exists() ) { return; } Artifact artifactRef = layout.getArtifact( path ); if ( !VersionUtil.isSnapshot( artifactRef.getVersion().getId( ) ) ) { return; } ItemSelector projectSelector = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace( ).getId() ) .withProjectId( artifactRef.getId( ) ) .build(); List<String> releasedVersions = new ArrayList<>( ); Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( ); for ( org.apache.archiva.repository.ManagedRepository repo : repos ) { if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { BaseRepositoryContentLayout repoContent = repo.getContent().getLayout( BaseRepositoryContentLayout.class ); Project proj = repoContent.getProject( projectSelector ); for ( Version version : repoContent.getVersions( proj ) ) { if ( !VersionUtil.isSnapshot( version.getId() ) ) { releasedVersions.add( version.getId() ); } } } } Collections.sort( releasedVersions, VersionComparator.getInstance( ) ); boolean needsMetadataUpdate = false; ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace().getId() ) .withProjectId( artifactRef.getId( ) ) .withArtifactId( artifactRef.getId( ) ); MetadataRepository metadataRepository = repositorySession.getRepository( ); if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion().getId( ) ) ) ) { ArchivaItemSelector selector = versionSelectorBuilder.withVersion( artifactRef.getVersion().getId( ) ).build( ); Version version = layout.getVersion( selector ); if (version.exists()) { repository.deleteItem( version ); } for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId( ), artifactFile.getName() ); } metadataRepository.removeProjectVersion( repositorySession, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId() ); needsMetadataUpdate = true; } if ( needsMetadataUpdate ) { updateMetadata( artifactRef ); } } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( MetadataRepositoryException e ) { log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } catch ( ItemNotFoundException e ) { log.error( "Could not find item to delete {}",e.getMessage( ), e ); } }
CleanupReleasedSnapshotsRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { StorageAsset artifactFile = repository.getRepository( ).getRoot().resolve( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); if ( !artifactFile.exists() ) { return; } Artifact artifactRef = layout.getArtifact( path ); if ( !VersionUtil.isSnapshot( artifactRef.getVersion().getId( ) ) ) { return; } ItemSelector projectSelector = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace( ).getId() ) .withProjectId( artifactRef.getId( ) ) .build(); List<String> releasedVersions = new ArrayList<>( ); Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( ); for ( org.apache.archiva.repository.ManagedRepository repo : repos ) { if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { BaseRepositoryContentLayout repoContent = repo.getContent().getLayout( BaseRepositoryContentLayout.class ); Project proj = repoContent.getProject( projectSelector ); for ( Version version : repoContent.getVersions( proj ) ) { if ( !VersionUtil.isSnapshot( version.getId() ) ) { releasedVersions.add( version.getId() ); } } } } Collections.sort( releasedVersions, VersionComparator.getInstance( ) ); boolean needsMetadataUpdate = false; ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace().getId() ) .withProjectId( artifactRef.getId( ) ) .withArtifactId( artifactRef.getId( ) ); MetadataRepository metadataRepository = repositorySession.getRepository( ); if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion().getId( ) ) ) ) { ArchivaItemSelector selector = versionSelectorBuilder.withVersion( artifactRef.getVersion().getId( ) ).build( ); Version version = layout.getVersion( selector ); if (version.exists()) { repository.deleteItem( version ); } for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId( ), artifactFile.getName() ); } metadataRepository.removeProjectVersion( repositorySession, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId() ); needsMetadataUpdate = true; } if ( needsMetadataUpdate ) { updateMetadata( artifactRef ); } } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( MetadataRepositoryException e ) { log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } catch ( ItemNotFoundException e ) { log.error( "Could not find item to delete {}",e.getMessage( ), e ); } } }
CleanupReleasedSnapshotsRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { StorageAsset artifactFile = repository.getRepository( ).getRoot().resolve( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); if ( !artifactFile.exists() ) { return; } Artifact artifactRef = layout.getArtifact( path ); if ( !VersionUtil.isSnapshot( artifactRef.getVersion().getId( ) ) ) { return; } ItemSelector projectSelector = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace( ).getId() ) .withProjectId( artifactRef.getId( ) ) .build(); List<String> releasedVersions = new ArrayList<>( ); Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( ); for ( org.apache.archiva.repository.ManagedRepository repo : repos ) { if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { BaseRepositoryContentLayout repoContent = repo.getContent().getLayout( BaseRepositoryContentLayout.class ); Project proj = repoContent.getProject( projectSelector ); for ( Version version : repoContent.getVersions( proj ) ) { if ( !VersionUtil.isSnapshot( version.getId() ) ) { releasedVersions.add( version.getId() ); } } } } Collections.sort( releasedVersions, VersionComparator.getInstance( ) ); boolean needsMetadataUpdate = false; ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace().getId() ) .withProjectId( artifactRef.getId( ) ) .withArtifactId( artifactRef.getId( ) ); MetadataRepository metadataRepository = repositorySession.getRepository( ); if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion().getId( ) ) ) ) { ArchivaItemSelector selector = versionSelectorBuilder.withVersion( artifactRef.getVersion().getId( ) ).build( ); Version version = layout.getVersion( selector ); if (version.exists()) { repository.deleteItem( version ); } for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId( ), artifactFile.getName() ); } metadataRepository.removeProjectVersion( repositorySession, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId() ); needsMetadataUpdate = true; } if ( needsMetadataUpdate ) { updateMetadata( artifactRef ); } } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( MetadataRepositoryException e ) { log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } catch ( ItemNotFoundException e ) { log.error( "Could not find item to delete {}",e.getMessage( ), e ); } } CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
                                                    RepositoryRegistry repositoryRegistry,
                                                    RepositorySession repositorySession,
                                                    List<RepositoryListener> listeners ); }
CleanupReleasedSnapshotsRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { StorageAsset artifactFile = repository.getRepository( ).getRoot().resolve( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); if ( !artifactFile.exists() ) { return; } Artifact artifactRef = layout.getArtifact( path ); if ( !VersionUtil.isSnapshot( artifactRef.getVersion().getId( ) ) ) { return; } ItemSelector projectSelector = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace( ).getId() ) .withProjectId( artifactRef.getId( ) ) .build(); List<String> releasedVersions = new ArrayList<>( ); Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( ); for ( org.apache.archiva.repository.ManagedRepository repo : repos ) { if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { BaseRepositoryContentLayout repoContent = repo.getContent().getLayout( BaseRepositoryContentLayout.class ); Project proj = repoContent.getProject( projectSelector ); for ( Version version : repoContent.getVersions( proj ) ) { if ( !VersionUtil.isSnapshot( version.getId() ) ) { releasedVersions.add( version.getId() ); } } } } Collections.sort( releasedVersions, VersionComparator.getInstance( ) ); boolean needsMetadataUpdate = false; ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace().getId() ) .withProjectId( artifactRef.getId( ) ) .withArtifactId( artifactRef.getId( ) ); MetadataRepository metadataRepository = repositorySession.getRepository( ); if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion().getId( ) ) ) ) { ArchivaItemSelector selector = versionSelectorBuilder.withVersion( artifactRef.getVersion().getId( ) ).build( ); Version version = layout.getVersion( selector ); if (version.exists()) { repository.deleteItem( version ); } for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId( ), artifactFile.getName() ); } metadataRepository.removeProjectVersion( repositorySession, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId() ); needsMetadataUpdate = true; } if ( needsMetadataUpdate ) { updateMetadata( artifactRef ); } } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( MetadataRepositoryException e ) { log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } catch ( ItemNotFoundException e ) { log.error( "Could not find item to delete {}",e.getMessage( ), e ); } } CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
                                                    RepositoryRegistry repositoryRegistry,
                                                    RepositorySession repositorySession,
                                                    List<RepositoryListener> listeners ); @Override void process( String path ); }
CleanupReleasedSnapshotsRepositoryPurge extends AbstractRepositoryPurge { @Override public void process( String path ) throws RepositoryPurgeException { try { StorageAsset artifactFile = repository.getRepository( ).getRoot().resolve( path ); BaseRepositoryContentLayout layout = repository.getLayout( BaseRepositoryContentLayout.class ); if ( !artifactFile.exists() ) { return; } Artifact artifactRef = layout.getArtifact( path ); if ( !VersionUtil.isSnapshot( artifactRef.getVersion().getId( ) ) ) { return; } ItemSelector projectSelector = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace( ).getId() ) .withProjectId( artifactRef.getId( ) ) .build(); List<String> releasedVersions = new ArrayList<>( ); Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( ); for ( org.apache.archiva.repository.ManagedRepository repo : repos ) { if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE )) { BaseRepositoryContentLayout repoContent = repo.getContent().getLayout( BaseRepositoryContentLayout.class ); Project proj = repoContent.getProject( projectSelector ); for ( Version version : repoContent.getVersions( proj ) ) { if ( !VersionUtil.isSnapshot( version.getId() ) ) { releasedVersions.add( version.getId() ); } } } } Collections.sort( releasedVersions, VersionComparator.getInstance( ) ); boolean needsMetadataUpdate = false; ArchivaItemSelector.Builder versionSelectorBuilder = ArchivaItemSelector.builder( ) .withNamespace( artifactRef.getNamespace().getId() ) .withProjectId( artifactRef.getId( ) ) .withArtifactId( artifactRef.getId( ) ); MetadataRepository metadataRepository = repositorySession.getRepository( ); if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion().getId( ) ) ) ) { ArchivaItemSelector selector = versionSelectorBuilder.withVersion( artifactRef.getVersion().getId( ) ).build( ); Version version = layout.getVersion( selector ); if (version.exists()) { repository.deleteItem( version ); } for ( RepositoryListener listener : listeners ) { listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId( ), artifactFile.getName() ); } metadataRepository.removeProjectVersion( repositorySession, repository.getId( ), artifactRef.getNamespace().getId(), artifactRef.getId( ), artifactRef.getVersion().getId() ); needsMetadataUpdate = true; } if ( needsMetadataUpdate ) { updateMetadata( artifactRef ); } } catch ( LayoutException e ) { log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) ); } catch ( MetadataRepositoryException e ) { log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e ); } catch ( ContentAccessException e ) { e.printStackTrace( ); } catch ( ItemNotFoundException e ) { log.error( "Could not find item to delete {}",e.getMessage( ), e ); } } CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
                                                    RepositoryRegistry repositoryRegistry,
                                                    RepositorySession repositorySession,
                                                    List<RepositoryListener> listeners ); @Override void process( String path );  }