View Javadoc

1   package org.apache.maven.plugin.dependency;
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.java 1400739 2012-10-21 23:05:22Z hboutemy $
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      @Component
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      // Mojo methods -----------------------------------------------------------
98  
99      /*
100      * @see org.apache.maven.plugin.Mojo#execute()
101      */
102     public void executeReport( Locale locale )
103         throws MavenReportException
104     {
105         // Step 0: Checking pom availability
106         if ( "pom".equals( project.getPackaging() ) )
107         {
108             getLog().info( "Skipping pom project" );
109             return;
110         }
111 
112         if ( outputDirectory == null || !outputDirectory.exists() )
113         {
114             getLog().info( "Skipping project with no Target directory" );
115             return;
116         }
117 
118         // Step 1: Analyze the project
119         ProjectDependencyAnalysis analysis = null;
120         try
121         {
122             analysis = analyzer.analyze( project );
123 
124             if ( usedDependencies != null )
125             {
126                 analysis = analysis.forceDeclaredDependenciesUsage( usedDependencies );
127             }
128         }
129         catch ( ProjectDependencyAnalyzerException exception )
130         {
131             throw new MavenReportException( "Cannot analyze dependencies", exception );
132         }
133 
134         //remove everything that's not in the compile scope
135         if ( ignoreNonCompile )
136         {
137             analysis = analysis.ignoreNonCompile();
138         }
139 
140         // Step 2: Create sink and bundle
141         Sink sink = getSink();
142         ResourceBundle bundle = getBundle( locale );
143 
144         // Step 3: Generate the report
145         AnalyzeReportView analyzethis = new AnalyzeReportView();
146         analyzethis.generateReport( analysis, sink, bundle );
147     }
148 
149     // MavenReport methods ----------------------------------------------------
150 
151     /*
152      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
153      */
154     public String getOutputName()
155     {
156         return "dependency-analysis";
157     }
158 
159     /*
160      * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
161      */
162     public String getName( Locale locale )
163     {
164         return getBundle( locale ).getString( "analyze.report.name" );
165     }
166 
167     /*
168      * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
169      */
170     public String getDescription( Locale locale )
171     {
172         return getBundle( locale ).getString( "analyze.report.description" );
173     }
174 
175     // AbstractMavenReport methods --------------------------------------------
176 
177     /*
178      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
179      */
180     protected MavenProject getProject()
181     {
182         return project;
183     }
184 
185     /*
186      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
187      */
188     protected String getOutputDirectory()
189     {
190         getLog().info( outputDirectory.toString() );
191 
192         return outputDirectory.toString();
193     }
194 
195     /*
196      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
197      */
198     protected Renderer getSiteRenderer()
199     {
200         return siteRenderer;
201     }
202 
203     // protected methods ------------------------------------------------------
204 
205     /**
206      * @param locale the current locale
207      */
208     protected ResourceBundle getBundle( Locale locale )
209     {
210         return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
211     }
212 }