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.Locale;
22  
23  import org.apache.maven.plugins.annotations.Component;
24  import org.apache.maven.plugins.annotations.Execute;
25  import org.apache.maven.plugins.annotations.LifecyclePhase;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.plugins.annotations.ResolutionScope;
29  import org.apache.maven.reporting.AbstractMavenReport;
30  import org.apache.maven.reporting.MavenReportException;
31  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
32  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
33  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
34  import org.codehaus.plexus.i18n.I18N;
35  
36  /**
37   * Analyzes the dependencies of this project and produces a report that summarizes which are: used and declared; used
38   * and undeclared; unused and declared.
39   *
40   * @since 2.0-alpha-5
41   */
42  @Mojo(name = "analyze-report", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true)
43  @Execute(phase = LifecyclePhase.TEST_COMPILE)
44  public class AnalyzeReportMojo extends AbstractMavenReport {
45      // fields -----------------------------------------------------------------
46  
47      /**
48       * The Maven project dependency analyzer to use.
49       */
50      @Component
51      private ProjectDependencyAnalyzer analyzer;
52  
53      /**
54       * Ignore Runtime/Provided/Test/System scopes for unused dependency analysis
55       *
56       * @since 2.2
57       */
58      @Parameter(property = "ignoreNonCompile", defaultValue = "false")
59      private boolean ignoreNonCompile;
60  
61      /**
62       * Force dependencies as used, to override incomplete result caused by bytecode-level analysis. Dependency format is
63       * <code>groupId:artifactId</code>.
64       *
65       * @since 2.6
66       */
67      @Parameter
68      private String[] usedDependencies;
69  
70      /**
71       * Skip plugin execution completely.
72       *
73       * @since 2.7
74       */
75      @Parameter(property = "mdep.analyze.skip", defaultValue = "false")
76      private boolean skip;
77  
78      /**
79       * Internationalization component
80       */
81      @Component
82      private I18N i18n;
83  
84      // Mojo methods -----------------------------------------------------------
85  
86      /*
87       * @see org.apache.maven.plugin.Mojo#execute()
88       */
89      @Override
90      public void executeReport(Locale locale) throws MavenReportException {
91          // Step 1: Analyze the project
92          ProjectDependencyAnalysis analysis;
93          try {
94              analysis = analyzer.analyze(project);
95  
96              if (usedDependencies != null) {
97                  analysis = analysis.forceDeclaredDependenciesUsage(usedDependencies);
98              }
99          } catch (ProjectDependencyAnalyzerException exception) {
100             throw new MavenReportException("Cannot analyze dependencies", exception);
101         }
102 
103         // remove everything that's not in the compile scope
104         if (ignoreNonCompile) {
105             analysis = analysis.ignoreNonCompile();
106         }
107 
108         // Step 3: Generate the report
109         AnalyzeReportRenderer r = new AnalyzeReportRenderer(getSink(), i18n, locale, analysis);
110         r.render();
111     }
112 
113     // MavenReport methods ----------------------------------------------------
114 
115     @Override
116     public boolean canGenerateReport() {
117         if (skip) {
118             getLog().info("Skipping plugin execution");
119             return false;
120         }
121 
122         // Step 0: Checking pom availability
123         if ("pom".equals(project.getPackaging())) {
124             getLog().info("Skipping pom project");
125             return false;
126         }
127 
128         return true;
129     }
130 
131     /** {@inheritDoc} */
132     @Override
133     public String getOutputName() {
134         return "dependency-analysis";
135     }
136 
137     /** {@inheritDoc} */
138     public String getName(Locale locale) {
139         return getI18nString(locale, "name");
140     }
141 
142     /** {@inheritDoc} */
143     public String getDescription(Locale locale) {
144         return getI18nString(locale, "description");
145     }
146 
147     // protected methods ------------------------------------------------------
148 
149     /**
150      * @param locale The locale
151      * @param key The key to search for
152      * @return The text appropriate for the locale.
153      */
154     protected String getI18nString(Locale locale, String key) {
155         return i18n.getString("analyze-report", locale, "report.analyze." + key);
156     }
157 }