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.repository.ArtifactRepository;
32  import org.apache.maven.doxia.sink.Sink;
33  import org.apache.maven.model.Dependency;
34  import org.apache.maven.plugin.logging.Log;
35  import org.apache.maven.project.MavenProjectBuilder;
36  import org.apache.maven.report.projectinfo.ProjectInfoReportUtils;
37  import org.apache.maven.report.projectinfo.dependencies.ManagementDependencies;
38  import org.apache.maven.reporting.AbstractMavenReportRenderer;
39  import org.codehaus.plexus.i18n.I18N;
40  import org.codehaus.plexus.util.StringUtils;
41  
42  /**
43   * @author Nick Stolwijk
44   * @version $Id: DependencyManagementRenderer.java 728546 2008-12-21 22:56:51Z bentmann $
45   * @since 2.1
46   */
47  public class DependencyManagementRenderer
48      extends AbstractMavenReportRenderer
49  {
50      private final ManagementDependencies dependencies;
51  
52      private final Locale locale;
53  
54      private final I18N i18n;
55  
56      private final Log log;
57  
58      private final ArtifactFactory artifactFactory;
59  
60      private final MavenProjectBuilder mavenProjectBuilder;
61  
62      private final List remoteRepositories;
63  
64      private final ArtifactRepository localRepository;
65  
66      /**
67       * Default constructor
68       *
69       * @param sink
70       * @param locale
71       * @param i18n
72       * @param log
73       * @param artifactFactory
74       * @param dependencies
75       * @param mavenProjectBuilder
76       * @param remoteRepositories
77       * @param localRepository
78       */
79      public DependencyManagementRenderer( Sink sink, Locale locale, I18N i18n, Log log,
80                                           ManagementDependencies dependencies, ArtifactFactory artifactFactory,
81                                           MavenProjectBuilder mavenProjectBuilder, List remoteRepositories,
82                                           ArtifactRepository localRepository )
83      {
84          super( sink );
85  
86          this.locale = locale;
87          this.i18n = i18n;
88          this.log = log;
89          this.dependencies = dependencies;
90          this.artifactFactory = artifactFactory;
91          this.mavenProjectBuilder = mavenProjectBuilder;
92          this.remoteRepositories = remoteRepositories;
93          this.localRepository = localRepository;
94      }
95  
96      // ----------------------------------------------------------------------
97      // Public methods
98      // ----------------------------------------------------------------------
99  
100     /** {@inheritDoc} */
101     public String getTitle()
102     {
103         return getReportString( "report.dependencyManagement.title" );
104     }
105 
106     /** {@inheritDoc} */
107     public void renderBody()
108     {
109         // Dependencies report
110 
111         if ( !dependencies.hasDependencies() )
112         {
113             startSection( getTitle() );
114 
115             paragraph( getReportString( "report.dependencyManagement.nolist" ) );
116 
117             endSection();
118 
119             return;
120         }
121 
122         // === Section: Project Dependencies.
123         renderSectionProjectDependencies();
124     }
125 
126     // ----------------------------------------------------------------------
127     // Private methods
128     // ----------------------------------------------------------------------
129 
130     private void renderSectionProjectDependencies()
131     {
132         startSection( getTitle() );
133 
134         // collect dependencies by scope
135         Map dependenciesByScope = dependencies.getManagementDependenciesByScope();
136 
137         renderDependenciesForAllScopes( dependenciesByScope );
138 
139         endSection();
140     }
141 
142     private void renderDependenciesForAllScopes( Map dependenciesByScope )
143     {
144         renderDependenciesForScope( Artifact.SCOPE_COMPILE, (List) dependenciesByScope.get( Artifact.SCOPE_COMPILE ) );
145         renderDependenciesForScope( Artifact.SCOPE_RUNTIME, (List) dependenciesByScope.get( Artifact.SCOPE_RUNTIME ) );
146         renderDependenciesForScope( Artifact.SCOPE_TEST, (List) dependenciesByScope.get( Artifact.SCOPE_TEST ) );
147         renderDependenciesForScope( Artifact.SCOPE_PROVIDED,
148                                     (List) dependenciesByScope.get( Artifact.SCOPE_PROVIDED ) );
149         renderDependenciesForScope( Artifact.SCOPE_SYSTEM, (List) dependenciesByScope.get( Artifact.SCOPE_SYSTEM ) );
150     }
151 
152     private String[] getDependencyTableHeader( boolean hasClassifier )
153     {
154         String groupId = getReportString( "report.dependencyManagement.column.groupId" );
155         String artifactId = getReportString( "report.dependencyManagement.column.artifactId" );
156         String version = getReportString( "report.dependencyManagement.column.version" );
157         String classifier = getReportString( "report.dependencyManagement.column.classifier" );
158         String type = getReportString( "report.dependencyManagement.column.type" );
159 
160         if ( hasClassifier )
161         {
162             return new String[] { groupId, artifactId, version, classifier, type };
163         }
164 
165         return new String[] { groupId, artifactId, version, type };
166     }
167 
168     private void renderDependenciesForScope( String scope, List artifacts )
169     {
170         if ( artifacts != null )
171         {
172             // can't use straight artifact comparison because we want optional last
173             Collections.sort( artifacts, getDependencyComparator() );
174 
175             startSection( scope );
176 
177             paragraph( getReportString( "report.dependencyManagement.intro." + scope ) );
178             startTable();
179 
180             boolean hasClassifier = false;
181             for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
182             {
183                 Dependency dependency = (Dependency) iterator.next();
184                 if ( StringUtils.isNotEmpty( dependency.getClassifier() ) )
185                 {
186                     hasClassifier = true;
187                     break;
188                 }
189             }
190 
191             String[] tableHeader = getDependencyTableHeader( hasClassifier );
192             tableHeader( tableHeader );
193 
194             for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
195             {
196                 Dependency dependency = (Dependency) iterator.next();
197                 tableRow( getDependencyRow( dependency, hasClassifier ) );
198             }
199             endTable();
200 
201             endSection();
202         }
203     }
204 
205     private String[] getDependencyRow( Dependency dependency, boolean hasClassifier )
206     {
207         Artifact artifact = artifactFactory.createParentArtifact( dependency.getGroupId(), dependency.getArtifactId(),
208                                                                   dependency.getVersion() );
209         String url =
210             ProjectInfoReportUtils.getArtifactUrl( artifactFactory, artifact, mavenProjectBuilder, remoteRepositories,
211                                                    localRepository );
212         String artifactIdCell = ProjectInfoReportUtils.getArtifactIdCell( artifact.getArtifactId(), url );
213 
214         if ( hasClassifier )
215         {
216             return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
217                 dependency.getClassifier(), dependency.getType() };
218         }
219 
220         return new String[] { dependency.getGroupId(), artifactIdCell, dependency.getVersion(),
221             dependency.getType() };
222     }
223 
224     private Comparator getDependencyComparator()
225     {
226         return new Comparator()
227         {
228             public int compare( Object o1, Object o2 )
229             {
230                 Dependency a1 = (Dependency) o1;
231                 Dependency a2 = (Dependency) o2;
232 
233                 int result = a1.getGroupId().compareTo( a2.getGroupId() );
234                 if ( result == 0 )
235                 {
236                     result = a1.getArtifactId().compareTo( a2.getArtifactId() );
237                     if ( result == 0 )
238                     {
239                         result = a1.getType().compareTo( a2.getType() );
240                         if ( result == 0 )
241                         {
242                             if ( a1.getClassifier() == null )
243                             {
244                                 if ( a2.getClassifier() != null )
245                                 {
246                                     result = 1;
247                                 }
248                             }
249                             else
250                             {
251                                 if ( a2.getClassifier() != null )
252                                 {
253                                     result = a1.getClassifier().compareTo( a2.getClassifier() );
254                                 }
255                                 else
256                                 {
257                                     result = -1;
258                                 }
259                             }
260                             if ( result == 0 )
261                             {
262                                 // We don't consider the version range in the comparison, just the resolved version
263                                 result = a1.getVersion().compareTo( a2.getVersion() );
264                             }
265                         }
266                     }
267                 }
268 
269                 return result;
270             }
271         };
272     }
273 
274     private String getReportString( String key )
275     {
276         return i18n.getString( "project-info-report", locale, key );
277     }
278 }