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