View Javadoc
1   package org.apache.maven.plugins.dependency.analyze;
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.Iterator;
23  import java.util.ResourceBundle;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
28  
29  /**
30   * This is the view part of the analyze-report mojo. It generates the HTML report for the project website. The HTML
31   * output is same as the CLI output.
32   */
33  public class AnalyzeReportView
34  {
35      /**
36       * Generates the HTML report.
37       * 
38       * @param analysis {@link ProjectDependencyAnalysis}
39       * @param sink {@link Sink}
40       * @param bundle {@link ResourceBundle}
41       */
42      public void generateReport( ProjectDependencyAnalysis analysis, Sink sink, ResourceBundle bundle )
43      {
44          sink.head();
45          sink.title();
46          sink.text( bundle.getString( "analyze.report.header" ) );
47          sink.title_();
48          sink.head_();
49          sink.body();
50  
51          // Generate title
52          sink.section1();
53          sink.sectionTitle1();
54          sink.text( bundle.getString( "analyze.report.mainTitle" ) );
55          sink.sectionTitle1_();
56  
57          // Generate Used Declared dependencies:
58          sink.section2();
59          sink.sectionTitle2();
60          sink.text( bundle.getString( "analyze.report.UsedDeclaredDependencies" ) );
61          sink.sectionTitle2_();
62          if ( analysis.getUsedDeclaredArtifacts().isEmpty() )
63          {
64              sink.paragraph();
65              sink.text( bundle.getString( "analyze.report.noDependency" ) );
66              sink.paragraph_();
67              sink.horizontalRule();
68          }
69          else
70          {
71              generateDependenciesTable( sink, analysis.getUsedDeclaredArtifacts().iterator() );
72          }
73          sink.section2_();
74  
75          // Generate Used Undeclared dependencies:
76          sink.section2();
77          sink.sectionTitle2();
78          sink.text( bundle.getString( "analyze.report.UsedUndeclaredDependencies" ) );
79          sink.sectionTitle2_();
80          if ( analysis.getUsedUndeclaredArtifacts().isEmpty() )
81          {
82              sink.paragraph();
83              sink.text( bundle.getString( "analyze.report.noDependency" ) );
84              sink.paragraph_();
85              sink.horizontalRule();
86          }
87          else
88          {
89              generateDependenciesTable( sink, analysis.getUsedUndeclaredArtifacts().iterator() );
90          }
91          sink.section2_();
92  
93          // Generate Unused declared dependencies:
94          sink.section2();
95          sink.sectionTitle2();
96          sink.text( bundle.getString( "analyze.report.UnusedDeclaredDependencies" ) );
97          sink.sectionTitle2_();
98          if ( analysis.getUnusedDeclaredArtifacts().isEmpty() )
99          {
100             sink.paragraph();
101             sink.text( bundle.getString( "analyze.report.noDependency" ) );
102             sink.paragraph_();
103             sink.horizontalRule();
104         }
105         else
106         {
107             generateDependenciesTable( sink, analysis.getUnusedDeclaredArtifacts().iterator() );
108         }
109         sink.section2_();
110 
111         // Generate Non-Test Scoped Test Dependencies:
112         sink.section2();
113         sink.sectionTitle2();
114         sink.text( "Compile Scoped Test Dependencies" );
115         sink.sectionTitle2_();
116         if ( analysis.getTestArtifactsWithNonTestScope().isEmpty() )
117         {
118             sink.paragraph();
119             sink.text( "None" );
120             sink.paragraph_();
121             sink.horizontalRule();
122         }
123         else
124         {
125             generateDependenciesTable( sink, analysis.getTestArtifactsWithNonTestScope().iterator() );
126         }
127         sink.section2_();
128 
129         sink.section1_();
130 
131         // Closing the report
132         sink.body_();
133         sink.flush();
134         sink.close();
135     }
136 
137     /**
138      * Generate a table for the given dependencies iterator.
139      * 
140      * @param sink {@link Sink}
141      * @param iter {@link Artifact}
142      */
143     public void generateDependenciesTable( Sink sink, Iterator<Artifact> iter )
144     {
145         sink.table();
146 
147         sink.tableRow();
148         sink.tableCell();
149         sink.bold();
150         sink.text( "GroupId" );
151         sink.bold_();
152         sink.tableCell_();
153 
154         sink.tableCell();
155         sink.bold();
156         sink.text( "ArtifactId" );
157         sink.bold_();
158         sink.tableCell_();
159 
160         sink.tableCell();
161         sink.bold();
162         sink.text( "Version" );
163         sink.bold_();
164         sink.tableCell_();
165 
166         sink.tableCell();
167         sink.bold();
168         sink.text( "Scope" );
169         sink.bold_();
170         sink.tableCell_();
171 
172         sink.tableCell();
173         sink.bold();
174         sink.text( "Classifier" );
175         sink.bold_();
176         sink.tableCell_();
177 
178         sink.tableCell();
179         sink.bold();
180         sink.text( "Type" );
181         sink.bold_();
182         sink.tableCell_();
183 
184         sink.tableCell();
185         sink.bold();
186         sink.text( "Optional" );
187         sink.bold_();
188         sink.tableCell_();
189 
190         sink.tableRow_();
191         while ( iter.hasNext() )
192         {
193             Artifact artifact = iter.next();
194 
195             sink.tableRow();
196             sink.tableCell();
197             sink.text( artifact.getGroupId() );
198             sink.tableCell_();
199             sink.tableCell();
200             sink.text( artifact.getArtifactId() );
201             sink.tableCell_();
202             sink.tableCell();
203             sink.text( artifact.getVersion() );
204             sink.tableCell_();
205             sink.tableCell();
206             sink.text( artifact.getScope() );
207             sink.tableCell_();
208             sink.tableCell();
209             sink.text( artifact.getClassifier() );
210             sink.tableCell_();
211             sink.tableCell();
212             sink.text( artifact.getType() );
213             sink.tableCell_();
214             sink.tableCell();
215             if ( artifact.isOptional() )
216             {
217                 sink.text( "" );
218             }
219             else
220             {
221                 sink.text( "false" );
222             }
223 
224             sink.tableCell_();
225             sink.tableRow_();
226         }
227 
228         sink.table_();
229         sink.horizontalRule();
230     }
231 }