View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.dependency.analyze;
20  
21  import java.util.Collection;
22  import java.util.Locale;
23  
24  import org.apache.maven.artifact.Artifact;
25  import org.apache.maven.doxia.sink.Sink;
26  import org.apache.maven.reporting.AbstractMavenReportRenderer;
27  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
28  import org.codehaus.plexus.i18n.I18N;
29  
30  /**
31   * This is the view part of the analyze-report mojo. It generates the HTML report for the project website. The HTML
32   * output is same as the CLI output.
33   */
34  public class AnalyzeReportRenderer extends AbstractMavenReportRenderer {
35      private final I18N i18n;
36  
37      private final Locale locale;
38  
39      private final ProjectDependencyAnalysis analysis;
40  
41      public AnalyzeReportRenderer(Sink sink, I18N i18n, Locale locale, ProjectDependencyAnalysis analysis) {
42          super(sink);
43          this.i18n = i18n;
44          this.locale = locale;
45          this.analysis = analysis;
46      }
47  
48      @Override
49      public String getTitle() {
50          return getI18nString("title");
51      }
52  
53      /**
54       * @param key The key.
55       * @return The translated string.
56       */
57      private String getI18nString(String key) {
58          return i18n.getString("analyze-report", locale, "report.analyze." + key);
59      }
60  
61      protected void renderBody() {
62          startSection(getTitle());
63  
64          boolean reported = false;
65  
66          // Generate Used Declared dependencies:
67          if (!analysis.getUsedDeclaredArtifacts().isEmpty()) {
68              startSection(getI18nString("UsedDeclaredDependencies"));
69              renderDependenciesTable(sink, analysis.getUsedDeclaredArtifacts());
70              endSection();
71              reported = true;
72          }
73  
74          // Generate Used Undeclared dependencies:
75  
76          if (!analysis.getUsedUndeclaredArtifacts().isEmpty()) {
77              startSection(getI18nString("UsedUndeclaredDependencies"));
78              renderDependenciesTable(sink, analysis.getUsedUndeclaredArtifacts());
79              endSection();
80              reported = true;
81          }
82  
83          // Generate Unused declared dependencies:
84          if (!analysis.getUnusedDeclaredArtifacts().isEmpty()) {
85              startSection(getI18nString("UnusedDeclaredDependencies"));
86              renderDependenciesTable(sink, analysis.getUnusedDeclaredArtifacts());
87              endSection();
88              reported = true;
89          }
90  
91          // Generate Non-Test Scoped Test Dependencies:
92          if (!analysis.getTestArtifactsWithNonTestScope().isEmpty()) {
93              startSection(getI18nString("CompileScopeTestOnlyDependencies"));
94              renderDependenciesTable(sink, analysis.getTestArtifactsWithNonTestScope());
95              endSection();
96              reported = true;
97          }
98  
99          if (!reported) {
100             text(getI18nString("noDependencyProblems"));
101         }
102 
103         endSection();
104     }
105 
106     private void renderDependenciesTable(Sink sink, Collection<Artifact> artifacts) {
107         startTable();
108 
109         tableHeader(new String[] {"GroupId", "ArtifactId", "Version", "Scope", "Classifier", "Type", "Optional"});
110 
111         for (Artifact artifact : artifacts) {
112             tableRow(new String[] {
113                 artifact.getGroupId(),
114                 artifact.getArtifactId(),
115                 artifact.getVersion(),
116                 artifact.getScope(),
117                 artifact.getClassifier(),
118                 artifact.getType(),
119                 artifact.isOptional() ? "" : "false"
120             });
121         }
122 
123         endTable();
124     }
125 }