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.doxia.siterenderer.Renderer;
24  import org.apache.maven.plugins.annotations.Component;
25  import org.apache.maven.plugins.annotations.Execute;
26  import org.apache.maven.plugins.annotations.LifecyclePhase;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.plugins.annotations.ResolutionScope;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.reporting.AbstractMavenReport;
32  import org.apache.maven.reporting.MavenReportException;
33  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis;
34  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer;
35  import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException;
36  
37  import java.io.File;
38  import java.util.Locale;
39  import java.util.ResourceBundle;
40  
41  /**
42   * Analyzes the dependencies of this project and produces a report that summarizes which are: used and declared; used
43   * and undeclared; unused and declared.
44   *
45   * @since 2.0-alpha-5
46   */
47  @Mojo( name = "analyze-report", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
48  @Execute( phase = LifecyclePhase.TEST_COMPILE )
49  public class AnalyzeReportMojo
50      extends AbstractMavenReport
51  {
52      // fields -----------------------------------------------------------------
53  
54      /**
55       * The Maven project to analyze.
56       */
57      @Parameter( defaultValue = "${project}", readonly = true, required = true )
58      private MavenProject project;
59  
60      /**
61       * The Maven project dependency analyzer to use.
62       */
63      @Component
64      private ProjectDependencyAnalyzer analyzer;
65  
66      /**
67       *
68       */
69      @Component
70      private Renderer siteRenderer;
71  
72      /**
73       * Target folder
74       *
75       * @since 2.0-alpha-5
76       */
77      @Parameter( defaultValue = "${project.build.directory}", readonly = true )
78      private File outputDirectory;
79  
80      /**
81       * Ignore Runtime/Provided/Test/System scopes for unused dependency analysis
82       * 
83       * @since 2.2
84       */
85      @Parameter( property = "ignoreNonCompile", defaultValue = "false" )
86      private boolean ignoreNonCompile;
87  
88      /**
89       * Force dependencies as used, to override incomplete result caused by bytecode-level analysis. Dependency format is
90       * <code>groupId:artifactId</code>.
91       * 
92       * @since 2.6
93       */
94      @Parameter
95      private String[] usedDependencies;
96  
97      /**
98       * Skip plugin execution completely.
99       *
100      * @since 2.7
101      */
102     @Parameter( property = "mdep.analyze.skip", defaultValue = "false" )
103     private boolean skip;
104 
105     // Mojo methods -----------------------------------------------------------
106 
107     /*
108      * @see org.apache.maven.plugin.Mojo#execute()
109      */
110     @Override
111     public void executeReport( Locale locale )
112         throws MavenReportException
113     {
114         if ( skip )
115         {
116             getLog().info( "Skipping plugin execution" );
117             return;
118         }
119 
120         // Step 0: Checking pom availability
121         if ( "pom".equals( project.getPackaging() ) )
122         {
123             getLog().info( "Skipping pom project" );
124             return;
125         }
126 
127         if ( outputDirectory == null || !outputDirectory.exists() )
128         {
129             getLog().info( "Skipping project with no Target directory" );
130             return;
131         }
132 
133         // Step 1: Analyze the project
134         ProjectDependencyAnalysis analysis;
135         try
136         {
137             analysis = analyzer.analyze( project );
138 
139             if ( usedDependencies != null )
140             {
141                 analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies );
142             }
143         }
144         catch ( ProjectDependencyAnalyzerException exception )
145         {
146             throw new MavenReportException( "Cannot analyze dependencies", exception );
147         }
148 
149         // remove everything that's not in the compile scope
150         if ( ignoreNonCompile )
151         {
152             analysis = analysis.ignoreNonCompile();
153         }
154 
155         // Step 2: Create sink and bundle
156         Sink sink = getSink();
157         ResourceBundle bundle = getBundle( locale );
158 
159         // Step 3: Generate the report
160         AnalyzeReportView analyzethis = new AnalyzeReportView();
161         analyzethis.generateReport( analysis, sink, bundle );
162     }
163 
164     // MavenReport methods ----------------------------------------------------
165 
166     /*
167      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
168      */
169     @Override
170     public String getOutputName()
171     {
172         return "dependency-analysis";
173     }
174 
175     /*
176      * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
177      */
178     @Override
179     public String getName( Locale locale )
180     {
181         return getBundle( locale ).getString( "analyze.report.name" );
182     }
183 
184     /*
185      * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
186      */
187     @Override
188     public String getDescription( Locale locale )
189     {
190         return getBundle( locale ).getString( "analyze.report.description" );
191     }
192 
193     // AbstractMavenReport methods --------------------------------------------
194 
195     /*
196      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
197      */
198     @Override
199     protected MavenProject getProject()
200     {
201         return project;
202     }
203 
204     /*
205      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
206      */
207     @Override
208     protected String getOutputDirectory()
209     {
210         getLog().info( outputDirectory.toString() );
211 
212         return outputDirectory.toString();
213     }
214 
215     /*
216      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
217      */
218     @Override
219     protected Renderer getSiteRenderer()
220     {
221         return siteRenderer;
222     }
223 
224     // protected methods ------------------------------------------------------
225 
226     /**
227      * @param locale the current locale
228      * @return The resource bundle {@link ResourceBundle}
229      */
230     protected ResourceBundle getBundle( Locale locale )
231     {
232         return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
233     }
234 }