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.database.ArtifactDAO;
25 import org.apache.maven.archiva.indexer.RepositoryContentIndex;
26 import org.apache.maven.archiva.model.ArtifactReference;
27 import org.apache.maven.archiva.model.ProjectReference;
28 import org.apache.maven.archiva.model.VersionedReference;
29 import org.apache.maven.archiva.repository.ContentNotFoundException;
30 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
31 import org.apache.maven.archiva.repository.layout.LayoutException;
32 import org.apache.maven.archiva.repository.metadata.MetadataTools;
33 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.Collections;
39 import java.util.List;
44 * This will look in a single managed repository, and purge any snapshots that are present
45 * that have a corresponding released version on the same repository.
49 * So, if you have the following (presented in the m2/default layout form) ...
51 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
52 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
53 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
54 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
55 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
56 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
57 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
59 * then the current highest ranked released (non-snapshot) version is 2.0, which means
60 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
61 * be purged. Leaving 2.1-SNAPSHOT in alone.
64 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
67 public class CleanupReleasedSnapshotsRepositoryPurge
68 extends AbstractRepositoryPurge
70 private MetadataTools metadataTools;
72 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
73 MetadataTools metadataTools, Map<String, RepositoryContentIndex> indices )
75 super( repository, artifactDao, indices );
76 this.metadataTools = metadataTools;
79 public void process( String path )
80 throws RepositoryPurgeException
84 File artifactFile = new File( repository.getRepoRoot(), path );
86 if ( !artifactFile.exists() )
88 // Nothing to do here, file doesn't exist, skip it.
92 ArtifactReference artifact = repository.toArtifactReference( path );
94 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
96 // Nothing to do here, not a snapshot, skip it.
100 ProjectReference reference = new ProjectReference();
101 reference.setGroupId( artifact.getGroupId() );
102 reference.setArtifactId( artifact.getArtifactId() );
104 // Gather up all of the versions.
105 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
107 // Split the versions into released and snapshots.
108 List<String> releasedVersions = new ArrayList<String>();
109 List<String> snapshotVersions = new ArrayList<String>();
111 for ( String version : allVersions )
113 if ( VersionUtil.isSnapshot( version ) )
115 snapshotVersions.add( version );
119 releasedVersions.add( version );
123 Collections.sort( allVersions, VersionComparator.getInstance() );
124 Collections.sort( releasedVersions, VersionComparator.getInstance() );
125 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
127 // Find out the highest released version.
128 String highestReleasedVersion = allVersions.get( allVersions.size() - 1 );
130 // Now clean out any version that is earlier than the highest released version.
131 boolean needsMetadataUpdate = false;
133 VersionedReference versionRef = new VersionedReference();
134 versionRef.setGroupId( artifact.getGroupId() );
135 versionRef.setArtifactId( artifact.getArtifactId() );
137 for ( String version : snapshotVersions )
139 if ( VersionComparator.getInstance().compare( version, highestReleasedVersion ) < 0 )
141 versionRef.setVersion( version );
142 repository.deleteVersion( versionRef );
143 needsMetadataUpdate = true;
147 if ( needsMetadataUpdate )
149 updateMetadata( artifact );
152 catch ( LayoutException e )
154 throw new RepositoryPurgeException( e.getMessage(), e );
156 catch ( ContentNotFoundException e )
158 throw new RepositoryPurgeException( e.getMessage(), e );
162 private void updateMetadata( ArtifactReference artifact )
164 VersionedReference versionRef = new VersionedReference();
165 versionRef.setGroupId( artifact.getGroupId() );
166 versionRef.setArtifactId( artifact.getArtifactId() );
167 versionRef.setVersion( artifact.getVersion() );
169 ProjectReference projectRef = new ProjectReference();
170 projectRef.setGroupId( artifact.getGroupId() );
171 projectRef.setArtifactId( artifact.getArtifactId() );
175 metadataTools.updateMetadata( repository, versionRef );
177 catch ( ContentNotFoundException e )
179 // Ignore. (Just means we have no snapshot versions left to reference).
181 catch ( RepositoryMetadataException e )
185 catch ( IOException e )
189 catch ( LayoutException e )
196 metadataTools.updateMetadata( repository, projectRef );
198 catch ( ContentNotFoundException e )
200 // Ignore. (Just means we have no snapshot versions left to reference).
202 catch ( RepositoryMetadataException e )
206 catch ( IOException e )
210 catch ( LayoutException e )