PR: MRM-16

Added missing metadata checks... 

git-svn-id: https://svn.apache.org/repos/asf/maven/repository-manager/trunk@351563 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Edwin L. Punzalan 2005-12-02 03:23:43 +00:00
parent c1678e136b
commit 286bd7bd95
2 changed files with 112 additions and 80 deletions

View File

@ -1,34 +1,34 @@
package org.apache.maven.repository.reporting; package org.apache.maven.repository.reporting;
/* /*
* Copyright 2001-2005 The Apache Software Foundation. * Copyright 2001-2005 The Apache Software Foundation.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import java.io.File; import java.io.File;
import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory; import org.apache.maven.artifact.factory.ArtifactFactory;
import org.apache.maven.artifact.manager.WagonManager;
import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.metadata.Plugin;
import org.apache.maven.artifact.repository.metadata.RepositoryMetadata; import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
import org.apache.maven.artifact.repository.metadata.Snapshot; import org.apache.maven.artifact.repository.metadata.Snapshot;
import org.apache.maven.artifact.repository.metadata.Versioning; import org.apache.maven.artifact.repository.metadata.Versioning;
import org.apache.maven.repository.RepositoryFileFilter; import org.apache.maven.repository.RepositoryFileFilter;
import org.apache.maven.wagon.ResourceDoesNotExistException;
import org.apache.maven.wagon.TransferFailedException;
/** /**
* This class will report on bad metadata files. These include invalid version declarations and incomplete version * This class will report on bad metadata files. These include invalid version declarations and incomplete version
@ -37,13 +37,14 @@ import org.apache.maven.wagon.TransferFailedException;
*/ */
public class BadMetadataReporter implements MetadataReportProcessor public class BadMetadataReporter implements MetadataReportProcessor
{ {
private WagonManager wagon; // plexus components
private ArtifactFactory artifactFactory; private ArtifactFactory artifactFactory;
private RepositoryQueryLayer repositoryQueryLayer;
public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter ) public void processMetadata( RepositoryMetadata metadata, ArtifactRepository repository, ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated(); String lastUpdated = metadata.getMetadata().getVersioning().getLastUpdated();
if ( lastUpdated == null || lastUpdated.length() == 0 ) if ( lastUpdated == null || lastUpdated.length() == 0 )
{ {
@ -57,62 +58,101 @@ public class BadMetadataReporter implements MetadataReportProcessor
} }
else if ( metadata.storedInArtifactVersionDirectory() ) else if ( metadata.storedInArtifactVersionDirectory() )
{ {
//snapshot metadata checkSnapshotMetadata( metadata, repository, reporter );
} }
else else
{ {
if ( !checkMetadataVersions( metadata, repository, reporter ) ) hasFailures = true; if ( !checkMetadataVersions( metadata, repository, reporter ) ) hasFailures = true;
if ( checkRepositoryVersions( metadata, repository, reporter ) ) hasFailures = true; if ( checkRepositoryVersions( metadata, repository, reporter ) ) hasFailures = true;
} }
if ( !hasFailures ) reporter.addSuccess( metadata ); if ( !hasFailures ) reporter.addSuccess( metadata );
} }
/** /**
* Checks the plugin metadata * Checks the plugin metadata
*/ */
public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository, public boolean checkPluginMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
File metadataDir = new File ( repository.getBasedir() + File.pathSeparator + formatAsDirectory( metadata.getGroupId() ) );
return hasFailures; HashMap prefixes = new HashMap();
for( Iterator plugins = metadata.getMetadata().getPlugins().iterator(); plugins.hasNext(); )
{
Plugin plugin = (Plugin) plugins.next();
String artifactId = plugin.getArtifactId();
if ( artifactId == null || artifactId.length() == 0 )
{
reporter.addFailure( metadata, "Missing or empty artifactId in group metadata." );
hasFailures = true;
}
String prefix = plugin.getPrefix();
if ( prefix == null || prefix.length() == 0 )
{
reporter.addFailure( metadata, "Missing or empty plugin prefix for artifactId " + artifactId );
hasFailures = true;
}
else
{
if ( prefixes.containsKey( prefix ) )
{
reporter.addFailure( metadata, "Duplicate plugin prefix found: " + prefix );
hasFailures = true;
}
else
{
prefixes.put( prefix, plugin );
}
}
File pluginDir = new File( metadataDir, artifactId );
if ( !pluginDir.exists() )
{
reporter.addFailure( metadata, "Metadata plugin " + artifactId + " is not present in the repository" );
hasFailures = true;
}
}
return hasFailures;
} }
/** /**
* Checks the snapshot metadata * Checks the snapshot metadata
*/ */
public boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository, public boolean checkSnapshotMetadata( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot(); Snapshot snapshot = metadata.getMetadata().getVersioning().getSnapshot();
String timestamp = snapshot.getTimestamp(); String timestamp = snapshot.getTimestamp();
String buildNumber = String.valueOf( snapshot.getBuildNumber() ); String buildNumber = String.valueOf( snapshot.getBuildNumber() );
String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom"; String artifactName = metadata.getArtifactId() + "-" + timestamp + "-" + buildNumber + ".pom";
//@todo use wagon instead //@todo use wagon instead
Artifact artifact = createArtifact( metadata ); Artifact artifact = createArtifact( metadata );
File artifactFile = new File ( repository.pathOf( artifact ) ); File artifactFile = new File( repository.pathOf( artifact ) );
File snapshotFile = new File( artifactFile.getParentFile(), artifactName ); File snapshotFile = new File( artifactFile.getParentFile(), artifactName );
if ( !snapshotFile.exists() ) if ( !snapshotFile.exists() )
{ {
reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." ); reporter.addFailure( metadata, "Snapshot artifact " + artifactName + " does not exist." );
hasFailures = true; hasFailures = true;
} }
return hasFailures; return hasFailures;
} }
/** /**
* Checks the declared metadata versions if the artifacts are present in the repository * Checks the declared metadata versions if the artifacts are present in the repository
*/ */
public boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository, public boolean checkMetadataVersions( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
Versioning versioning = metadata.getMetadata().getVersioning(); Versioning versioning = metadata.getMetadata().getVersioning();
@ -121,25 +161,11 @@ public class BadMetadataReporter implements MetadataReportProcessor
String version = (String) versions.next(); String version = (String) versions.next();
Artifact artifact = createArtifact( metadata, version ); Artifact artifact = createArtifact( metadata, version );
try if ( !repositoryQueryLayer.containsArtifact( artifact ) )
{
wagon.getArtifact( artifact, repository );
}
catch ( TransferFailedException e )
{
reporter.addWarning( artifact, "An error occurred during the transfer of the artifact in " +
"the repository." );
}
catch ( ResourceDoesNotExistException e )
{
//do nothing, will check later that this artifact has not been resolved
}
if ( !artifact.isResolved() )
{ {
reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " + reporter.addFailure( metadata, "Artifact version " + version + " is present in metadata but " +
"missing in the repository." ); "missing in the repository." );
if ( !hasFailures ) hasFailures = true; if ( !hasFailures ) hasFailures = true;
} }
} }
@ -147,17 +173,17 @@ public class BadMetadataReporter implements MetadataReportProcessor
} }
/** /**
* Searches the artifact repository directory for all versions and verifies that all of them are listed in the * Searches the artifact repository directory for all versions and verifies that all of them are listed in the
* metadata file. * metadata file.
*/ */
public boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository, public boolean checkRepositoryVersions( RepositoryMetadata metadata, ArtifactRepository repository,
ArtifactReporter reporter ) ArtifactReporter reporter )
{ {
boolean hasFailures = false; boolean hasFailures = false;
Versioning versioning = metadata.getMetadata().getVersioning(); Versioning versioning = metadata.getMetadata().getVersioning();
String repositoryPath = repository.getBasedir(); String repositoryPath = repository.getBasedir();
File versionsDir = new File( repositoryPath, formatAsDirectory( metadata.getGroupId() ) + File versionsDir = new File( repositoryPath, formatAsDirectory( metadata.getGroupId() ) +
File.pathSeparator + metadata.getArtifactId() ); File.pathSeparator + metadata.getArtifactId() );
File[] versions = versionsDir.listFiles( new RepositoryFileFilter() ); File[] versions = versionsDir.listFiles( new RepositoryFileFilter() );
for( int idx=0; idx<versions.length; idx++ ) for( int idx=0; idx<versions.length; idx++ )
{ {
@ -165,7 +191,7 @@ public class BadMetadataReporter implements MetadataReportProcessor
if ( !versioning.getVersions().contains( version ) ) if ( !versioning.getVersions().contains( version ) )
{ {
reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " + reporter.addFailure( metadata, "Artifact version " + version + " found in the repository but " +
"missing in the metadata." ); "missing in the metadata." );
if ( !hasFailures ) hasFailures = true; if ( !hasFailures ) hasFailures = true;
} }
} }
@ -180,15 +206,21 @@ public class BadMetadataReporter implements MetadataReportProcessor
return directory.replace( '.', File.pathSeparatorChar ); return directory.replace( '.', File.pathSeparatorChar );
} }
/**
* Used to create an artifact object from a metadata base version
*/
private Artifact createArtifact( RepositoryMetadata metadata ) private Artifact createArtifact( RepositoryMetadata metadata )
{ {
return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(), return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),
metadata.getBaseVersion(), "pom" ); metadata.getBaseVersion(), "pom" );
} }
/**
* Used to create an artifact object with a specified version
*/
private Artifact createArtifact( RepositoryMetadata metadata, String version ) private Artifact createArtifact( RepositoryMetadata metadata, String version )
{ {
return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(), return artifactFactory.createBuildArtifact( metadata.getGroupId(), metadata.getArtifactId(),
version, "pom" ); version, "pom" );
} }
} }

View File

@ -18,7 +18,7 @@
<implementation>org.apache.maven.repository.reporting.BadMetadataReporter</implementation> <implementation>org.apache.maven.repository.reporting.BadMetadataReporter</implementation>
<requirements> <requirements>
<requirement> <requirement>
<role>org.apache.maven.artifact.manager.WagonManager</role> <role>org.apache.maven.repository.reporting.RepositoryQueryLayer</role>
</requirement> </requirement>
<requirement> <requirement>
<role>org.apache.maven.artifact.factory.ArtifactFactory</role> <role>org.apache.maven.artifact.factory.ArtifactFactory</role>