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