1 package org.apache.maven.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.metadata.repository.MetadataRepository;
23 import org.apache.archiva.metadata.repository.RepositorySession;
24 import org.apache.archiva.repository.events.RepositoryListener;
25 import org.apache.maven.archiva.common.utils.VersionComparator;
26 import org.apache.maven.archiva.common.utils.VersionUtil;
27 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
28 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
29 import org.apache.maven.archiva.model.ArchivaArtifact;
30 import org.apache.maven.archiva.model.ArtifactReference;
31 import org.apache.maven.archiva.model.ProjectReference;
32 import org.apache.maven.archiva.model.VersionedReference;
33 import org.apache.maven.archiva.repository.ContentNotFoundException;
34 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
35 import org.apache.maven.archiva.repository.RepositoryContentFactory;
36 import org.apache.maven.archiva.repository.RepositoryException;
37 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
38 import org.apache.maven.archiva.repository.layout.LayoutException;
39 import org.apache.maven.archiva.repository.metadata.MetadataTools;
40 import org.apache.maven.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.
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.
72 public class CleanupReleasedSnapshotsRepositoryPurge
73 extends AbstractRepositoryPurge
75 private MetadataTools metadataTools;
77 private ArchivaConfiguration archivaConfig;
79 private RepositoryContentFactory repoContentFactory;
81 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
82 ArchivaConfiguration archivaConfig,
83 RepositoryContentFactory repoContentFactory,
84 RepositorySession repositorySession,
85 List<RepositoryListener> listeners )
87 super( repository, repositorySession, listeners );
88 this.metadataTools = metadataTools;
89 this.archivaConfig = archivaConfig;
90 this.repoContentFactory = repoContentFactory;
93 public void process( String path )
94 throws RepositoryPurgeException
98 File artifactFile = new File( repository.getRepoRoot(), path );
100 if ( !artifactFile.exists() )
102 // Nothing to do here, file doesn't exist, skip it.
106 ArtifactReference artifactRef = repository.toArtifactReference( path );
108 if ( !VersionUtil.isSnapshot( artifactRef.getVersion() ) )
110 // Nothing to do here, not a snapshot, skip it.
114 ProjectReference reference = new ProjectReference();
115 reference.setGroupId( artifactRef.getGroupId() );
116 reference.setArtifactId( artifactRef.getArtifactId() );
118 // Gather up all of the versions.
119 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
121 List<ManagedRepositoryConfiguration> repos = archivaConfig.getConfiguration().getManagedRepositories();
122 for ( ManagedRepositoryConfiguration repo : repos )
124 if ( repo.isReleases() && !repo.getId().equals( repository.getId() ) )
128 ManagedRepositoryContent repoContent = repoContentFactory.getManagedRepositoryContent(
130 allVersions.addAll( repoContent.getVersions( reference ) );
132 catch ( RepositoryNotFoundException e )
136 catch ( RepositoryException e )
143 // Split the versions into released and snapshots.
144 List<String> releasedVersions = new ArrayList<String>();
145 List<String> snapshotVersions = new ArrayList<String>();
147 for ( String version : allVersions )
149 if ( VersionUtil.isSnapshot( version ) )
151 snapshotVersions.add( version );
155 releasedVersions.add( version );
159 Collections.sort( allVersions, VersionComparator.getInstance() );
160 Collections.sort( releasedVersions, VersionComparator.getInstance() );
161 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
163 // Now clean out any version that is earlier than the highest released version.
164 boolean needsMetadataUpdate = false;
166 VersionedReference versionRef = new VersionedReference();
167 versionRef.setGroupId( artifactRef.getGroupId() );
168 versionRef.setArtifactId( artifactRef.getArtifactId() );
170 ArchivaArtifact artifact = new ArchivaArtifact( artifactRef.getGroupId(), artifactRef.getArtifactId(),
171 artifactRef.getVersion(), artifactRef.getClassifier(),
172 artifactRef.getType(), repository.getId() );
174 MetadataRepository metadataRepository = repositorySession.getRepository();
175 for ( String version : snapshotVersions )
177 if ( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
179 versionRef.setVersion( version );
180 repository.deleteVersion( versionRef );
182 // FIXME: looks incomplete, might not delete related metadata?
183 for ( RepositoryListener listener : listeners )
185 listener.deleteArtifact( metadataRepository, repository.getId(), artifact.getGroupId(),
186 artifact.getArtifactId(), artifact.getVersion(),
187 artifactFile.getName() );
190 needsMetadataUpdate = true;
194 if ( needsMetadataUpdate )
196 updateMetadata( artifactRef );
199 catch ( LayoutException e )
201 log.debug( "Not processing file that is not an artifact: {}", e.getMessage() );
203 catch ( ContentNotFoundException e )
205 throw new RepositoryPurgeException( e.getMessage(), e );
209 private void updateMetadata( ArtifactReference artifact )
211 VersionedReference versionRef = new VersionedReference();
212 versionRef.setGroupId( artifact.getGroupId() );
213 versionRef.setArtifactId( artifact.getArtifactId() );
214 versionRef.setVersion( artifact.getVersion() );
216 ProjectReference projectRef = new ProjectReference();
217 projectRef.setGroupId( artifact.getGroupId() );
218 projectRef.setArtifactId( artifact.getArtifactId() );
222 metadataTools.updateMetadata( repository, versionRef );
224 catch ( ContentNotFoundException e )
226 // Ignore. (Just means we have no snapshot versions left to reference).
228 catch ( RepositoryMetadataException e )
232 catch ( IOException e )
236 catch ( LayoutException e )
243 metadataTools.updateMetadata( repository, projectRef );
245 catch ( ContentNotFoundException e )
247 // Ignore. (Just means we have no snapshot versions left to reference).
249 catch ( RepositoryMetadataException e )
253 catch ( IOException e )
257 catch ( LayoutException e )