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.maven.archiva.common.utils.VersionComparator;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
25 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
26 import org.apache.maven.archiva.database.ArtifactDAO;
27 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
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;
49 * This will look in a single managed repository, and purge any snapshots that are present
50 * that have a corresponding released version on the same repository.
54 * So, if you have the following (presented in the m2/default layout form) ...
56 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
57 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
58 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
59 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
60 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
61 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
62 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
64 * then the current highest ranked released (non-snapshot) version is 2.0, which means
65 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
66 * be purged. Leaving 2.1-SNAPSHOT in alone.
69 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
72 public class CleanupReleasedSnapshotsRepositoryPurge
73 extends AbstractRepositoryPurge
75 private MetadataTools metadataTools;
77 private ArchivaConfiguration archivaConfig;
79 private RepositoryContentFactory repoContentFactory;
81 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
82 MetadataTools metadataTools, Map<String, RepositoryContentIndex> indices,
83 ArchivaConfiguration archivaConfig, RepositoryContentFactory repoContentFactory )
85 super( repository, artifactDao, indices );
86 this.metadataTools = metadataTools;
87 this.archivaConfig = archivaConfig;
88 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 artifact = repository.toArtifactReference( path );
106 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
108 // Nothing to do here, not a snapshot, skip it.
112 ProjectReference reference = new ProjectReference();
113 reference.setGroupId( artifact.getGroupId() );
114 reference.setArtifactId( artifact.getArtifactId() );
116 // Gather up all of the versions.
117 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
119 List<ManagedRepositoryConfiguration> repos = archivaConfig.getConfiguration().getManagedRepositories();
120 for( ManagedRepositoryConfiguration repo : repos )
122 if( repo.isReleases() && !repo.getId().equals( repository.getId() ) )
126 ManagedRepositoryContent repoContent = repoContentFactory.getManagedRepositoryContent( repo.getId() );
127 allVersions.addAll( repoContent.getVersions( reference ) );
129 catch( RepositoryNotFoundException e )
133 catch( RepositoryException e )
140 // Split the versions into released and snapshots.
141 List<String> releasedVersions = new ArrayList<String>();
142 List<String> snapshotVersions = new ArrayList<String>();
144 for ( String version : allVersions )
146 if ( VersionUtil.isSnapshot( version ) )
148 snapshotVersions.add( version );
152 releasedVersions.add( version );
156 Collections.sort( allVersions, VersionComparator.getInstance() );
157 Collections.sort( releasedVersions, VersionComparator.getInstance() );
158 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
160 // Now clean out any version that is earlier than the highest released version.
161 boolean needsMetadataUpdate = false;
163 VersionedReference versionRef = new VersionedReference();
164 versionRef.setGroupId( artifact.getGroupId() );
165 versionRef.setArtifactId( artifact.getArtifactId() );
167 for ( String version : snapshotVersions )
169 if( releasedVersions.contains( VersionUtil.getReleaseVersion( version ) ) )
171 versionRef.setVersion( version );
172 repository.deleteVersion( versionRef );
173 needsMetadataUpdate = true;
177 if ( needsMetadataUpdate )
179 updateMetadata( artifact );
182 catch ( LayoutException e )
184 throw new RepositoryPurgeException( e.getMessage(), e );
186 catch ( ContentNotFoundException e )
188 throw new RepositoryPurgeException( e.getMessage(), e );
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 )