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.model.ArtifactReference;
26 import org.apache.maven.archiva.model.ProjectReference;
27 import org.apache.maven.archiva.model.VersionedReference;
28 import org.apache.maven.archiva.repository.ContentNotFoundException;
29 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
30 import org.apache.maven.archiva.repository.layout.LayoutException;
31 import org.apache.maven.archiva.repository.metadata.MetadataTools;
32 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.Collections;
38 import java.util.List;
42 * This will look in a single managed repository, and purge any snapshots that are present
43 * that have a corresponding released version on the same repository.
47 * So, if you have the following (presented in the m2/default layout form) ...
49 * /com/foo/foo-tool/1.0-SNAPSHOT/foo-tool-1.0-SNAPSHOT.jar
50 * /com/foo/foo-tool/1.1-SNAPSHOT/foo-tool-1.1-SNAPSHOT.jar
51 * /com/foo/foo-tool/1.2.1-SNAPSHOT/foo-tool-1.2.1-SNAPSHOT.jar
52 * /com/foo/foo-tool/1.2.1/foo-tool-1.2.1.jar
53 * /com/foo/foo-tool/2.0-SNAPSHOT/foo-tool-2.0-SNAPSHOT.jar
54 * /com/foo/foo-tool/2.0/foo-tool-2.0.jar
55 * /com/foo/foo-tool/2.1-SNAPSHOT/foo-tool-2.1-SNAPSHOT.jar
57 * then the current highest ranked released (non-snapshot) version is 2.0, which means
58 * the snapshots from 1.0-SNAPSHOT, 1.1-SNAPSHOT, 1.2.1-SNAPSHOT, and 2.0-SNAPSHOT can
59 * be purged. Leaving 2.1-SNAPSHOT in alone.
62 * @author <a href="mailto:oching@apache.org">Maria Odea Ching</a>
65 public class CleanupReleasedSnapshotsRepositoryPurge
66 extends AbstractRepositoryPurge
68 private MetadataTools metadataTools;
70 public CleanupReleasedSnapshotsRepositoryPurge( ManagedRepositoryContent repository, ArtifactDAO artifactDao,
71 MetadataTools metadataTools )
73 super( repository, artifactDao );
74 this.metadataTools = metadataTools;
77 public void process( String path )
78 throws RepositoryPurgeException
82 File artifactFile = new File( repository.getRepoRoot(), path );
84 if ( !artifactFile.exists() )
86 // Nothing to do here, file doesn't exist, skip it.
90 ArtifactReference artifact = repository.toArtifactReference( path );
92 if ( !VersionUtil.isSnapshot( artifact.getVersion() ) )
94 // Nothing to do here, not a snapshot, skip it.
98 ProjectReference reference = new ProjectReference();
99 reference.setGroupId( artifact.getGroupId() );
100 reference.setArtifactId( artifact.getArtifactId() );
102 // Gather up all of the versions.
103 List<String> allVersions = new ArrayList<String>( repository.getVersions( reference ) );
105 // Split the versions into released and snapshots.
106 List<String> releasedVersions = new ArrayList<String>();
107 List<String> snapshotVersions = new ArrayList<String>();
109 for ( String version : allVersions )
111 if ( VersionUtil.isSnapshot( version ) )
113 snapshotVersions.add( version );
117 releasedVersions.add( version );
121 Collections.sort( allVersions, VersionComparator.getInstance() );
122 Collections.sort( releasedVersions, VersionComparator.getInstance() );
123 Collections.sort( snapshotVersions, VersionComparator.getInstance() );
125 // Find out the highest released version.
126 String highestReleasedVersion = allVersions.get( allVersions.size() - 1 );
128 // Now clean out any version that is earlier than the highest released version.
129 boolean needsMetadataUpdate = false;
131 VersionedReference versionRef = new VersionedReference();
132 versionRef.setGroupId( artifact.getGroupId() );
133 versionRef.setArtifactId( artifact.getArtifactId() );
135 for ( String version : snapshotVersions )
137 if ( VersionComparator.getInstance().compare( version, highestReleasedVersion ) < 0 )
139 versionRef.setVersion( version );
140 repository.deleteVersion( versionRef );
141 needsMetadataUpdate = true;
145 if ( needsMetadataUpdate )
147 updateMetadata( artifact );
150 catch ( LayoutException e )
152 throw new RepositoryPurgeException( e.getMessage(), e );
154 catch ( ContentNotFoundException e )
156 throw new RepositoryPurgeException( e.getMessage(), e );
160 private void updateMetadata( ArtifactReference artifact )
162 VersionedReference versionRef = new VersionedReference();
163 versionRef.setGroupId( artifact.getGroupId() );
164 versionRef.setArtifactId( artifact.getArtifactId() );
165 versionRef.setVersion( artifact.getVersion() );
167 ProjectReference projectRef = new ProjectReference();
168 projectRef.setGroupId( artifact.getGroupId() );
169 projectRef.setArtifactId( artifact.getArtifactId() );
173 metadataTools.updateMetadata( repository, versionRef );
175 catch ( ContentNotFoundException e )
177 // Ignore. (Just means we have no snapshot versions left to reference).
179 catch ( RepositoryMetadataException e )
183 catch ( IOException e )
187 catch ( LayoutException e )
194 metadataTools.updateMetadata( repository, projectRef );
196 catch ( ContentNotFoundException e )
198 // Ignore. (Just means we have no snapshot versions left to reference).
200 catch ( RepositoryMetadataException e )
204 catch ( IOException e )
208 catch ( LayoutException e )