1 package org.apache.archiva.consumers.core.repository;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.admin.model.RepositoryAdminException;
23 import org.apache.archiva.admin.model.beans.ManagedRepository;
24 import org.apache.archiva.admin.model.managed.ManagedRepositoryAdmin;
25 import org.apache.archiva.common.utils.VersionComparator;
26 import org.apache.archiva.common.utils.VersionUtil;
27 import org.apache.archiva.metadata.repository.MetadataRepository;
28 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
29 import org.apache.archiva.metadata.repository.RepositorySession;
30 import org.apache.archiva.model.ArtifactReference;
31 import org.apache.archiva.model.ProjectReference;
32 import org.apache.archiva.model.VersionedReference;
33 import org.apache.archiva.repository.ContentNotFoundException;
34 import org.apache.archiva.repository.ManagedRepositoryContent;
35 import org.apache.archiva.repository.ReleaseScheme;
36 import org.apache.archiva.repository.RepositoryContentFactory;
37 import org.apache.archiva.repository.RepositoryException;
38 import org.apache.archiva.repository.RepositoryNotFoundException;
39 import org.apache.archiva.repository.RepositoryRegistry;
40 import org.apache.archiva.repository.events.RepositoryListener;
41 import org.apache.archiva.repository.layout.LayoutException;
42 import org.apache.archiva.repository.metadata.MetadataTools;
43 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
45 import javax.inject.Inject;
46 import java.io.IOException;
47 import java.nio.file.Files;
48 import java.nio.file.Path;
49 import java.nio.file.Paths;
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.List;
57 * This will look in a single managed repository, and purge any snapshots that are present
58 * that have a corresponding released version on the same repository.
61 * So, if you have the following (presented in the m2/default layout form) ...
63 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
64 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
65 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
66 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
67 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
68 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
69 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
71 * then the current highest ranked released (non-snapshot) version is 2.0, which means
72 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
73 * be purged. Leaving 2.1-SNAPSHOT in alone.
75 public class CleanupReleasedSnapshotsRepositoryPurge
76 extends AbstractRepositoryPurge
78 private MetadataTools metadataTools;
80 private RepositoryRegistry repositoryRegistry;
82 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
83 RepositoryRegistry repositoryRegistry,
84 RepositorySession repositorySession,
85 List<RepositoryListener> listeners )
87 super( repository, repositorySession, listeners );
88 this.metadataTools = metadataTools;
89 this.repositoryRegistry = repositoryRegistry;
93 public void process( String path )
94 throws RepositoryPurgeException
98 Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
100 if ( !Files.exists(artifactFile) )
102 // Nothing to do here, file doesn't exist, skip it.
106 ArtifactReference artifactRef = repository.toArtifactReference( path );
108 if ( !VersionUtil.isSnapshot( artifactRef.getVersion( ) ) )
110 // Nothing to do here, not a snapshot, skip it.
114 ProjectReference reference = new ProjectReference( );
115 reference.setGroupId( artifactRef.getGroupId( ) );
116 reference.setArtifactId( artifactRef.getArtifactId( ) );
118 // Gether the released versions
119 List<String> releasedVersions = new ArrayList<>( );
121 Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( );
122 for ( org.apache.archiva.repository.ManagedRepository repo : repos )
125 if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ))
129 ManagedRepositoryContent repoContent = repo.getContent();
130 for ( String version : repoContent.getVersions( reference ) )
132 if ( !VersionUtil.isSnapshot( version ) )
134 releasedVersions.add( version );
138 catch ( RepositoryException e )
145 Collections.sort( releasedVersions, VersionComparator.getInstance( ) );
147 // Now clean out any version that is earlier than the highest released version.
148 boolean needsMetadataUpdate = false;
150 VersionedReference versionRef = new VersionedReference( );
151 versionRef.setGroupId( artifactRef.getGroupId( ) );
152 versionRef.setArtifactId( artifactRef.getArtifactId( ) );
154 MetadataRepository metadataRepository = repositorySession.getRepository( );
156 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion( ) ) ) )
158 versionRef.setVersion( artifactRef.getVersion( ) );
159 repository.deleteVersion( versionRef );
161 for ( RepositoryListener listener : listeners )
163 listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
164 artifactRef.getArtifactId( ), artifactRef.getVersion( ),
165 artifactFile.getFileName().toString() );
167 metadataRepository.removeProjectVersion( repository.getId( ), artifactRef.getGroupId( ),
168 artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
170 needsMetadataUpdate = true;
173 if ( needsMetadataUpdate )
175 updateMetadata( artifactRef );
178 catch ( LayoutException e )
180 log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
182 catch ( ContentNotFoundException e )
184 throw new RepositoryPurgeException( e.getMessage( ), e );
186 catch ( MetadataRepositoryException e )
188 log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
192 private void updateMetadata( ArtifactReference artifact )
194 VersionedReference versionRef = new VersionedReference( );
195 versionRef.setGroupId( artifact.getGroupId( ) );
196 versionRef.setArtifactId( artifact.getArtifactId( ) );
197 versionRef.setVersion( artifact.getVersion( ) );
199 ProjectReference projectRef = new ProjectReference( );
200 projectRef.setGroupId( artifact.getGroupId( ) );
201 projectRef.setArtifactId( artifact.getArtifactId( ) );
205 metadataTools.updateMetadata( repository, versionRef );
207 catch ( ContentNotFoundException e )
209 // Ignore. (Just means we have no snapshot versions left to reference).
211 catch ( RepositoryMetadataException e )
215 catch ( IOException e )
219 catch ( LayoutException e )
226 metadataTools.updateMetadata( repository, projectRef );
228 catch ( ContentNotFoundException e )
230 // Ignore. (Just means we have no snapshot versions left to reference).
232 catch ( RepositoryMetadataException e )
236 catch ( IOException e )
240 catch ( LayoutException e )