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