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.metadata.repository.MetadataRepository;
26 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
27 import org.apache.archiva.metadata.repository.RepositorySession;
28 import org.apache.archiva.repository.events.RepositoryListener;
29 import org.apache.archiva.common.utils.VersionComparator;
30 import org.apache.archiva.common.utils.VersionUtil;
31 import org.apache.archiva.model.ArtifactReference;
32 import org.apache.archiva.model.ProjectReference;
33 import org.apache.archiva.model.VersionedReference;
34 import org.apache.archiva.repository.ContentNotFoundException;
35 import org.apache.archiva.repository.ManagedRepositoryContent;
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.layout.LayoutException;
40 import org.apache.archiva.repository.metadata.MetadataTools;
41 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
44 import java.io.IOException;
45 import java.util.ArrayList;
46 import java.util.Collections;
47 import java.util.List;
51 * This will look in a single managed repository, and purge any snapshots that are present
52 * that have a corresponding released version on the same repository.
55 * So, if you have the following (presented in the m2/default layout form) ...
57 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
58 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
59 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
60 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
61 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
62 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
63 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
65 * then the current highest ranked released (non-snapshot) version is 2.0, which means
66 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
67 * be purged. Leaving 2.1-SNAPSHOT in alone.
69 public class CleanupReleasedSnapshotsRepositoryPurge
70 extends AbstractRepositoryPurge
72 private MetadataTools metadataTools;
74 private ManagedRepositoryAdmin managedRepositoryAdmin;
76 private RepositoryContentFactory repoContentFactory;
78 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
79 ManagedRepositoryAdmin managedRepositoryAdmin,
80 RepositoryContentFactory repoContentFactory,
81 RepositorySession repositorySession,
82 List<RepositoryListener> listeners )
84 super( repository, repositorySession, listeners );
85 this.metadataTools = metadataTools;
86 this.managedRepositoryAdmin = managedRepositoryAdmin;
87 this.repoContentFactory = repoContentFactory;
91 public void process( String path )
92 throws RepositoryPurgeException
96 File artifactFile = new File( repository.getRepoRoot(), path );
98 if ( !artifactFile.exists() )
100 // Nothing to do here, file doesn't exist, skip it.
104 ArtifactReference artifactRef = repository.toArtifactReference( path );
106 if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) )
108 // Nothing to do here, not a snapshot, skip it.
112 ProjectReference reference = new ProjectReference();
113 reference.setGroupId( artifactRef.getGroupId() );
114 reference.setArtifactId( artifactRef.getArtifactId() );
116 // Gether the released versions
117 List<String> releasedVersions = new ArrayList<>();
119 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
120 for ( ManagedRepository repo : repos )
122 if ( repo.isReleases() )
126 ManagedRepositoryContent repoContent =
127 repoContentFactory.getManagedRepositoryContent( repo.getId() );
128 for ( String version : repoContent.getVersions( reference ) )
130 if ( !VersionUtil.isSnapshot( version ) )
132 releasedVersions.add( version );
136 catch ( RepositoryNotFoundException e )
140 catch ( RepositoryException e )
147 Collections.sort( releasedVersions, VersionComparator.getInstance() );
149 // Now clean out any version that is earlier than the highest released version.
150 boolean needsMetadataUpdate = false;
152 VersionedReference versionRef = new VersionedReference();
153 versionRef.setGroupId( artifactRef.getGroupId() );
154 versionRef.setArtifactId( artifactRef.getArtifactId() );
156 MetadataRepository metadataRepository = repositorySession.getRepository();
158 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion() ) ) )
160 versionRef.setVersion( artifactRef.getVersion() );
161 repository.deleteVersion( versionRef );
163 for ( RepositoryListener listener : listeners )
165 listener.deleteArtifact( metadataRepository, repository.getId(), artifactRef.getGroupId(),
166 artifactRef.getArtifactId(), artifactRef.getVersion(),
167 artifactFile.getName() );
169 metadataRepository.removeProjectVersion( repository.getId(), artifactRef.getGroupId(),
170 artifactRef.getArtifactId(), artifactRef.getVersion());
172 needsMetadataUpdate = true;
175 if ( needsMetadataUpdate )
177 updateMetadata( artifactRef );
179 } catch ( RepositoryAdminException e )
181 throw new RepositoryPurgeException( e.getMessage(), e );
183 catch ( LayoutException e )
185 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
187 catch ( ContentNotFoundException e )
189 throw new RepositoryPurgeException( e.getMessage(), e );
191 catch ( MetadataRepositoryException e )
193 log.error("Could not remove metadata during cleanup of released snapshots of {}", path, e);
197 private void updateMetadata( ArtifactReference artifact )
199 VersionedReference versionRef = new VersionedReference();
200 versionRef.setGroupId( artifact.getGroupId() );
201 versionRef.setArtifactId( artifact.getArtifactId() );
202 versionRef.setVersion( artifact.getVersion() );
204 ProjectReference projectRef = new ProjectReference();
205 projectRef.setGroupId( artifact.getGroupId() );
206 projectRef.setArtifactId( artifact.getArtifactId() );
210 metadataTools.updateMetadata( repository, versionRef );
212 catch ( ContentNotFoundException e )
214 // Ignore. (Just means we have no snapshot versions left to reference).
216 catch ( RepositoryMetadataException e )
220 catch ( IOException e )
224 catch ( LayoutException e )
231 metadataTools.updateMetadata( repository, projectRef );
233 catch ( ContentNotFoundException e )
235 // Ignore. (Just means we have no snapshot versions left to reference).
237 catch ( RepositoryMetadataException e )
241 catch ( IOException e )
245 catch ( LayoutException e )