View Javadoc
1   package org.apache.maven.plugin.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   * @version $Id: AnalyzeReportMojo.html 937155 2015-01-21 21:53:50Z khmarbaise $
46   * @since 2.0-alpha-5
47   */
48  @Mojo( name = "analyze-report", requiresDependencyResolution = ResolutionScope.TEST, threadSafe = true )
49  @Execute( phase = LifecyclePhase.TEST_COMPILE )
50  public class AnalyzeReportMojo
51      extends AbstractMavenReport
52  {
53      // fields -----------------------------------------------------------------
54  
55      /**
56       * The Maven project to analyze.
57       */
58      @Parameter( defaultValue = "${project}", readonly = true, required = true )
59      private MavenProject project;
60  
61      /**
62       * The Maven project dependency analyzer to use.
63       */
64      @Component
65      private ProjectDependencyAnalyzer analyzer;
66  
67      /**
68       *
69       */
70      @Component
71      private Renderer siteRenderer;
72  
73      /**
74       * Target folder
75       *
76       * @since 2.0-alpha-5
77       */
78      @Parameter( defaultValue = "${project.build.directory}", readonly = true )
79      private File outputDirectory;
80  
81      /**
82       * Ignore Runtime/Provided/Test/System scopes for unused dependency analysis
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.
90       * Dependency format is <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     public void executeReport( Locale locale )
111         throws MavenReportException
112     {
113         if ( isSkip() )
114         {
115             getLog().info( "Skipping plugin execution" );
116             return;
117         }
118 
119         // Step 0: Checking pom availability
120         if ( "pom".equals( project.getPackaging() ) )
121         {
122             getLog().info( "Skipping pom project" );
123             return;
124         }
125 
126         if ( outputDirectory == null || !outputDirectory.exists() )
127         {
128             getLog().info( "Skipping project with no Target directory" );
129             return;
130         }
131 
132         // Step 1: Analyze the project
133         ProjectDependencyAnalysis analysis;
134         try
135         {
136             analysis = analyzer.analyze( project );
137 
138             if ( usedDependencies != null )
139             {
140                 analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies );
141             }
142         }
143         catch ( ProjectDependencyAnalyzerException exception )
144         {
145             throw new MavenReportException( "Cannot analyze dependencies", exception );
146         }
147 
148         //remove everything that's not in the compile scope
149         if ( ignoreNonCompile )
150         {
151             analysis = analysis.ignoreNonCompile();
152         }
153 
154         // Step 2: Create sink and bundle
155         Sink sink = getSink();
156         ResourceBundle bundle = getBundle( locale );
157 
158         // Step 3: Generate the report
159         AnalyzeReportView analyzethis = new AnalyzeReportView();
160         analyzethis.generateReport( analysis, sink, bundle );
161     }
162 
163     // MavenReport methods ----------------------------------------------------
164 
165     /*
166      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
167      */
168     public String getOutputName()
169     {
170         return "dependency-analysis";
171     }
172 
173     /*
174      * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
175      */
176     public String getName( Locale locale )
177     {
178         return getBundle( locale ).getString( "analyze.report.name" );
179     }
180 
181     /*
182      * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
183      */
184     public String getDescription( Locale locale )
185     {
186         return getBundle( locale ).getString( "analyze.report.description" );
187     }
188 
189     // AbstractMavenReport methods --------------------------------------------
190 
191     /*
192      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
193      */
194     protected MavenProject getProject()
195     {
196         return project;
197     }
198 
199     /*
200      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
201      */
202     protected String getOutputDirectory()
203     {
204         getLog().info( outputDirectory.toString() );
205 
206         return outputDirectory.toString();
207     }
208 
209     /*
210      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
211      */
212     protected Renderer getSiteRenderer()
213     {
214         return siteRenderer;
215     }
216 
217     // protected methods ------------------------------------------------------
218 
219     /**
220      * @param locale the current locale
221      */
222     protected ResourceBundle getBundle( Locale locale )
223     {
224         return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
225     }
226 
227     public boolean isSkip()
228     {
229         return skip;
230     }
231 
232     public void setSkip( boolean skip )
233     {
234         this.skip = skip;
235     }
236 }