View Javadoc
1   package org.apache.maven.plugins.dependency.analyze;
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.Reader;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Set;
29  
30  import org.apache.commons.collections.CollectionUtils;
31  import org.apache.maven.model.Dependency;
32  import org.apache.maven.model.Model;
33  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
34  import org.apache.maven.plugin.AbstractMojo;
35  import org.apache.maven.plugin.MojoExecutionException;
36  import org.apache.maven.plugin.MojoFailureException;
37  import org.apache.maven.plugins.annotations.Mojo;
38  import org.apache.maven.plugins.annotations.Parameter;
39  import org.apache.maven.project.MavenProject;
40  import org.codehaus.plexus.util.IOUtil;
41  import org.codehaus.plexus.util.ReaderFactory;
42  
43  /**
44   * Analyzes the <code>&lt;dependencies/&gt;</code> and <code>&lt;dependencyManagement/&gt;</code> tags in the
45   * <code>pom.xml</code> and determines the duplicate declared dependencies.
46   *
47   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
48   */
49  @Mojo( name = "analyze-duplicate", aggregator = false, threadSafe = true )
50  public class AnalyzeDuplicateMojo
51      extends AbstractMojo
52  {
53      /**
54       * Skip plugin execution completely.
55       *
56       * @since 2.7
57       */
58      @Parameter( property = "mdep.analyze.skip", defaultValue = "false" )
59      private boolean skip;
60  
61      /**
62       * The Maven project to analyze.
63       */
64      @Parameter( defaultValue = "${project}", readonly = true, required = true )
65      private MavenProject project;
66  
67      /**
68       * {@inheritDoc}
69       */
70      @Override
71      public void execute()
72          throws MojoExecutionException, MojoFailureException
73      {
74          if ( skip )
75          {
76              getLog().info( "Skipping plugin execution" );
77              return;
78          }
79  
80          MavenXpp3Reader pomReader = new MavenXpp3Reader();
81          Model model = null;
82          Reader reader = null;
83          try
84          {
85              reader = ReaderFactory.newXmlReader( project.getFile() );
86              model = pomReader.read( reader );
87              reader.close();
88              reader = null;
89          }
90          catch ( Exception e )
91          {
92              throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
93          }
94          finally
95          {
96              IOUtil.close( reader );
97          }
98  
99          Set<String> duplicateDependencies = Collections.emptySet();
100         if ( model.getDependencies() != null )
101         {
102             duplicateDependencies = findDuplicateDependencies( model.getDependencies() );
103         }
104 
105         Set<String> duplicateDependenciesManagement = Collections.emptySet();
106         if ( model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null )
107         {
108             duplicateDependenciesManagement =
109                 findDuplicateDependencies( model.getDependencyManagement().getDependencies() );
110         }
111 
112         if ( getLog().isInfoEnabled() )
113         {
114             StringBuilder sb = new StringBuilder();
115 
116             if ( !duplicateDependencies.isEmpty() )
117             {
118                 sb.append( "List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n" );
119                 for ( Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); )
120                 {
121                     String dup = it.next();
122 
123                     sb.append( "\to " ).append( dup );
124                     if ( it.hasNext() )
125                     {
126                         sb.append( "\n" );
127                     }
128                 }
129             }
130 
131             if ( !duplicateDependenciesManagement.isEmpty() )
132             {
133                 if ( sb.length() > 0 )
134                 {
135                     sb.append( "\n" );
136                 }
137                 sb.append( "List of duplicate dependencies defined in <dependencyManagement/> in your pom.xml:\n" );
138                 for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); )
139                 {
140                     String dup = it.next();
141 
142                     sb.append( "\to " ).append( dup );
143                     if ( it.hasNext() )
144                     {
145                         sb.append( "\n" );
146                     }
147                 }
148             }
149 
150             if ( sb.length() > 0 )
151             {
152                 getLog().info( sb.toString() );
153             }
154             else
155             {
156                 getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" );
157             }
158         }
159     }
160 
161     @SuppressWarnings( "unchecked" )
162     private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies )
163     {
164         List<String> modelDependencies2 = new ArrayList<String>();
165         for ( Dependency dep : modelDependencies )
166         {
167             modelDependencies2.add( dep.getManagementKey() );
168         }
169 
170         //@formatter:off
171         return new LinkedHashSet<String>( 
172           CollectionUtils.disjunction( modelDependencies2, new LinkedHashSet<String>( modelDependencies2 ) ) 
173         );
174         //@formatter:on
175     }
176 }