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.versioning.InvalidVersionSpecificationException;
24 import org.apache.maven.artifact.versioning.VersionRange;
25 import org.apache.maven.model.Dependency;
26 import org.apache.maven.model.Model;
28 import java.text.MessageFormat;
29 import java.util.Iterator;
30 import java.util.List;
33 * @plexus.component role="org.apache.maven.archiva.reporting.ArtifactReportProcessor" role-hint="dependency"
35 public class DependencyArtifactReportProcessor
36 implements ArtifactReportProcessor
41 private ArtifactFactory artifactFactory;
46 private RepositoryQueryLayerFactory layerFactory;
48 private static final String POM = "pom";
50 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
52 RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
53 processArtifact( artifact, reporter, queryLayer );
55 if ( model != null && POM.equals( artifact.getType() ) )
57 List dependencies = model.getDependencies();
58 processDependencies( dependencies, reporter, queryLayer, artifact );
62 private void processArtifact( Artifact artifact, ReportingDatabase reporter,
63 RepositoryQueryLayer repositoryQueryLayer )
65 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
67 reporter.addFailure( artifact, "Artifact does not exist in the repository" );
71 private void processDependencies( List dependencies, ReportingDatabase reporter,
72 RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
74 if ( dependencies.size() > 0 )
76 Iterator iterator = dependencies.iterator();
77 while ( iterator.hasNext() )
79 Dependency dependency = (Dependency) iterator.next();
83 Artifact artifact = createArtifact( dependency );
85 // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
86 // repository and mapping out the graph
88 if ( artifact.getVersion() == null )
90 // it was a range, for now presume it exists
94 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
96 String reason = MessageFormat.format(
97 "Artifact''s dependency {0} does not exist in the repository",
98 new String[]{getDependencyString( dependency )} );
99 reporter.addFailure( sourceArtifact, reason );
102 catch ( InvalidVersionSpecificationException e )
104 String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
105 new String[]{getDependencyString( dependency ),
106 dependency.getVersion()} );
107 reporter.addFailure( sourceArtifact, reason );
113 static String getDependencyString( Dependency dependency )
115 String str = "(group=" + dependency.getGroupId();
116 str += ", artifact=" + dependency.getArtifactId();
117 str += ", version=" + dependency.getVersion();
118 str += ", type=" + dependency.getType();
119 if ( dependency.getClassifier() != null )
121 str += ", classifier=" + dependency.getClassifier();
127 private Artifact createArtifact( Dependency dependency )
128 throws InvalidVersionSpecificationException
130 VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
134 throw new InvalidVersionSpecificationException( "Dependency version was null" );
137 return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
138 dependency.getType(), dependency.getClassifier(),
139 dependency.getScope() );