View Javadoc
1   package org.apache.maven.plugin.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.HashSet;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Set;
28  
29  import org.apache.commons.collections.CollectionUtils;
30  import org.apache.maven.model.Dependency;
31  import org.apache.maven.model.Model;
32  import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
33  import org.apache.maven.plugin.AbstractMojo;
34  import org.apache.maven.plugin.MojoExecutionException;
35  import org.apache.maven.plugin.MojoFailureException;
36  import org.apache.maven.plugins.annotations.Mojo;
37  import org.apache.maven.plugins.annotations.Parameter;
38  import org.apache.maven.project.MavenProject;
39  import org.codehaus.plexus.util.IOUtil;
40  import org.codehaus.plexus.util.ReaderFactory;
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.html 922735 2014-09-18 19:32:48Z khmarbaise $
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      public void execute()
71          throws MojoExecutionException, MojoFailureException
72      {
73          if ( isSkip() )
74          {
75              getLog().info( "Skipping plugin execution" );
76              return;
77          }
78  
79          MavenXpp3Reader pomReader = new MavenXpp3Reader();
80          Model model = null;
81          Reader reader = null;
82          try
83          {
84              reader = ReaderFactory.newXmlReader( project.getFile() );
85              model = pomReader.read( reader );
86          }
87          catch ( Exception e )
88          {
89              throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
90          }
91          finally
92          {
93              IOUtil.close( reader );
94          }
95  
96          Set<String> duplicateDependencies = new HashSet<String>();
97          if ( model.getDependencies() != null )
98          {
99              duplicateDependencies = findDuplicateDependencies( model.getDependencies() );
100         }
101 
102         Set<String> duplicateDependenciesManagement = new HashSet<String>();
103         if ( model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null )
104         {
105             duplicateDependenciesManagement =
106                 findDuplicateDependencies( model.getDependencyManagement().getDependencies() );
107         }
108 
109         if ( getLog().isInfoEnabled() )
110         {
111             StringBuilder sb = new StringBuilder();
112 
113             if ( !duplicateDependencies.isEmpty() )
114             {
115                 sb.append( "List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n" );
116                 for ( Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); )
117                 {
118                     String dup = it.next();
119 
120                     sb.append("\to ").append(dup);
121                     if ( it.hasNext() )
122                     {
123                         sb.append( "\n" );
124                     }
125                 }
126             }
127 
128             if ( !duplicateDependenciesManagement.isEmpty() )
129             {
130                 if ( sb.length() > 0 )
131                 {
132                     sb.append( "\n" );
133                 }
134                 sb.append(
135                     "List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml:\n" );
136                 for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); )
137                 {
138                     String dup = it.next();
139 
140                     sb.append("\to ").append(dup);
141                     if ( it.hasNext() )
142                     {
143                         sb.append( "\n" );
144                     }
145                 }
146             }
147 
148             if ( sb.length() > 0 )
149             {
150                 getLog().info( sb.toString() );
151             }
152             else
153             {
154                 getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" );
155             }
156         }
157     }
158 
159     private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies )
160     {
161         List<String> modelDependencies2 = new ArrayList<String>();
162         for ( Dependency dep : modelDependencies )
163         {
164             modelDependencies2.add( dep.getManagementKey() );
165         }
166 
167         return new HashSet<String>(
168             CollectionUtils.disjunction( modelDependencies2, new HashSet<String>( modelDependencies2 ) ) );
169     }
170 
171     public boolean isSkip()
172     {
173         return skip;
174     }
175 
176     public void setSkip( boolean skip )
177     {
178         this.skip = skip;
179     }
180 }