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 private static final String ROLE_HINT = "dependency";
52 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
54 RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
55 if ( !queryLayer.containsArtifact( artifact ) )
57 // TODO: is this even possible?
58 addFailure( reporter, artifact, "missing-artifact", "Artifact does not exist in the repository" );
61 if ( model != null && POM.equals( artifact.getType() ) )
63 List dependencies = model.getDependencies();
64 processDependencies( dependencies, reporter, queryLayer, artifact );
68 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
70 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
71 reporter.addFailure( artifact, ROLE_HINT, problem, reason );
74 private void processDependencies( List dependencies, ReportingDatabase reporter,
75 RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
77 if ( dependencies.size() > 0 )
79 Iterator iterator = dependencies.iterator();
80 while ( iterator.hasNext() )
82 Dependency dependency = (Dependency) iterator.next();
86 Artifact artifact = createArtifact( dependency );
88 // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
89 // repository and mapping out the graph
91 if ( artifact.getVersion() == null )
93 // it was a range, for now presume it exists
97 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
99 String reason = MessageFormat.format(
100 "Artifact''s dependency {0} does not exist in the repository",
101 new String[]{getDependencyString( dependency )} );
102 addFailure( reporter, sourceArtifact, "missing-dependency:" + getDependencyKey( dependency ),
106 catch ( InvalidVersionSpecificationException e )
108 String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
109 new String[]{getDependencyString( dependency ),
110 dependency.getVersion()} );
111 addFailure( reporter, sourceArtifact, "bad-version:" + getDependencyKey( dependency ), reason );
117 private String getDependencyKey( Dependency dependency )
119 String str = dependency.getGroupId();
120 str += ":" + dependency.getArtifactId();
121 str += ":" + dependency.getVersion();
122 str += ":" + dependency.getType();
123 if ( dependency.getClassifier() != null )
125 str += ":" + dependency.getClassifier();
130 static String getDependencyString( Dependency dependency )
132 String str = "(group=" + dependency.getGroupId();
133 str += ", artifact=" + dependency.getArtifactId();
134 str += ", version=" + dependency.getVersion();
135 str += ", type=" + dependency.getType();
136 if ( dependency.getClassifier() != null )
138 str += ", classifier=" + dependency.getClassifier();
144 private Artifact createArtifact( Dependency dependency )
145 throws InvalidVersionSpecificationException
147 VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
151 throw new InvalidVersionSpecificationException( "Dependency version was null" );
154 return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
155 dependency.getType(), dependency.getClassifier(),
156 dependency.getScope() );