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 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.project.MavenProject;
37  import org.codehaus.plexus.util.IOUtil;
38  import org.codehaus.plexus.util.ReaderFactory;
39  
40  /**
41   * Analyzes the <code>&lt;dependencies/&gt;</code> and <code>&lt;dependencyManagement/&gt;</code> tags in the
42   * <code>pom.xml</code> and determines the duplicate declared dependencies.
43   *
44   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
45   * @version $Id: AnalyzeDuplicateMojo.java 1085777 2011-03-26 18:13:19Z hboutemy $
46   * @goal analyze-duplicate
47   * @aggregator false
48   */
49  public class AnalyzeDuplicateMojo
50      extends AbstractMojo
51  {
52      /**
53       * The Maven project to analyze.
54       *
55       * @parameter expression="${project}"
56       * @required
57       * @readonly
58       */
59      private MavenProject project;
60  
61      /** {@inheritDoc} */
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( "List of duplicate dependencies defined in <dependencyManagement/> in "
121                     + "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>( CollectionUtils.disjunction( modelDependencies2,
154                                                                  new HashSet<String>( modelDependencies2 ) ) );
155     }
156 }