Coverage Report - org.apache.maven.plugin.dependency.AnalyzeDuplicateMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AnalyzeDuplicateMojo
86 %
38/44
73 %
19/26
8,5
 
 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.commons.collections.CollectionUtils;
 23  
 import org.apache.maven.model.Dependency;
 24  
 import org.apache.maven.model.Model;
 25  
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 26  
 import org.apache.maven.plugin.AbstractMojo;
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugin.MojoFailureException;
 29  
 import org.apache.maven.plugins.annotations.Component;
 30  
 import org.apache.maven.plugins.annotations.Mojo;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.codehaus.plexus.util.IOUtil;
 33  
 import org.codehaus.plexus.util.ReaderFactory;
 34  
 
 35  
 import java.io.Reader;
 36  
 import java.util.ArrayList;
 37  
 import java.util.HashSet;
 38  
 import java.util.Iterator;
 39  
 import java.util.List;
 40  
 import java.util.Set;
 41  
 
 42  
 /**
 43  
  * Analyzes the <code>&lt;dependencies/&gt;</code> and <code>&lt;dependencyManagement/&gt;</code> tags in the
 44  
  * <code>pom.xml</code> and determines the duplicate declared dependencies.
 45  
  *
 46  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 47  
  * @version $Id: AnalyzeDuplicateMojo.java 1357251 2012-07-04 13:28:33Z olamy $
 48  
  */
 49  
 @Mojo( name = "analyze-duplicate", aggregator = false )
 50  2
 public class AnalyzeDuplicateMojo
 51  
     extends AbstractMojo
 52  
 {
 53  
     /**
 54  
      * The Maven project to analyze.
 55  
      */
 56  
     @Component
 57  
     private MavenProject project;
 58  
 
 59  
     /**
 60  
      * {@inheritDoc}
 61  
      */
 62  
     public void execute()
 63  
         throws MojoExecutionException, MojoFailureException
 64  
     {
 65  2
         MavenXpp3Reader pomReader = new MavenXpp3Reader();
 66  2
         Model model = null;
 67  2
         Reader reader = null;
 68  
         try
 69  
         {
 70  2
             reader = ReaderFactory.newXmlReader( project.getFile() );
 71  2
             model = pomReader.read( reader );
 72  
         }
 73  0
         catch ( Exception e )
 74  
         {
 75  0
             throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
 76  
         }
 77  
         finally
 78  
         {
 79  2
             IOUtil.close( reader );
 80  2
         }
 81  
 
 82  2
         Set<String> duplicateDependencies = new HashSet<String>();
 83  2
         if ( model.getDependencies() != null )
 84  
         {
 85  2
             duplicateDependencies = findDuplicateDependencies( model.getDependencies() );
 86  
         }
 87  
 
 88  2
         Set<String> duplicateDependenciesManagement = new HashSet<String>();
 89  2
         if ( model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null )
 90  
         {
 91  1
             duplicateDependenciesManagement =
 92  
                 findDuplicateDependencies( model.getDependencyManagement().getDependencies() );
 93  
         }
 94  
 
 95  2
         if ( getLog().isInfoEnabled() )
 96  
         {
 97  2
             StringBuffer sb = new StringBuffer();
 98  
 
 99  2
             if ( !duplicateDependencies.isEmpty() )
 100  
             {
 101  1
                 sb.append( "List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n" );
 102  1
                 for ( Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); )
 103  
                 {
 104  1
                     String dup = it.next();
 105  
 
 106  1
                     sb.append( "\to " + dup );
 107  1
                     if ( it.hasNext() )
 108  
                     {
 109  0
                         sb.append( "\n" );
 110  
                     }
 111  1
                 }
 112  
             }
 113  
 
 114  2
             if ( !duplicateDependenciesManagement.isEmpty() )
 115  
             {
 116  1
                 if ( sb.length() > 0 )
 117  
                 {
 118  0
                     sb.append( "\n" );
 119  
                 }
 120  1
                 sb.append(
 121  
                     "List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml:\n" );
 122  1
                 for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); )
 123  
                 {
 124  1
                     String dup = it.next();
 125  
 
 126  1
                     sb.append( "\to " + dup );
 127  1
                     if ( it.hasNext() )
 128  
                     {
 129  0
                         sb.append( "\n" );
 130  
                     }
 131  1
                 }
 132  
             }
 133  
 
 134  2
             if ( sb.length() > 0 )
 135  
             {
 136  2
                 getLog().info( sb.toString() );
 137  
             }
 138  
             else
 139  
             {
 140  0
                 getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" );
 141  
             }
 142  
         }
 143  2
     }
 144  
 
 145  
     private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies )
 146  
     {
 147  3
         List<String> modelDependencies2 = new ArrayList<String>();
 148  3
         for ( Dependency dep : modelDependencies )
 149  
         {
 150  8
             modelDependencies2.add( dep.getManagementKey() );
 151  
         }
 152  
 
 153  3
         return new HashSet<String>(
 154  
             CollectionUtils.disjunction( modelDependencies2, new HashSet<String>( modelDependencies2 ) ) );
 155  
     }
 156  
 }