1 package org.apache.maven.archiva.reporting.processor;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.maven.archiva.layer.RepositoryQueryLayer;
23 import org.apache.maven.archiva.layer.RepositoryQueryLayerFactory;
24 import org.apache.maven.archiva.reporting.database.ReportingDatabase;
25 import org.apache.maven.artifact.Artifact;
26 import org.apache.maven.artifact.factory.ArtifactFactory;
27 import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
28 import org.apache.maven.artifact.versioning.VersionRange;
29 import org.apache.maven.model.Dependency;
30 import org.apache.maven.model.Model;
32 import java.text.MessageFormat;
33 import java.util.Iterator;
34 import java.util.List;
37 * @plexus.component role="org.apache.maven.archiva.reporting.processor.ArtifactReportProcessor" role-hint="dependency"
39 public class DependencyArtifactReportProcessor
40 implements ArtifactReportProcessor
45 private ArtifactFactory artifactFactory;
50 private RepositoryQueryLayerFactory layerFactory;
52 private static final String POM = "pom";
54 private static final String ROLE_HINT = "dependency";
56 public void processArtifact( Artifact artifact, Model model, ReportingDatabase reporter )
58 RepositoryQueryLayer queryLayer = layerFactory.createRepositoryQueryLayer( artifact.getRepository() );
59 if ( !queryLayer.containsArtifact( artifact ) )
61 // TODO: is this even possible?
62 addFailure( reporter, artifact, "missing-artifact", "Artifact does not exist in the repository" );
65 if ( model != null && POM.equals( artifact.getType() ) )
67 List dependencies = model.getDependencies();
68 processDependencies( dependencies, reporter, queryLayer, artifact );
72 private static void addFailure( ReportingDatabase reporter, Artifact artifact, String problem, String reason )
74 // TODO: reason could be an i18n key derived from the processor and the problem ID and the
75 reporter.addFailure( artifact, ROLE_HINT, problem, reason );
78 private void processDependencies( List dependencies, ReportingDatabase reporter,
79 RepositoryQueryLayer repositoryQueryLayer, Artifact sourceArtifact )
81 if ( dependencies.size() > 0 )
83 Iterator iterator = dependencies.iterator();
84 while ( iterator.hasNext() )
86 Dependency dependency = (Dependency) iterator.next();
90 Artifact artifact = createArtifact( dependency );
92 // TODO: handle ranges properly. We should instead be mapping out all the artifacts in the
93 // repository and mapping out the graph
95 if ( artifact.getVersion() == null )
97 // it was a range, for now presume it exists
101 if ( !repositoryQueryLayer.containsArtifact( artifact ) )
103 String reason = MessageFormat.format(
104 "Artifact''s dependency {0} does not exist in the repository",
105 new String[]{getDependencyString( dependency )} );
106 addFailure( reporter, sourceArtifact, "missing-dependency:" + getDependencyKey( dependency ),
110 catch ( InvalidVersionSpecificationException e )
112 String reason = MessageFormat.format( "Artifact''s dependency {0} contains an invalid version {1}",
113 new String[]{getDependencyString( dependency ),
114 dependency.getVersion()} );
115 addFailure( reporter, sourceArtifact, "bad-version:" + getDependencyKey( dependency ), reason );
121 private String getDependencyKey( Dependency dependency )
123 String str = dependency.getGroupId();
124 str += ":" + dependency.getArtifactId();
125 str += ":" + dependency.getVersion();
126 str += ":" + dependency.getType();
127 if ( dependency.getClassifier() != null )
129 str += ":" + dependency.getClassifier();
134 static String getDependencyString( Dependency dependency )
136 String str = "(group=" + dependency.getGroupId();
137 str += ", artifact=" + dependency.getArtifactId();
138 str += ", version=" + dependency.getVersion();
139 str += ", type=" + dependency.getType();
140 if ( dependency.getClassifier() != null )
142 str += ", classifier=" + dependency.getClassifier();
148 private Artifact createArtifact( Dependency dependency )
149 throws InvalidVersionSpecificationException
151 VersionRange spec = VersionRange.createFromVersionSpec( dependency.getVersion() );
155 throw new InvalidVersionSpecificationException( "Dependency version was null" );
158 return artifactFactory.createDependencyArtifact( dependency.getGroupId(), dependency.getArtifactId(), spec,
159 dependency.getType(), dependency.getClassifier(),
160 dependency.getScope() );