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 java.io.File;
23  import java.util.Locale;
24  import java.util.ResourceBundle;
25  
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.doxia.siterenderer.Renderer;
28  import org.apache.maven.project.MavenProject;
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  /**
36   * Analyzes the dependencies of this project and produces a report that summarises which are: used and declared; used
37   * and undeclared; unused and declared.
38   * 
39   * @version $Id: AnalyzeReportMojo.java 728546 2008-12-21 22:56:51Z bentmann $
40   * @since 2.0-alpha-5
41   * 
42   * @goal analyze-report
43   * @requiresDependencyResolution test
44   * @execute phase="test-compile"
45   */
46  public class AnalyzeReportMojo
47      extends AbstractMavenReport
48  {
49      // fields -----------------------------------------------------------------
50  
51      /**
52       * The Maven project to analyze.
53       * 
54       * @parameter expression="${project}"
55       * @required
56       * @readonly
57       */
58      private MavenProject project;
59  
60      /**
61       * The Maven project dependency analyzer to use.
62       * 
63       * @component
64       * @required
65       * @readonly
66       */
67      private ProjectDependencyAnalyzer analyzer;
68  
69      /**
70       * 
71       * 
72       * @component
73       * @required
74       * @readonly
75       */
76      private Renderer siteRenderer;
77  
78      /**
79       * Target folder
80       * 
81       * @parameter expression="${project.build.directory}"
82       * @readonly
83       * @since 2.0-alpha-5
84       */
85      private File outputDirectory;
86  
87      // Mojo methods -----------------------------------------------------------
88  
89      /*
90       * @see org.apache.maven.plugin.Mojo#execute()
91       */
92      public void executeReport( Locale locale ) throws MavenReportException
93      {
94          // Step 0: Checking pom availability
95          if ( "pom".equals( project.getPackaging() ) )
96          {
97              getLog().info( "Skipping pom project" );
98              return;
99          }
100         
101         if ( outputDirectory == null || !outputDirectory.exists() )
102         {
103             getLog().info( "Skipping project with no Target directory" );
104             return;
105         }
106 
107         // Step 1: Analyse the project
108         ProjectDependencyAnalysis analysis = null;
109         try
110         {
111             analysis = analyzer.analyze( project );
112         }
113         catch ( ProjectDependencyAnalyzerException exception )
114         {
115             throw new MavenReportException( "Cannot analyze dependencies", exception );
116         }
117 
118         // Step 2: Create sink and bundle
119         Sink sink = getSink();
120         ResourceBundle bundle = getBundle( locale );
121 
122         // Step 3: Generate the report
123         AnalyzeReportView analyzethis = new AnalyzeReportView();
124         analyzethis.generateReport( analysis, sink, bundle );
125     }
126     
127     // MavenReport methods ----------------------------------------------------
128 
129     /*
130      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputName()
131      */
132     public String getOutputName()
133     {
134         return "dependency-analysis";
135     }
136 
137     /*
138      * @see org.apache.maven.reporting.AbstractMavenReport#getName(java.util.Locale)
139      */
140     public String getName( Locale locale )
141     {
142         return getBundle( locale ).getString( "analyze.report.name" );
143     }
144 
145     /*
146      * @see org.apache.maven.reporting.AbstractMavenReport#getDescription(java.util.Locale)
147      */
148     public String getDescription( Locale locale )
149     {
150         return getBundle( locale ).getString( "analyze.report.description" );
151     }
152     
153     // AbstractMavenReport methods --------------------------------------------
154 
155     /*
156      * @see org.apache.maven.reporting.AbstractMavenReport#getProject()
157      */
158     protected MavenProject getProject()
159     {
160         return project;
161     }
162 
163     /*
164      * @see org.apache.maven.reporting.AbstractMavenReport#getOutputDirectory()
165      */
166     protected String getOutputDirectory()
167     {
168         getLog().info( outputDirectory.toString() );
169 
170         return outputDirectory.toString();
171     }
172 
173     /*
174      * @see org.apache.maven.reporting.AbstractMavenReport#getSiteRenderer()
175      */
176     protected Renderer getSiteRenderer()
177     {
178         return siteRenderer;
179     }
180     
181     // protected methods ------------------------------------------------------
182 
183     /**
184      * 
185      * 
186      * @param locale
187      *            the current locale
188      */
189     protected ResourceBundle getBundle( Locale locale )
190     {
191         return ResourceBundle.getBundle( "analyze-report", locale, this.getClass().getClassLoader() );
192     }
193 }