Coverage Report - org.apache.maven.plugin.dependency.AbstractAnalyzeMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAnalyzeMojo
0%
0/92
0%
0/56
6.2
 
 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.io.StringWriter;
 24  
 import java.util.HashSet;
 25  
 import java.util.Iterator;
 26  
 import java.util.Set;
 27  
 
 28  
 import org.apache.maven.artifact.Artifact;
 29  
 import org.apache.maven.plugin.AbstractMojo;
 30  
 import org.apache.maven.plugin.MojoExecutionException;
 31  
 import org.apache.maven.plugin.MojoFailureException;
 32  
 import org.apache.maven.project.MavenProject;
 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  
 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
 37  
 
 38  
 /**
 39  
  * Analyzes the dependencies of this project and determines which are: used and declared; used and undeclared; unused
 40  
  * and declared.
 41  
  * 
 42  
  * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
 43  
  * @version $Id: AbstractAnalyzeMojo.java 728546 2008-12-21 22:56:51Z bentmann $
 44  
  * @since 2.0-alpha-5
 45  
  */
 46  0
 public abstract class AbstractAnalyzeMojo
 47  
     extends AbstractMojo
 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  
      * Whether to fail the build if a dependency warning is found.
 71  
      * 
 72  
      * @parameter expression="${failOnWarning}" default-value="false"
 73  
      */
 74  
     private boolean failOnWarning;
 75  
 
 76  
     /**
 77  
      * Output used dependencies
 78  
      * 
 79  
      * @parameter expression="${verbose}" default-value="false"
 80  
      */
 81  
     private boolean verbose;
 82  
 
 83  
     /**
 84  
      * Ignore Runtime,Provide,Test,System scopes for unused dependency analysis
 85  
      * 
 86  
      * @parameter expression="${ignoreNonCompile}" default-value="false"
 87  
      */
 88  
     private boolean ignoreNonCompile;
 89  
 
 90  
     /**
 91  
      * Output the xml for the missing dependencies
 92  
      * 
 93  
      * @parameter expression="${outputXML}" default-value="false"
 94  
      * @since 2.0-alpha-5
 95  
      */
 96  
     private boolean outputXML;
 97  
 
 98  
     /**
 99  
      * Output scriptable values
 100  
      * 
 101  
      * @parameter expression="${scriptableOutput}" default-value="false"
 102  
      * @since 2.0-alpha-5
 103  
      */
 104  
     private boolean scriptableOutput;
 105  
 
 106  
     /**
 107  
      * Flag to use for scriptable output
 108  
      * 
 109  
      * @parameter expression="${scriptableFlag}" default-value="$$$%%%"
 110  
      * @since 2.0-alpha-5
 111  
      */
 112  
     private String scriptableFlag;
 113  
 
 114  
     /**
 115  
      * Flag to use for scriptable output
 116  
      * 
 117  
      * @parameter expression="${basedir}"
 118  
      * @readonly
 119  
      * @since 2.0-alpha-5
 120  
      */
 121  
     private File baseDir;
 122  
 
 123  
     /**
 124  
      * Target folder
 125  
      * 
 126  
      * @parameter expression="${project.build.directory}"
 127  
      * @readonly
 128  
      * @since 2.0-alpha-5
 129  
      */
 130  
     private File outputDirectory;
 131  
 
 132  
     // Mojo methods -----------------------------------------------------------
 133  
 
 134  
     /*
 135  
      * @see org.apache.maven.plugin.Mojo#execute()
 136  
      */
 137  
     public void execute()
 138  
         throws MojoExecutionException, MojoFailureException
 139  
     {
 140  0
         if ( "pom".equals( project.getPackaging() ) )
 141  
         {
 142  0
             getLog().info( "Skipping pom project" );
 143  0
             return;
 144  
         }
 145  
         
 146  0
         if ( outputDirectory == null || !outputDirectory.exists())
 147  
         {
 148  0
             getLog().info( "Skipping project with no build directory" );
 149  0
             return;
 150  
         }
 151  
 
 152  0
         boolean warning = checkDependencies();
 153  
 
 154  0
         if ( warning && failOnWarning )
 155  
         {
 156  0
             throw new MojoExecutionException( "Dependency problems found" );
 157  
         }
 158  0
     }
 159  
 
 160  
     // private methods --------------------------------------------------------
 161  
 
 162  
     private boolean checkDependencies()
 163  
         throws MojoExecutionException
 164  
     {
 165  
         ProjectDependencyAnalysis analysis;
 166  
         try
 167  
         {
 168  0
             analysis = analyzer.analyze( project );
 169  
         }
 170  0
         catch ( ProjectDependencyAnalyzerException exception )
 171  
         {
 172  0
             throw new MojoExecutionException( "Cannot analyze dependencies", exception );
 173  0
         }
 174  
 
 175  0
         Set usedDeclared = analysis.getUsedDeclaredArtifacts();
 176  0
         Set usedUndeclared = analysis.getUsedUndeclaredArtifacts();
 177  0
         Set unusedDeclared = analysis.getUnusedDeclaredArtifacts();
 178  
         
 179  0
         if ( ignoreNonCompile )
 180  
         {
 181  0
             Set filteredUnusedDeclared = new HashSet( unusedDeclared );
 182  0
             Iterator iter = filteredUnusedDeclared.iterator();
 183  0
             while ( iter.hasNext() )
 184  
             {
 185  0
                 Artifact artifact = (Artifact) iter.next();
 186  0
                 if ( !artifact.getScope().equals( Artifact.SCOPE_COMPILE ) )
 187  
                 {
 188  0
                     iter.remove();
 189  
                 }
 190  0
             }
 191  0
             unusedDeclared = filteredUnusedDeclared;
 192  
         }
 193  
 
 194  0
         if ( ( !verbose || usedDeclared.isEmpty() ) && usedUndeclared.isEmpty() && unusedDeclared.isEmpty() )
 195  
         {
 196  0
             getLog().info( "No dependency problems found" );
 197  0
             return false;
 198  
         }
 199  
         
 200  0
         if ( verbose && !usedDeclared.isEmpty() )
 201  
         {
 202  0
             getLog().info( "Used declared dependencies found:" );
 203  
 
 204  0
             logArtifacts( analysis.getUsedDeclaredArtifacts(), false );
 205  
         }
 206  
         
 207  0
         if ( !usedUndeclared.isEmpty() )
 208  
         {
 209  0
             getLog().warn( "Used undeclared dependencies found:" );
 210  
 
 211  0
             logArtifacts( usedUndeclared, true );
 212  
         }
 213  
 
 214  0
         if ( !unusedDeclared.isEmpty() )
 215  
         {
 216  0
             getLog().warn( "Unused declared dependencies found:" );
 217  
             
 218  0
             logArtifacts( unusedDeclared, true );
 219  
         }
 220  
 
 221  0
         if ( outputXML )
 222  
         {
 223  0
             writeDependencyXML( usedUndeclared );
 224  
         }
 225  
         
 226  0
         if ( scriptableOutput )
 227  
         {
 228  0
             writeScriptableOutput( usedUndeclared );
 229  
         }
 230  
 
 231  0
         return !usedUndeclared.isEmpty() || !unusedDeclared.isEmpty();
 232  
     }
 233  
 
 234  
     private void logArtifacts( Set artifacts, boolean warn )
 235  
     {
 236  0
         if ( artifacts.isEmpty() )
 237  
         {
 238  0
             getLog().info( "   None" );
 239  
         }
 240  
         else
 241  
         {
 242  0
             for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); )
 243  
             {
 244  0
                 Artifact artifact = (Artifact) iterator.next();
 245  
 
 246  
                 // called because artifact will set the version to -SNAPSHOT only if I do this. MNG-2961
 247  0
                 artifact.isSnapshot();
 248  
 
 249  0
                 if ( warn )
 250  
                 {
 251  0
                     getLog().warn( "   " + artifact );
 252  
                 }
 253  
                 else
 254  
                 {
 255  0
                     getLog().info( "   " + artifact );
 256  
                 }
 257  
 
 258  0
             }
 259  
         }
 260  0
     }
 261  
 
 262  
     private void writeDependencyXML( Set artifacts )
 263  
     {
 264  0
         if ( !artifacts.isEmpty() )
 265  
         {
 266  0
             getLog().info( "Add the following to your pom to correct the missing dependencies: " );
 267  
 
 268  0
             StringWriter out = new StringWriter();
 269  0
             PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( out );
 270  
 
 271  0
             Iterator iter = artifacts.iterator();
 272  0
             while ( iter.hasNext() )
 273  
             {
 274  0
                 Artifact artifact = (Artifact) iter.next();
 275  
 
 276  
                 // called because artifact will set the version to -SNAPSHOT only if I do this. MNG-2961
 277  0
                 artifact.isSnapshot();
 278  
 
 279  0
                 writer.startElement( "dependency" );
 280  0
                 writer.startElement( "groupId" );
 281  0
                 writer.writeText( artifact.getGroupId() );
 282  0
                 writer.endElement();
 283  0
                 writer.startElement( "artifactId" );
 284  0
                 writer.writeText( artifact.getArtifactId() );
 285  0
                 writer.endElement();
 286  0
                 writer.startElement( "version" );
 287  0
                 writer.writeText( artifact.getBaseVersion() );
 288  0
                 writer.endElement();
 289  
 
 290  0
                 if ( !Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) )
 291  
                 {
 292  0
                     writer.startElement( "scope" );
 293  0
                     writer.writeText( artifact.getScope() );
 294  0
                     writer.endElement();
 295  
                 }
 296  0
                 writer.endElement();
 297  0
             }
 298  
 
 299  0
             getLog().info( "\n" + out.getBuffer() );
 300  
         }
 301  0
     }
 302  
     
 303  
     private void writeScriptableOutput( Set artifacts )
 304  
     {
 305  0
         if ( !artifacts.isEmpty() )
 306  
         {
 307  0
             getLog().info( "Missing dependencies: " );
 308  0
             String pomFile = baseDir.getAbsolutePath() + File.separatorChar + "pom.xml";
 309  0
             StringBuffer buf = new StringBuffer();
 310  0
             Iterator iter = artifacts.iterator();
 311  0
             while ( iter.hasNext() )
 312  
             {
 313  0
                 Artifact artifact = (Artifact) iter.next();
 314  
 
 315  
                 // called because artifact will set the version to -SNAPSHOT only if I do this. MNG-2961
 316  0
                 artifact.isSnapshot();
 317  
 
 318  0
                 buf.append( scriptableFlag + ":" + pomFile + ":" + artifact.getDependencyConflictId() + ":"
 319  
                                 + artifact.getClassifier() + ":" + artifact.getBaseVersion() + ":"
 320  
                                 + artifact.getScope() + "\n" );
 321  0
             }
 322  0
             getLog().info( "\n" + buf );
 323  
         }
 324  0
     }
 325  
 }