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.RepositorySession;
27 import org.apache.archiva.repository.events.RepositoryListener;
28 import org.apache.archiva.common.utils.VersionComparator;
29 import org.apache.archiva.common.utils.VersionUtil;
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.RepositoryContentFactory;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryNotFoundException;
38 import org.apache.archiva.repository.layout.LayoutException;
39 import org.apache.archiva.repository.metadata.MetadataTools;
40 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
43 import java.io.IOException;
44 import java.util.ArrayList;
45 import java.util.Collections;
46 import java.util.List;
50 * This will look in a single managed repository, and purge any snapshots that are present
51 * that have a corresponding released version on the same repository.
54 * So, if you have the following (presented in the m2/default layout form) ...
56 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
57 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
58 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
59 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
60 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
61 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
62 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
64 * then the current highest ranked released (non-snapshot) version is 2.0, which means
65 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
66 * be purged. Leaving 2.1-SNAPSHOT in alone.
68 public class CleanupReleasedSnapshotsRepositoryPurge
69 extends AbstractRepositoryPurge
71 private MetadataTools metadataTools;
73 private ManagedRepositoryAdmin managedRepositoryAdmin;
75 private RepositoryContentFactory repoContentFactory;
77 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
78 ManagedRepositoryAdmin managedRepositoryAdmin,
79 RepositoryContentFactory repoContentFactory,
80 RepositorySession repositorySession,
81 List<RepositoryListener> listeners )
83 super( repository, repositorySession, listeners );
84 this.metadataTools = metadataTools;
85 this.managedRepositoryAdmin = managedRepositoryAdmin;
86 this.repoContentFactory = repoContentFactory;
90 public void process( String path )
91 throws RepositoryPurgeException
95 File artifactFile = new File( repository.getRepoRoot(), path );
97 if ( !artifactFile.exists() )
99 // Nothing to do here, file doesn't exist, skip it.
103 ArtifactReference artifactRef = repository.toArtifactReference( path );
105 if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) )
107 // Nothing to do here, not a snapshot, skip it.
111 ProjectReference reference = new ProjectReference();
112 reference.setGroupId( artifactRef.getGroupId() );
113 reference.setArtifactId( artifactRef.getArtifactId() );
115 // Gether the released versions
116 List<String> releasedVersions = new ArrayList<>();
118 List<ManagedRepository> repos = managedRepositoryAdmin.getManagedRepositories();
119 for ( ManagedRepository repo : repos )
121 if ( repo.isReleases() )
125 ManagedRepositoryContent repoContent =
126 repoContentFactory.getManagedRepositoryContent( repo.getId() );
127 for ( String version : repoContent.getVersions( reference ) )
129 if ( !VersionUtil.isSnapshot( version ) )
131 releasedVersions.add( version );
135 catch ( RepositoryNotFoundException e )
139 catch ( RepositoryException e )
146 Collections.sort( releasedVersions, VersionComparator.getInstance() );
148 // Now clean out any version that is earlier than the highest released version.
149 boolean needsMetadataUpdate = false;
151 VersionedReference versionRef = new VersionedReference();
152 versionRef.setGroupId( artifactRef.getGroupId() );
153 versionRef.setArtifactId( artifactRef.getArtifactId() );
155 MetadataRepository metadataRepository = repositorySession.getRepository();
157 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion() ) ) )
159 versionRef.setVersion( artifactRef.getVersion() );
160 repository.deleteVersion( versionRef );
162 // FIXME: looks incomplete, might not delete related metadata?
163 for ( RepositoryListener listener : listeners )
165 listener.deleteArtifact( metadataRepository, repository.getId(), artifactRef.getGroupId(),
166 artifactRef.getArtifactId(), artifactRef.getVersion(),
167 artifactFile.getName() );
170 needsMetadataUpdate = true;
173 if ( needsMetadataUpdate )
175 updateMetadata( artifactRef );
177 } catch ( RepositoryAdminException e )
179 throw new RepositoryPurgeException( e.getMessage(), e );
181 catch ( LayoutException e )
183 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
185 catch ( ContentNotFoundException e )
187 throw new RepositoryPurgeException( e.getMessage(), e );
191 private void updateMetadata( ArtifactReference artifact )
193 VersionedReference versionRef = new VersionedReference();
194 versionRef.setGroupId( artifact.getGroupId() );
195 versionRef.setArtifactId( artifact.getArtifactId() );
196 versionRef.setVersion( artifact.getVersion() );
198 ProjectReference projectRef = new ProjectReference();
199 projectRef.setGroupId( artifact.getGroupId() );
200 projectRef.setArtifactId( artifact.getArtifactId() );
204 metadataTools.updateMetadata( repository, versionRef );
206 catch ( ContentNotFoundException e )
208 // Ignore. (Just means we have no snapshot versions left to reference).
210 catch ( RepositoryMetadataException e )
214 catch ( IOException e )
218 catch ( LayoutException e )
225 metadataTools.updateMetadata( repository, projectRef );
227 catch ( ContentNotFoundException e )
229 // Ignore. (Just means we have no snapshot versions left to reference).
231 catch ( RepositoryMetadataException e )
235 catch ( IOException e )
239 catch ( LayoutException e )