You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CleanupReleasedSnapshotsRepositoryPurge.java 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package org.apache.archiva.consumers.core.repository;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.VersionComparator;
  21. import org.apache.archiva.common.utils.VersionUtil;
  22. import org.apache.archiva.metadata.repository.MetadataRepository;
  23. import org.apache.archiva.metadata.repository.MetadataRepositoryException;
  24. import org.apache.archiva.metadata.repository.RepositorySession;
  25. import org.apache.archiva.model.ArtifactReference;
  26. import org.apache.archiva.model.ProjectReference;
  27. import org.apache.archiva.model.VersionedReference;
  28. import org.apache.archiva.repository.ContentNotFoundException;
  29. import org.apache.archiva.repository.LayoutException;
  30. import org.apache.archiva.repository.ManagedRepositoryContent;
  31. import org.apache.archiva.repository.ReleaseScheme;
  32. import org.apache.archiva.repository.RepositoryException;
  33. import org.apache.archiva.repository.RepositoryRegistry;
  34. import org.apache.archiva.metadata.audit.RepositoryListener;
  35. import org.apache.archiva.repository.content.ItemSelector;
  36. import org.apache.archiva.repository.content.Project;
  37. import org.apache.archiva.repository.content.Version;
  38. import org.apache.archiva.repository.content.base.ArchivaItemSelector;
  39. import org.apache.archiva.repository.metadata.base.MetadataTools;
  40. import org.apache.archiva.repository.metadata.RepositoryMetadataException;
  41. import java.io.IOException;
  42. import java.nio.file.Files;
  43. import java.nio.file.Path;
  44. import java.nio.file.Paths;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.List;
  49. /**
  50. * <p>
  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.
  53. * </p>
  54. * <p>
  55. * So, if you have the following (presented in the m2/default layout form) ...
  56. * <pre>
  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
  64. * </pre>
  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.
  68. */
  69. public class CleanupReleasedSnapshotsRepositoryPurge
  70. extends AbstractRepositoryPurge
  71. {
  72. private MetadataTools metadataTools;
  73. private RepositoryRegistry repositoryRegistry;
  74. public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
  75. RepositoryRegistry repositoryRegistry,
  76. RepositorySession repositorySession,
  77. List<RepositoryListener> listeners )
  78. {
  79. super( repository, repositorySession, listeners );
  80. this.metadataTools = metadataTools;
  81. this.repositoryRegistry = repositoryRegistry;
  82. }
  83. @Override
  84. public void process( String path )
  85. throws RepositoryPurgeException
  86. {
  87. try
  88. {
  89. Path artifactFile = Paths.get( repository.getRepoRoot( ), path );
  90. if ( !Files.exists(artifactFile) )
  91. {
  92. // Nothing to do here, file doesn't exist, skip it.
  93. return;
  94. }
  95. ArtifactReference artifactRef = repository.toArtifactReference( path );
  96. if ( !VersionUtil.isSnapshot( artifactRef.getVersion( ) ) )
  97. {
  98. // Nothing to do here, not a snapshot, skip it.
  99. return;
  100. }
  101. ItemSelector selector = ArchivaItemSelector.builder( )
  102. .withNamespace( artifactRef.getGroupId( ) )
  103. .withProjectId( artifactRef.getArtifactId( ) )
  104. .build();
  105. // Gether the released versions
  106. List<String> releasedVersions = new ArrayList<>( );
  107. Collection<org.apache.archiva.repository.ManagedRepository> repos = repositoryRegistry.getManagedRepositories( );
  108. for ( org.apache.archiva.repository.ManagedRepository repo : repos )
  109. {
  110. if ( repo.getActiveReleaseSchemes().contains( ReleaseScheme.RELEASE ))
  111. {
  112. ManagedRepositoryContent repoContent = repo.getContent();
  113. Project proj = repoContent.getProject( selector );
  114. for ( Version version : repoContent.getVersions( proj ) )
  115. {
  116. if ( !VersionUtil.isSnapshot( version.getVersion() ) )
  117. {
  118. releasedVersions.add( version.getVersion() );
  119. }
  120. }
  121. }
  122. }
  123. Collections.sort( releasedVersions, VersionComparator.getInstance( ) );
  124. // Now clean out any version that is earlier than the highest released version.
  125. boolean needsMetadataUpdate = false;
  126. VersionedReference versionRef = new VersionedReference( );
  127. versionRef.setGroupId( artifactRef.getGroupId( ) );
  128. versionRef.setArtifactId( artifactRef.getArtifactId( ) );
  129. MetadataRepository metadataRepository = repositorySession.getRepository( );
  130. if ( releasedVersions.contains( VersionUtil.getReleaseVersion( artifactRef.getVersion( ) ) ) )
  131. {
  132. versionRef.setVersion( artifactRef.getVersion( ) );
  133. repository.deleteVersion( versionRef );
  134. for ( RepositoryListener listener : listeners )
  135. {
  136. listener.deleteArtifact( metadataRepository, repository.getId( ), artifactRef.getGroupId( ),
  137. artifactRef.getArtifactId( ), artifactRef.getVersion( ),
  138. artifactFile.getFileName().toString() );
  139. }
  140. metadataRepository.removeProjectVersion( repositorySession, repository.getId( ),
  141. artifactRef.getGroupId( ), artifactRef.getArtifactId( ), artifactRef.getVersion( ) );
  142. needsMetadataUpdate = true;
  143. }
  144. if ( needsMetadataUpdate )
  145. {
  146. updateMetadata( artifactRef );
  147. }
  148. }
  149. catch ( LayoutException e )
  150. {
  151. log.debug( "Not processing file that is not an artifact: {}", e.getMessage( ) );
  152. }
  153. catch ( ContentNotFoundException e )
  154. {
  155. throw new RepositoryPurgeException( e.getMessage( ), e );
  156. }
  157. catch ( MetadataRepositoryException e )
  158. {
  159. log.error( "Could not remove metadata during cleanup of released snapshots of {}", path, e );
  160. }
  161. catch ( org.apache.archiva.repository.ContentAccessException e )
  162. {
  163. e.printStackTrace( );
  164. }
  165. }
  166. /*
  167. * TODO: Uses a deprecated API, but if we use the API with location string, it does not work as expected
  168. * -> not sure what needs to be changed here.
  169. */
  170. @SuppressWarnings( "deprecation" )
  171. private void updateMetadata( ArtifactReference artifact )
  172. {
  173. VersionedReference versionRef = new VersionedReference( );
  174. versionRef.setGroupId( artifact.getGroupId( ) );
  175. versionRef.setArtifactId( artifact.getArtifactId( ) );
  176. versionRef.setVersion( artifact.getVersion( ) );
  177. ProjectReference projectRef = new ProjectReference( );
  178. projectRef.setGroupId( artifact.getGroupId( ) );
  179. projectRef.setArtifactId( artifact.getArtifactId( ) );
  180. try
  181. {
  182. metadataTools.updateMetadata( repository, versionRef );
  183. }
  184. catch ( ContentNotFoundException e )
  185. {
  186. // Ignore. (Just means we have no snapshot versions left to reference).
  187. }
  188. catch ( RepositoryMetadataException e )
  189. {
  190. // Ignore.
  191. }
  192. catch ( IOException e )
  193. {
  194. // Ignore.
  195. }
  196. catch ( LayoutException e )
  197. {
  198. // Ignore.
  199. }
  200. try
  201. {
  202. metadataTools.updateMetadata( repository, projectRef );
  203. }
  204. catch ( ContentNotFoundException e )
  205. {
  206. // Ignore. (Just means we have no snapshot versions left to reference).
  207. }
  208. catch ( RepositoryMetadataException e )
  209. {
  210. // Ignore.
  211. }
  212. catch ( IOException e )
  213. {
  214. // Ignore.
  215. }
  216. catch ( LayoutException e )
  217. {
  218. // Ignore.
  219. }
  220. }
  221. }