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 ArtifactReporter to receive processing results
65 * @throws ReportProcessorException if an error was occurred while processing the metadata
67 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
68 throws ReportProcessorException
70 boolean hasFailures = false;
72 if ( metadata.storedInGroupDirectory() )
76 hasFailures = checkPluginMetadata( metadata, repository, reporter );
78 catch ( IOException e )
80 throw new ReportProcessorException( "Error getting plugin artifact directories versions", e );
85 String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
86 if ( lastUpdated == null || lastUpdated.length() == 0 )
88 reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
92 if ( metadata.storedInArtifactVersionDirectory() )
94 hasFailures |= checkSnapshotMetadata( metadata, repository, reporter );
98 if ( !checkMetadataVersions( metadata, repository, reporter ) )
105 if ( checkRepositoryVersions( metadata, repository, reporter ) )
110 catch ( IOException e )
112 throw new ReportProcessorException( "Error getting versions", e );
119 reporter.addSuccess( metadata );
124 * Method for processing a GroupRepositoryMetadata
126 * @param metadata the metadata to be processed.
127 * @param repository the repository where the metadata was encountered
128 * @param reporter the ArtifactReporter to receive processing results
130 private boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
131 ArtifactReporter reporter )
134 boolean hasFailures = false;
137 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
138 List pluginDirs = getArtifactIdFiles( metadataDir );
140 Map prefixes = new HashMap();
141 for ( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
143 Plugin plugin = (Plugin) plugins.next();
145 String artifactId = plugin.getArtifactId();
146 if ( artifactId == null || artifactId.length() == 0 )
148 reporter.addFailure( metadata, "Missing or empty artifactId in group metadata." );
152 String prefix = plugin.getPrefix();
153 if ( prefix == null || prefix.length() == 0 )
155 reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId + "." );
160 if ( prefixes.containsKey( prefix ) )
162 reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix + "." );
167 prefixes.put( prefix, plugin );
171 if ( artifactId != null && artifactId.length() > 0 )
173 File pluginDir = new File( metadataDir, artifactId );
174 if ( !pluginDirs.contains( pluginDir ) )
176 reporter.addFailure( metadata, "Metadata plugin " + artifactId + " not found in the repository" );
181 pluginDirs.remove( pluginDir );
186 if ( pluginDirs.size() > 0 )
188 for ( Iterator plugins = pluginDirs.iterator(); plugins.hasNext(); )
190 File plugin = (File) plugins.next();
191 reporter.addFailure( metadata, "Plugin " + plugin.getName() + " is present in the repository but " +
192 "missing in the metadata." );
201 * Method for processing a SnapshotArtifactRepository
203 * @param metadata the metadata to be processed.
204 * @param repository the repository where the metadata was encountered
205 * @param reporter the ArtifactReporter to receive processing results
207 private boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
208 ArtifactReporter reporter )
210 RepositoryQueryLayer repositoryQueryLayer =
211 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
213 boolean hasFailures = false;
215 Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
217 String version = StringUtils.replace( metadata.getBaseVersion(), Artifact.SNAPSHOT_VERSION,
218 snapshot.getTimestamp() + "-" + snapshot.getBuildNumber() );
220 artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(), version );
221 artifact.isSnapshot(); // trigger baseVersion correction
223 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
225 reporter.addFailure( metadata, "Snapshot artifact " + version + " does not exist." );
233 * Method for validating the versions declared inside an ArtifactRepositoryMetadata
235 * @param metadata the metadata to be processed.
236 * @param repository the repository where the metadata was encountered
237 * @param reporter the ArtifactReporter to receive processing results
239 private boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
240 ArtifactReporter reporter )
242 RepositoryQueryLayer repositoryQueryLayer =
243 repositoryQueryLayerFactory.createRepositoryQueryLayer( repository );
245 boolean hasFailures = false;
246 Versioning versioning = metadata.getMetadata().getVersioning();
247 for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
249 String version = (String) versions.next();
252 artifactFactory.createProjectArtifact( metadata.getGroupId(), metadata.getArtifactId(), version );
254 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
256 reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
257 "missing in the repository." );
265 * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
266 * ArtifactRepositoryMetadata
268 * @param metadata the metadata to be processed.
269 * @param repository the repository where the metadata was encountered
270 * @param reporter the ArtifactReporter to receive processing results
272 private boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
273 ArtifactReporter reporter )
276 boolean hasFailures = false;
277 Versioning versioning = metadata.getMetadata().getVersioning();
279 new File( repository.getBasedir(), repository.pathOfRemoteRepositoryMetadata( metadata ) ).getParentFile();
280 List versions = FileUtils.getFileNames( versionsDir, "*/*.pom", null, false );
281 for ( Iterator i = versions.iterator(); i.hasNext(); )
283 File path = new File( (String) i.next() );
284 String version = path.getParentFile().getName();
285 if ( !versioning.getVersions().contains( version ) )
287 reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
288 "missing in the metadata." );
296 * Used to gather artifactIds from a groupId directory.
298 * @param groupIdDir the directory of the group
299 * @return the list of artifact ID File objects for each directory
300 * @throws IOException if there was a failure to read the directories
302 private List getArtifactIdFiles( File groupIdDir )
305 List artifactIdFiles = new ArrayList();
307 List fileArray = new ArrayList( Arrays.asList( groupIdDir.listFiles() ) );
308 for ( Iterator files = fileArray.iterator(); files.hasNext(); )
310 File artifactDir = (File) files.next();
312 if ( artifactDir.isDirectory() )
314 List versions = FileUtils.getFileNames( artifactDir, "*/*.pom", null, false );
315 if ( versions.size() > 0 )
317 artifactIdFiles.add( artifactDir );
322 return artifactIdFiles;