1 package org.apache.maven.archiva.reporting;
4 * Copyright 2005-2006 The Apache Software Foundation.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import org.apache.maven.archiva.layer.RepositoryQueryLayer;
20 import org.apache.maven.archiva.layer.RepositoryQueryLayerFactory;
21 import org.apache.maven.artifact.Artifact;
22 import org.apache.maven.artifact.factory.ArtifactFactory;
23 import org.apache.maven.artifact.repository.ArtifactRepository;
24 import org.apache.maven.artifact.repository.metadata.Plugin;
25 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
26 import org.apache.maven.artifact.repository.metadata.Snapshot;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
28 import org.codehaus.plexus.util.FileUtils;
29 import org.codehaus.plexus.util.StringUtils;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
41 * This class will report on bad metadata files. These include invalid version declarations and incomplete version
42 * information inside the metadata file. Plugin metadata will be checked for validity of the latest plugin artifacts.
44 * @plexus.component role="org.apache.maven.archiva.reporting.MetadataReportProcessor" role-hint="bad-metadata"
46 public class BadMetadataReportProcessor
47 implements MetadataReportProcessor
52 private ArtifactFactory artifactFactory;
57 private RepositoryQueryLayerFactory repositoryQueryLayerFactory;
60 * Process the metadata encountered in the repository and report all errors found, if any.
62 * @param metadata the metadata to be processed.
63 * @param repository the repository where the metadata was encountered
64 * @param reporter the ReportingDatabase to receive processing results
66 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
67 ReportingDatabase reporter )
69 if ( metadata.storedInGroupDirectory() )
73 checkPluginMetadata( metadata, repository, reporter );
75 catch ( IOException e )
77 reporter.addWarning( metadata, "Error getting plugin artifact directories versions: " + e );
82 String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
83 if ( lastUpdated == null || lastUpdated.length() == 0 )
85 reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
88 if ( metadata.storedInArtifactVersionDirectory() )
90 checkSnapshotMetadata( metadata, repository, reporter );
94 checkMetadataVersions( metadata, repository, reporter );
98 checkRepositoryVersions( metadata, repository, reporter );
100 catch ( IOException e )
102 reporter.addWarning( metadata, "Error getting plugin artifact directories versions: " + e );
109 * Method for processing a GroupRepositoryMetadata
111 * @param metadata the metadata to be processed.
112 * @param repository the repository where the metadata was encountered
113 * @param reporter the ReportingDatabase to receive processing results
115 private void checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
116 ReportingDatabase reporter )
120 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
121 List pluginDirs = getArtifactIdFiles( metadataDir );
123 Map prefixes = new HashMap();
124 for ( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
126 Plugin plugin = (Plugin) plugins.next();
128 String artifactId = plugin.getArtifactId();
129 if ( artifactId == null || artifactId.length() == 0 )
131 reporter.addFailure( metadata,
132 "Missing or empty artifactId in group metadata for plugin " + plugin.getPrefix() );
135 String prefix = plugin.getPrefix();
136 if ( prefix == null || prefix.length() == 0 )
138 reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId + "." );
142 if ( prefixes.containsKey( prefix ) )
144 reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix + "." );
148 prefixes.put( prefix, plugin );
152 if ( artifactId != null && artifactId.length() > 0 )
154 File pluginDir = new File( metadataDir, artifactId );
155 if ( !pluginDirs.contains( pluginDir ) )
157 reporter.addFailure( metadata, "Metadata plugin " + artifactId + " not found in the repository" );
161 pluginDirs.remove( pluginDir );
166 if ( pluginDirs.size() > 0 )
168 for ( Iterator plugins = pluginDirs.iterator(); plugins.hasNext(); )
170 File plugin = (File) plugins.next();
171 reporter.addFailure( metadata, "Plugin " + plugin.getName() + " is present in the repository but " +
172 "missing in the metadata." );
178 * Method for processing a SnapshotArtifactRepository
180 * @param metadata the metadata to be processed.
181 * @param repository the repository where the metadata was encountered
182 * @param reporter the ReportingDatabase to receive processing results
184 private void checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
185 ReportingDatabase reporter )
187 RepositoryQueryLayer repositoryQueryLayer =
188 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
190 Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
192 String version = StringUtils.replace( metadata.getBaseVersion(), Artifact.SNAPSHOT_VERSION,
193 snapshot.getTimestamp() + "-" + snapshot.getBuildNumber() );
195 artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(), version );
196 artifact.isSnapshot(); // trigger baseVersion correction
198 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
200 reporter.addFailure( metadata, "Snapshot artifact " + version + " does not exist." );
205 * Method for validating the versions declared inside an ArtifactRepositoryMetadata
207 * @param metadata the metadata to be processed.
208 * @param repository the repository where the metadata was encountered
209 * @param reporter the ReportingDatabase to receive processing results
211 private void checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
212 ReportingDatabase reporter )
214 RepositoryQueryLayer repositoryQueryLayer =
215 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
217 Versioning versioning = metadata.getMetadata().getVersioning();
218 for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
220 String version = (String) versions.next();
223 artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(), version );
225 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
227 reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
228 "missing in the repository." );
234 * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
235 * ArtifactRepositoryMetadata
237 * @param metadata the metadata to be processed.
238 * @param repository the repository where the metadata was encountered
239 * @param reporter the ReportingDatabase to receive processing results
241 private void checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
242 ReportingDatabase reporter )
245 Versioning versioning = metadata.getMetadata().getVersioning();
247 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
248 List versions = FileUtils.getFileNames( versionsDir, "*/*.pom", null, false );
249 for ( Iterator i = versions.iterator(); i.hasNext(); )
251 File path = new File( (String) i.next() );
252 String version = path.getParentFile().getName();
253 if ( !versioning.getVersions().contains( version ) )
255 reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
256 "missing in the metadata." );
262 * Used to gather artifactIds from a groupId directory.
264 * @param groupIdDir the directory of the group
265 * @return the list of artifact ID File objects for each directory
266 * @throws IOException if there was a failure to read the directories
268 private List getArtifactIdFiles( File groupIdDir )
271 List artifactIdFiles = new ArrayList();
273 File[] files = groupIdDir.listFiles();
276 for ( Iterator i = Arrays.asList( files ).iterator(); i.hasNext(); )
278 File artifactDir = (File) i.next();
280 if ( artifactDir.isDirectory() )
282 List versions = FileUtils.getFileNames( artifactDir, "*/*.pom", null, false );
283 if ( versions.size() > 0 )
285 artifactIdFiles.add( artifactDir );
291 return artifactIdFiles;