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.common.utils.VersionComparator;
23 import org.apache.archiva.common.utils.VersionUtil;
24 import org.apache.archiva.metadata.repository.MetadataRepository;
25 import org.apache.archiva.metadata.repository.MetadataRepositoryException;
26 import org.apache.archiva.metadata.repository.RepositorySession;
27 import org.apache.archiva.model.ArtifactReference;
28 import org.apache.archiva.model.ProjectReference;
29 import org.apache.archiva.model.VersionedReference;
30 import org.apache.archiva.repository.ContentNotFoundException;
31 import org.apache.archiva.repository.LayoutException;
32 import org.apache.archiva.repository.ManagedRepositoryContent;
33 import org.apache.archiva.repository.ReleaseScheme;
34 import org.apache.archiva.repository.RepositoryException;
35 import org.apache.archiva.repository.RepositoryRegistry;
36 import org.apache.archiva.repository.events.RepositoryListener;
37 import org.apache.archiva.repository.metadata.MetadataTools;
38 import org.apache.archiva.repository.metadata.RepositoryMetadataException;
40 import java.io.IOException;
41 import java.nio.file.Files;
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.ArrayList;
45 import java.util.Collection;
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 RepositoryRegistry repositoryRegistry;
76 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
77 RepositoryRegistry repositoryRegistry,
78 RepositorySession repositorySession,
79 List<RepositoryListener> listeners )
81 super( repository, repositorySession, listeners );
82 this.metadataTools = metadataTools;
83 this.repositoryRegistry = repositoryRegistry;
87 public void process( String path )
88 throws RepositoryPurgeException
92 Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
94 if ( !Files.exists(artifactFile) )
96 // Nothing to do here, file doesn't exist, skip it.
100 ArtifactReference artifactRef = repository.toArtifactReference( path );
102 if ( !VersionUtil.isSnapshot( artifactRef.getVersion( ) ) )
104 // Nothing to do here, not a snapshot, skip it.
108 ProjectReference reference = new ProjectReference( );
109 reference.setGroupId( artifactRef.getGroupId( ) );
110 reference.setArtifactId( artifactRef.getArtifactId( ) );
112 // Gether the released versions
113 List<String> releasedVersions = new ArrayList<>( );
115 Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( );
116 for ( org.apache.archiva.repository.ManagedRepository repo : repos )
119 if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ))
123 ManagedRepositoryContent repoContent = repo.getContent();
124 for ( String version : repoContent.getVersions( reference ) )
126 if ( !VersionUtil.isSnapshot( version ) )
128 releasedVersions.add( version );
132 catch ( RepositoryException e )
139 Collections.sort( releasedVersions, VersionComparator.getInstance( ) );
141 // Now clean out any version that is earlier than the highest released version.
142 boolean needsMetadataUpdate = false;
144 VersionedReference versionRef = new VersionedReference( );
145 versionRef.setGroupId( artifactRef.getGroupId( ) );
146 versionRef.setArtifactId( artifactRef.getArtifactId( ) );
148 MetadataRepository metadataRepository = repositorySession.getRepository( );
150 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion( ) ) ) )
152 versionRef.setVersion( artifactRef.getVersion( ) );
153 repository.deleteVersion( versionRef );
155 for ( RepositoryListener listener : listeners )
157 listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
158 artifactRef.getArtifactId( ), artifactRef.getVersion( ),
159 artifactFile.getFileName().toString() );
161 metadataRepository.removeProjectVersion( repositorySession, repository.getId( ),
162 artifactRef.getGroupId( ), artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
164 needsMetadataUpdate = true;
167 if ( needsMetadataUpdate )
169 updateMetadata( artifactRef );
172 catch ( LayoutException e )
174 log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
176 catch ( ContentNotFoundException e )
178 throw new RepositoryPurgeException( e.getMessage( ), e );
180 catch ( MetadataRepositoryException e )
182 log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
188 * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
189 * -> not sure what needs to be changed here.
191 @SuppressWarnings( "deprecation" )
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 )