View Javadoc
1   package org.apache.maven.report.projectinfo.dependencies.renderer;
2   
3   /*
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
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.Artifact;
30  import org.apache.maven.artifact.factory.ArtifactFactory;
31  import org.apache.maven.artifact.metadata.ArtifactMetadataRetrievalException;
32  import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
33  import org.apache.maven.artifact.repository.ArtifactRepository;
34  import org.apache.maven.artifact.versioning.ArtifactVersion;
35  import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException;
36  import org.apache.maven.artifact.versioning.VersionRange;
37  import org.apache.maven.doxia.sink.Sink;
38  import org.apache.maven.model.Dependency;
39  import org.apache.maven.model.License;
40  import org.apache.maven.plugin.logging.Log;
41  import org.apache.maven.project.MavenProject;
42  import org.apache.maven.project.MavenProjectBuilder;
43  import org.apache.maven.project.ProjectBuildingException;
44  import org.apache.maven.report.projectinfo.AbstractProjectInfoRenderer;
45  import org.apache.maven.report.projectinfo.ProjectInfoReportUtils;
46  import org.apache.maven.report.projectinfo.dependencies.ManagementDependencies;
47  import org.apache.maven.report.projectinfo.dependencies.RepositoryUtils;
48  import org.codehaus.plexus.i18n.I18N;
49  import org.codehaus.plexus.util.StringUtils;
50  
51  /**
52   * @author Nick Stolwijk
53   * @version $Id: DependencyManagementRenderer.html 981339 2016-02-28 15:47:44Z michaelo $
54   * @since 2.1
55   */
56  public class DependencyManagementRenderer
57      extends AbstractProjectInfoRenderer
58  {
59      private final ManagementDependencies dependencies;
60  
61      private final Log log;
62  
63      private final ArtifactMetadataSource artifactMetadataSource;
64  
65      private final ArtifactFactory artifactFactory;
66  
67      private final MavenProjectBuilder mavenProjectBuilder;
68  
69      private final List<ArtifactRepository> remoteRepositories;
70  
71      private final ArtifactRepository localRepository;
72  
73      private final RepositoryUtils repoUtils;
74  
75      /**
76       * Default constructor
77       *
78       * @param sink {@link Sink}
79       * @param locale {@link Locale}
80       * @param i18n {@link I18N}
81       * @param log {@link Log}
82       * @param dependencies {@link ManagementDependencies}
83       * @param artifactMetadataSource {@link ArtifactMetadataSource}
84       * @param artifactFactory {@link ArtifactFactory}
85       * @param mavenProjectBuilder {@link MavenProjectBuilder}
86       * @param remoteRepositories {@link ArtifactRepository}
87       * @param localRepository {@link ArtifactRepository}
88       * @param repoUtils {@link RepositoryUtils}
89       */
90      public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log log,
91                                           ManagementDependencies dependencies,
92                                           ArtifactMetadataSource artifactMetadataSource,
93                                           ArtifactFactory artifactFactory, MavenProjectBuilder mavenProjectBuilder,
94                                           List<ArtifactRepository> remoteRepositories,
95                                           ArtifactRepository localRepository, RepositoryUtils repoUtils )
96      {
97          super( sink, i18n, locale );
98  
99          this.log = log;
100         this.dependencies = dependencies;
101         this.artifactMetadataSource = artifactMetadataSource;
102         this.artifactFactory = artifactFactory;
103         this.mavenProjectBuilder = mavenProjectBuilder;
104         this.remoteRepositories = remoteRepositories;
105         this.localRepository = localRepository;
106         this.repoUtils = repoUtils;
107     }
108 
109     // ----------------------------------------------------------------------
110     // Public methods
111     // ----------------------------------------------------------------------
112 
113     @Override
114     protected String getI18Nsection()
115     {
116         return "dependency-management";
117     }
118 
119     @Override
120     public void renderBody()
121     {
122         // Dependencies report
123 
124         if ( !dependencies.hasDependencies() )
125         {
126             startSection( getTitle() );
127 
128             paragraph( getI18nString( "nolist" ) );
129 
130             endSection();
131 
132             return;
133         }
134 
135         // === Section: Project Dependencies.
136         renderSectionProjectDependencies();
137     }
138 
139     // ----------------------------------------------------------------------
140     // Private methods
141     // ----------------------------------------------------------------------
142 
143     private void renderSectionProjectDependencies()
144     {
145         startSection( getTitle() );
146 
147         // collect dependencies by scope
148         Map<String, List<Dependency>> dependenciesByScope = dependencies.getManagementDependenciesByScope();
149 
150         renderDependenciesForAllScopes( dependenciesByScope );
151 
152         endSection();
153     }
154 
155     private void renderDependenciesForAllScopes( Map<String, List<Dependency>> dependenciesByScope )
156     {
157         renderDependenciesForScope( Artifact.SCOPE_COMPILE, dependenciesByScope.get( Artifact.SCOPE_COMPILE ) );
158         renderDependenciesForScope( Artifact.SCOPE_RUNTIME, dependenciesByScope.get( Artifact.SCOPE_RUNTIME ) );
159         renderDependenciesForScope( Artifact.SCOPE_TEST, dependenciesByScope.get( Artifact.SCOPE_TEST ) );
160         renderDependenciesForScope( Artifact.SCOPE_PROVIDED, dependenciesByScope.get( Artifact.SCOPE_PROVIDED ) );
161         renderDependenciesForScope( Artifact.SCOPE_SYSTEM, dependenciesByScope.get( Artifact.SCOPE_SYSTEM ) );
162     }
163 
164     private String[] getDependencyTableHeader( boolean hasClassifier )
165     {
166         String groupId = getI18nString( "column.groupId" );
167         String artifactId = getI18nString( "column.artifactId" );
168         String version = getI18nString( "column.version" );
169         String classifier = getI18nString( "column.classifier" );
170         String type = getI18nString( "column.type" );
171         String license = getI18nString( "column.license" );
172 
173         if ( hasClassifier )
174         {
175             return new String[] { groupId, artifactId, version, classifier, type, license };
176         }
177 
178         return new String[] { groupId, artifactId, version, type, license };
179     }
180 
181     private void renderDependenciesForScope( String scope, List<Dependency> artifacts )
182     {
183         if ( artifacts != null )
184         {
185             // can't use straight artifact comparison because we want optional last
186             Collections.sort( artifacts, getDependencyComparator() );
187 
188             startSection( scope );
189 
190             paragraph( getI18nString( "intro." + scope ) );
191             startTable();
192 
193             boolean hasClassifier = false;
194             for ( Dependency dependency : artifacts )
195             {
196                 if ( StringUtils.isNotEmpty( dependency.getClassifier() ) )
197                 {
198                     hasClassifier = true;
199                     break;
200                 }
201             }
202 
203             String[] tableHeader = getDependencyTableHeader( hasClassifier );
204             tableHeader( tableHeader );
205 
206             for ( Dependency dependency : artifacts )
207             {
208                 tableRow( getDependencyRow( dependency, hasClassifier ) );
209             }
210             endTable();
211 
212             endSection();
213         }
214     }
215 
216     @SuppressWarnings( "unchecked" )
217     private String[] getDependencyRow( Dependency dependency, boolean hasClassifier )
218     {
219         Artifact artifact =
220             artifactFactory.createProjectArtifact( dependency.getGroupId(), dependency.getArtifactId(),
221                                                    dependency.getVersion() );
222 
223         StringBuilder licensesBuffer = new StringBuilder();
224         String url = null;
225         try
226         {
227             VersionRange range = VersionRange.createFromVersionSpec( dependency.getVersion() );
228 
229             if ( range.getRecommendedVersion() == null )
230             {
231                 // MPIR-216: no direct version but version range: need to choose one precise version
232                 log.debug( "Resolving range for DependencyManagement on " + artifact.getId() );
233 
234                 List<ArtifactVersion> versions =
235                     artifactMetadataSource.retrieveAvailableVersions( artifact, localRepository, remoteRepositories );
236 
237                 // only use versions from range
238                 for ( Iterator<ArtifactVersion> iter = versions.iterator(); iter.hasNext(); )
239                 {
240                     if ( ! range.containsVersion( iter.next() ) )
241                     {
242                         iter.remove();
243                     }
244                 }
245 
246                 // select latest, assuming pom information will be the most accurate
247                 if ( versions.size() > 0 )
248                 {
249                     ArtifactVersion maxArtifactVersion = Collections.max( versions );
250 
251                     artifact.setVersion( maxArtifactVersion.toString() );
252                     log.debug( "DependencyManagement resolved: " + artifact.getId() );
253                 }
254             }
255 
256             url =
257                 ProjectInfoReportUtils.getArtifactUrl( artifactFactory, artifact, mavenProjectBuilder,
258                                                        remoteRepositories, localRepository );
259 
260             MavenProject artifactProject = repoUtils.getMavenProjectFromRepository( artifact );
261 
262             List<License> licenses = artifactProject.getLicenses();
263             for ( License license : licenses )
264             {
265                 String licenseCell = ProjectInfoReportUtils.getArtifactIdCell( license.getName(), license.getUrl() );
266                 if ( licensesBuffer.length() > 0 )
267                 {
268                     licensesBuffer.append( ", " );
269                 }
270                 licensesBuffer.append( licenseCell );
271             }
272         }
273         catch ( InvalidVersionSpecificationException e )
274         {
275             log.warn( "Unable to parse version for " + artifact.getId(), e );
276         }
277         catch ( ArtifactMetadataRetrievalException e )
278         {
279             log.warn( "Unable to retrieve versions for " + artifact.getId() + " from repository.", e );
280         }
281         catch ( ProjectBuildingException e )
282         {
283             log.warn( "Unable to create Maven project for " + artifact.getId() + " from repository.", e );
284         }
285 
286         String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell( artifact.getArtifactId(), url );
287 
288         if ( hasClassifier )
289         {
290             return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
291                 dependency.getClassifier(), dependency.getType(), licensesBuffer.toString() };
292         }
293 
294         return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
295             dependency.getType(), licensesBuffer.toString() };
296     }
297 
298     private Comparator<Dependency> getDependencyComparator()
299     {
300         return new Comparator<Dependency>()
301         {
302             public int compare( Dependency a1, Dependency a2 )
303             {
304                 int result = a1.getGroupId().compareTo( a2.getGroupId() );
305                 if ( result != 0 )
306                 {
307                     return result;
308                 }
309 
310                 result = a1.getArtifactId().compareTo( a2.getArtifactId() );
311                 if ( result != 0 )
312                 {
313                     return result;
314                 }
315 
316                 result = a1.getType().compareTo( a2.getType() );
317                 if ( result != 0 )
318                 {
319                     return result;
320                 }
321 
322                 if ( a1.getClassifier() == null )
323                 {
324                     if ( a2.getClassifier() != null )
325                     {
326                         return 1;
327                     }
328                 }
329                 else
330                 {
331                     if ( a2.getClassifier() != null )
332                     {
333                         result = a1.getClassifier().compareTo( a2.getClassifier() );
334                     }
335                     else
336                     {
337                         return -1;
338                     }
339                 }
340 
341                 if ( result != 0 )
342                 {
343                     return result;
344                 }
345 
346                 // We don't consider the version range in the comparison, just the resolved version
347                 return a1.getVersion().compareTo( a2.getVersion() );
348             }
349         };
350     }
351 }