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.RepositoryContentFactory;
36 import org.apache.archiva.repository.RepositoryException;
37 import org.apache.archiva.repository.RepositoryNotFoundException;
38 import org.apache.archiva.repository.events.RepositoryListener;
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 );
180 catch ( RepositoryAdminException e )
182 throw new RepositoryPurgeException( e.getMessage( ), e );
184 catch ( LayoutException e )
186 log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
188 catch ( ContentNotFoundException e )
190 throw new RepositoryPurgeException( e.getMessage( ), e );
192 catch ( MetadataRepositoryException e )
194 log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
198 private void updateMetadata( ArtifactReference artifact )
200 VersionedReference versionRef = new VersionedReference( );
201 versionRef.setGroupId( artifact.getGroupId( ) );
202 versionRef.setArtifactId( artifact.getArtifactId( ) );
203 versionRef.setVersion( artifact.getVersion( ) );
205 ProjectReference projectRef = new ProjectReference( );
206 projectRef.setGroupId( artifact.getGroupId( ) );
207 projectRef.setArtifactId( artifact.getArtifactId( ) );
211 metadataTools.updateMetadata( repository, versionRef );
213 catch ( ContentNotFoundException e )
215 // Ignore. (Just means we have no snapshot versions left to reference).
217 catch ( RepositoryMetadataException e )
221 catch ( IOException e )
225 catch ( LayoutException e )
232 metadataTools.updateMetadata( repository, projectRef );
234 catch ( ContentNotFoundException e )
236 // Ignore. (Just means we have no snapshot versions left to reference).
238 catch ( RepositoryMetadataException e )
242 catch ( IOException e )
246 catch ( LayoutException e )