1 package org.apache.maven.repository.reporting;
4 * Copyright 2001-2005 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
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 import java.util.HashMap;
22 import java.util.Iterator;
23 import org.apache.maven.artifact.Artifact;
24 import org.apache.maven.artifact.factory.ArtifactFactory;
25 import org.apache.maven.artifact.repository.ArtifactRepository;
26 import org.apache.maven.artifact.repository.metadata.Plugin;
27 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.Snapshot;
29 import org.apache.maven.artifact.repository.metadata.Versioning;
30 import org.apache.maven.repository.RepositoryFileFilter;
34 * This class will report on bad metadata files. These include invalid version declarations and incomplete version
35 * information inside the metadata file. Plugin metadata will be checked for validity of the latest plugin artifacts.
38 public class BadMetadataReportProcessor implements MetadataReportProcessor
41 private ArtifactFactory artifactFactory;
42 private RepositoryQueryLayer repositoryQueryLayer;
44 public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
46 boolean hasFailures = false;
48 String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
49 if ( lastUpdated == null || lastUpdated.length() == 0 )
51 reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
55 if ( metadata.storedInGroupDirectory() )
57 checkPluginMetadata( metadata, repository, reporter );
59 else if ( metadata.storedInArtifactVersionDirectory() )
61 checkSnapshotMetadata( metadata, repository, reporter );
65 if ( !checkMetadataVersions( metadata, repository, reporter ) ) hasFailures = true;
67 if ( checkRepositoryVersions( metadata, repository, reporter ) ) hasFailures = true;
70 if ( !hasFailures ) reporter.addSuccess( metadata );
74 * Checks the plugin metadata
76 public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
77 ArtifactReporter reporter )
79 boolean hasFailures = false;
81 File metadataDir = new File ( repository.getBasedir() + File.pathSeparator + formatAsDirectory( metadata.getGroupId() ) );
83 HashMap prefixes = new HashMap();
84 for( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
86 Plugin plugin = (Plugin) plugins.next();
88 String artifactId = plugin.getArtifactId();
89 if ( artifactId == null || artifactId.length() == 0 )
91 reporter.addFailure( metadata, "Missing or empty artifactId in group metadata." );
95 String prefix = plugin.getPrefix();
96 if ( prefix == null || prefix.length() == 0 )
98 reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId );
103 if ( prefixes.containsKey( prefix ) )
105 reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix );
110 prefixes.put( prefix, plugin );
114 File pluginDir = new File( metadataDir, artifactId );
115 if ( !pluginDir.exists() )
117 reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" );
126 * Checks the snapshot metadata
128 public boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
129 ArtifactReporter reporter )
131 boolean hasFailures = false;
133 Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
134 String timestamp = snapshot.getTimestamp();
135 String buildNumber = String.valueOf( snapshot.getBuildNumber() );
136 String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom";
138 //@todo use wagon instead
139 Artifact artifact = createArtifact( metadata );
140 File artifactFile = new File( repository.pathOf( artifact ) );
141 File snapshotFile = new File( artifactFile.getParentFile(), artifactName );
142 if ( !snapshotFile.exists() )
144 reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." );
152 * Checks the declared metadata versions if the artifacts are present in the repository
154 public boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
155 ArtifactReporter reporter )
157 boolean hasFailures = false;
158 Versioning versioning = metadata.getMetadata().getVersioning();
159 for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
161 String version = (String) versions.next();
163 Artifact artifact = createArtifact( metadata, version );
165 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
167 reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
168 "missing in the repository." );
169 if ( !hasFailures ) hasFailures = true;
176 * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
179 public boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
180 ArtifactReporter reporter )
182 boolean hasFailures = false;
183 Versioning versioning = metadata.getMetadata().getVersioning();
184 String repositoryPath = repository.getBasedir();
185 File versionsDir = new File( repositoryPath, formatAsDirectory( metadata.getGroupId() ) +
186 File.pathSeparator + metadata.getArtifactId() );
187 File[] versions = versionsDir.listFiles( new RepositoryFileFilter() );
188 for( int idx=0; idx<versions.length; idx++ )
190 String version = versions[ idx ].getName();
191 if ( !versioning.getVersions().contains( version ) )
193 reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
194 "missing in the metadata." );
195 if ( !hasFailures ) hasFailures = true;
202 * Formats an artifact groupId to the directory structure format used for storage in repositories
204 private String formatAsDirectory( String directory )
206 return directory.replace( '.', File.pathSeparatorChar );
210 * Used to create an artifact object from a metadata base version
212 private Artifact createArtifact( RepositoryMetadata metadata )
214 return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),
215 metadata.getBaseVersion(), "pom" );
219 * Used to create an artifact object with a specified version
221 private Artifact createArtifact( RepositoryMetadata metadata, String version )
223 return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),