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 937155 2015-01-21 21:53:50Z 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( "List of duplicate dependencies defined in <dependencyManagement/> in your pom.xml:\n" );
135                 for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); )
136                 {
137                     String dup = it.next();
138 
139                     sb.append( "\to " ).append( dup );
140                     if ( it.hasNext() )
141                     {
142                         sb.append( "\n" );
143                     }
144                 }
145             }
146 
147             if ( sb.length() > 0 )
148             {
149                 getLog().info( sb.toString() );
150             }
151             else
152             {
153                 getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" );
154             }
155         }
156     }
157 
158     private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies )
159     {
160         List<String> modelDependencies2 = new ArrayList<String>();
161         for ( Dependency dep : modelDependencies )
162         {
163             modelDependencies2.add( dep.getManagementKey() );
164         }
165 
166         return new HashSet<String>(
167             CollectionUtils.disjunction( modelDependencies2, new HashSet<String>( modelDependencies2 ) ) );
168     }
169 
170     public boolean isSkip()
171     {
172         return skip;
173     }
174 
175     public void setSkip( boolean skip )
176     {
177         this.skip = skip;
178     }
179 }