View Javadoc

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  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          MavenXpp3Reader pomReader = new MavenXpp3Reader();
66          Model model = null;
67          Reader reader = null;
68          try
69          {
70              reader = ReaderFactory.newXmlReader( project.getFile() );
71              model = pomReader.read( reader );
72          }
73          catch ( Exception e )
74          {
75              throw new MojoExecutionException( "IOException: " + e.getMessage(), e );
76          }
77          finally
78          {
79              IOUtil.close( reader );
80          }
81  
82          Set<String> duplicateDependencies = new HashSet<String>();
83          if ( model.getDependencies() != null )
84          {
85              duplicateDependencies = findDuplicateDependencies( model.getDependencies() );
86          }
87  
88          Set<String> duplicateDependenciesManagement = new HashSet<String>();
89          if ( model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null )
90          {
91              duplicateDependenciesManagement =
92                  findDuplicateDependencies( model.getDependencyManagement().getDependencies() );
93          }
94  
95          if ( getLog().isInfoEnabled() )
96          {
97              StringBuffer sb = new StringBuffer();
98  
99              if ( !duplicateDependencies.isEmpty() )
100             {
101                 sb.append( "List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n" );
102                 for ( Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); )
103                 {
104                     String dup = it.next();
105 
106                     sb.append( "\to " + dup );
107                     if ( it.hasNext() )
108                     {
109                         sb.append( "\n" );
110                     }
111                 }
112             }
113 
114             if ( !duplicateDependenciesManagement.isEmpty() )
115             {
116                 if ( sb.length() > 0 )
117                 {
118                     sb.append( "\n" );
119                 }
120                 sb.append(
121                     "List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml:\n" );
122                 for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); )
123                 {
124                     String dup = it.next();
125 
126                     sb.append( "\to " + dup );
127                     if ( it.hasNext() )
128                     {
129                         sb.append( "\n" );
130                     }
131                 }
132             }
133 
134             if ( sb.length() > 0 )
135             {
136                 getLog().info( sb.toString() );
137             }
138             else
139             {
140                 getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" );
141             }
142         }
143     }
144 
145     private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies )
146     {
147         List<String> modelDependencies2 = new ArrayList<String>();
148         for ( Dependency dep : modelDependencies )
149         {
150             modelDependencies2.add( dep.getManagementKey() );
151         }
152 
153         return new HashSet<String>(
154             CollectionUtils.disjunction( modelDependencies2, new HashSet<String>( modelDependencies2 ) ) );
155     }
156 }