1 package org.apache.maven.plugin.dependency.analyze;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
44
45
46
47
48
49 @Mojo( name = "analyze-duplicate", aggregator = false, threadSafe = true )
50 public class AnalyzeDuplicateMojo
51 extends AbstractMojo
52 {
53
54
55
56
57
58 @Parameter( property = "mdep.analyze.skip", defaultValue = "false" )
59 private boolean skip;
60
61
62
63
64 @Parameter( defaultValue = "${project}", readonly = true, required = true )
65 private MavenProject project;
66
67
68
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 }