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.repository.events.RepositoryListener;
23 import org.apache.maven.archiva.common.utils.VersionComparator;
24 import org.apache.maven.archiva.common.utils.VersionUtil;
25 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
26 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
27 import org.apache.maven.archiva.model.ArchivaArtifact;
28 import org.apache.maven.archiva.model.ArtifactReference;
29 import org.apache.maven.archiva.model.ProjectReference;
30 import org.apache.maven.archiva.model.VersionedReference;
31 import org.apache.maven.archiva.repository.ContentNotFoundException;
32 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
33 import org.apache.maven.archiva.repository.RepositoryContentFactory;
34 import org.apache.maven.archiva.repository.RepositoryException;
35 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
36 import org.apache.maven.archiva.repository.layout.LayoutException;
37 import org.apache.maven.archiva.repository.metadata.MetadataTools;
38 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.List;
48 * This will look in a single managed repository, and purge any snapshots that are present
49 * that have a corresponding released version on the same repository.
53 * So, if you have the following (presented in the m2/default layout form) ...
55 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
56 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
57 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
58 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
59 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
60 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
61 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
63 * then the current highest ranked released (non-snapshot) version is 2.0, which means
64 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
65 * be purged. Leaving 2.1-SNAPSHOT in alone.
70 public class CleanupReleasedSnapshotsRepositoryPurge
71 extends AbstractRepositoryPurge
73 private MetadataTools metadataTools;
75 private ArchivaConfiguration archivaConfig;
77 private RepositoryContentFactory repoContentFactory;
79 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, MetadataTools metadataTools,
80 ArchivaConfiguration archivaConfig,
81 RepositoryContentFactory repoContentFactory,
82 List<RepositoryListener> listeners )
84 super( repository, listeners );
85 this.metadataTools = metadataTools;
86 this.archivaConfig = archivaConfig;
87 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 // Gather up all of the versions.
116 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
118 List<ManagedRepositoryConfiguration> repos = archivaConfig.getConfiguration().getManagedRepositories();
119 for( ManagedRepositoryConfiguration repo : repos )
121 if( repo.isReleases() && !repo.getId().equals( repository.getId() ) )
125 ManagedRepositoryContent repoContent = repoContentFactory.getManagedRepositoryContent( repo.getId() );
126 allVersions.addAll( repoContent.getVersions( reference ) );
128 catch( RepositoryNotFoundException e )
132 catch( RepositoryException e )
139 // Split the versions into released and snapshots.
140 List<String> releasedVersions = new ArrayList<String>();
141 List<String> snapshotVersions = new ArrayList<String>();
143 for ( String version : allVersions )
145 if ( VersionUtil.isSnapshot( version ) )
147 snapshotVersions.add( version );
151 releasedVersions.add( version );
155 Collections.sort( allVersions, VersionComparator.getInstance() );
156 Collections.sort( releasedVersions, VersionComparator.getInstance() );
157 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
159 // Now clean out any version that is earlier than the highest released version.
160 boolean needsMetadataUpdate = false;
162 VersionedReference versionRef = new VersionedReference();
163 versionRef.setGroupId( artifactRef.getGroupId() );
164 versionRef.setArtifactId( artifactRef.getArtifactId() );
166 ArchivaArtifact artifact =
167 new ArchivaArtifact( artifactRef.getGroupId(), artifactRef.getArtifactId(), artifactRef.getVersion(),
168 artifactRef.getClassifier(), artifactRef.getType(), repository.getId() );
170 for ( String version : snapshotVersions )
172 if( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
174 versionRef.setVersion( version );
175 repository.deleteVersion( versionRef );
177 // TODO: looks incomplete, might not delete related metadata?
178 for ( RepositoryListener listener : listeners )
180 listener.deleteArtifact( repository.getId(), artifact.getGroupId(), artifact.getArtifactId(),
181 artifact.getVersion(), artifactFile.getName() );
184 needsMetadataUpdate = true;
188 if ( needsMetadataUpdate )
190 updateMetadata( artifactRef );
193 catch ( LayoutException e )
195 log.debug( "Not processing file that is not an artifact: " + e.getMessage() );
197 catch ( ContentNotFoundException e )
199 throw new RepositoryPurgeException( e.getMessage(), e );
203 private void updateMetadata( ArtifactReference artifact )
205 VersionedReference versionRef = new VersionedReference();
206 versionRef.setGroupId( artifact.getGroupId() );
207 versionRef.setArtifactId( artifact.getArtifactId() );
208 versionRef.setVersion( artifact.getVersion() );
210 ProjectReference projectRef = new ProjectReference();
211 projectRef.setGroupId( artifact.getGroupId() );
212 projectRef.setArtifactId( artifact.getArtifactId() );
216 metadataTools.updateMetadata( repository, versionRef );
218 catch ( ContentNotFoundException e )
220 // Ignore. (Just means we have no snapshot versions left to reference).
222 catch ( RepositoryMetadataException e )
226 catch ( IOException e )
230 catch ( LayoutException e )
237 metadataTools.updateMetadata( repository, projectRef );
239 catch ( ContentNotFoundException e )
241 // Ignore. (Just means we have no snapshot versions left to reference).
243 catch ( RepositoryMetadataException e )
247 catch ( IOException e )
251 catch ( LayoutException e )