You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BadMetadataReportProcessor.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package org.apache.maven.repository.reporting;
  2. /*
  3. * Copyright 2001-2005 The Apache Software Foundation.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.io.File;
  18. import java.util.HashMap;
  19. import java.util.Iterator;
  20. import org.apache.maven.artifact.Artifact;
  21. import org.apache.maven.artifact.factory.ArtifactFactory;
  22. import org.apache.maven.artifact.repository.ArtifactRepository;
  23. import org.apache.maven.artifact.repository.metadata.Plugin;
  24. import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
  25. import org.apache.maven.artifact.repository.metadata.Snapshot;
  26. import org.apache.maven.artifact.repository.metadata.Versioning;
  27. import org.apache.maven.repository.RepositoryFileFilter;
  28. /**
  29. * This class will report on bad metadata files. These include invalid version declarations and incomplete version
  30. * information inside the metadata file. Plugin metadata will be checked for validity of the latest plugin artifacts.
  31. *
  32. */
  33. public class BadMetadataReportProcessor implements MetadataReportProcessor
  34. {
  35. // plexus components
  36. private ArtifactFactory artifactFactory;
  37. private RepositoryQueryLayer repositoryQueryLayer;
  38. public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
  39. {
  40. boolean hasFailures = false;
  41. String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
  42. if ( lastUpdated == null || lastUpdated.length() == 0 )
  43. {
  44. reporter.addFailure( metadata, "Missing lastUpdated element inside the metadata." );
  45. hasFailures = true;
  46. }
  47. if ( metadata.storedInGroupDirectory() )
  48. {
  49. checkPluginMetadata( metadata, repository, reporter );
  50. }
  51. else if ( metadata.storedInArtifactVersionDirectory() )
  52. {
  53. checkSnapshotMetadata( metadata, repository, reporter );
  54. }
  55. else
  56. {
  57. if ( !checkMetadataVersions( metadata, repository, reporter ) ) hasFailures = true;
  58. if ( checkRepositoryVersions( metadata, repository, reporter ) ) hasFailures = true;
  59. }
  60. if ( !hasFailures ) reporter.addSuccess( metadata );
  61. }
  62. /**
  63. * Checks the plugin metadata
  64. */
  65. public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
  66. ArtifactReporter reporter )
  67. {
  68. boolean hasFailures = false;
  69. File metadataDir = new File ( repository.getBasedir() + File.pathSeparator + formatAsDirectory( metadata.getGroupId() ) );
  70. HashMap prefixes = new HashMap();
  71. for( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
  72. {
  73. Plugin plugin = (Plugin) plugins.next();
  74. String artifactId = plugin.getArtifactId();
  75. if ( artifactId == null || artifactId.length() == 0 )
  76. {
  77. reporter.addFailure( metadata, "Missing or empty artifactId in group metadata." );
  78. hasFailures = true;
  79. }
  80. String prefix = plugin.getPrefix();
  81. if ( prefix == null || prefix.length() == 0 )
  82. {
  83. reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId );
  84. hasFailures = true;
  85. }
  86. else
  87. {
  88. if ( prefixes.containsKey( prefix ) )
  89. {
  90. reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix );
  91. hasFailures = true;
  92. }
  93. else
  94. {
  95. prefixes.put( prefix, plugin );
  96. }
  97. }
  98. File pluginDir = new File( metadataDir, artifactId );
  99. if ( !pluginDir.exists() )
  100. {
  101. reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" );
  102. hasFailures = true;
  103. }
  104. }
  105. return hasFailures;
  106. }
  107. /**
  108. * Checks the snapshot metadata
  109. */
  110. public boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
  111. ArtifactReporter reporter )
  112. {
  113. boolean hasFailures = false;
  114. Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
  115. String timestamp = snapshot.getTimestamp();
  116. String buildNumber = String.valueOf( snapshot.getBuildNumber() );
  117. String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom";
  118. //@todo use wagon instead
  119. Artifact artifact = createArtifact( metadata );
  120. File artifactFile = new File( repository.pathOf( artifact ) );
  121. File snapshotFile = new File( artifactFile.getParentFile(), artifactName );
  122. if ( !snapshotFile.exists() )
  123. {
  124. reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." );
  125. hasFailures = true;
  126. }
  127. return hasFailures;
  128. }
  129. /**
  130. * Checks the declared metadata versions if the artifacts are present in the repository
  131. */
  132. public boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
  133. ArtifactReporter reporter )
  134. {
  135. boolean hasFailures = false;
  136. Versioning versioning = metadata.getMetadata().getVersioning();
  137. for ( Iterator versions = versioning.getVersions().iterator(); versions.hasNext(); )
  138. {
  139. String version = (String) versions.next();
  140. Artifact artifact = createArtifact( metadata, version );
  141. if ( !repositoryQueryLayer.containsArtifact( artifact ) )
  142. {
  143. reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
  144. "missing in the repository." );
  145. if ( !hasFailures ) hasFailures = true;
  146. }
  147. }
  148. return hasFailures;
  149. }
  150. /**
  151. * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
  152. * metadata file.
  153. */
  154. public boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
  155. ArtifactReporter reporter )
  156. {
  157. boolean hasFailures = false;
  158. Versioning versioning = metadata.getMetadata().getVersioning();
  159. String repositoryPath = repository.getBasedir();
  160. File versionsDir = new File( repositoryPath, formatAsDirectory( metadata.getGroupId() ) +
  161. File.pathSeparator + metadata.getArtifactId() );
  162. File[] versions = versionsDir.listFiles( new RepositoryFileFilter() );
  163. for( int idx=0; idx<versions.length; idx++ )
  164. {
  165. String version = versions[ idx ].getName();
  166. if ( !versioning.getVersions().contains( version ) )
  167. {
  168. reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
  169. "missing in the metadata." );
  170. if ( !hasFailures ) hasFailures = true;
  171. }
  172. }
  173. return hasFailures;
  174. }
  175. /**
  176. * Formats an artifact groupId to the directory structure format used for storage in repositories
  177. */
  178. private String formatAsDirectory( String directory )
  179. {
  180. return directory.replace( '.', File.pathSeparatorChar );
  181. }
  182. /**
  183. * Used to create an artifact object from a metadata base version
  184. */
  185. private Artifact createArtifact( RepositoryMetadata metadata )
  186. {
  187. return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),
  188. metadata.getBaseVersion(), "pom" );
  189. }
  190. /**
  191. * Used to create an artifact object with a specified version
  192. */
  193. private Artifact createArtifact( RepositoryMetadata metadata, String version )
  194. {
  195. return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),
  196. version, "pom" );
  197. }
  198. }