1 | |
package org.apache.maven.plugin.dependency; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
@Mojo( name = "analyze-duplicate", aggregator = false ) |
50 | 2 | public class AnalyzeDuplicateMojo |
51 | |
extends AbstractMojo |
52 | |
{ |
53 | |
|
54 | |
|
55 | |
|
56 | |
@Component |
57 | |
private MavenProject project; |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void execute() |
63 | |
throws MojoExecutionException, MojoFailureException |
64 | |
{ |
65 | 2 | MavenXpp3Reader pomReader = new MavenXpp3Reader(); |
66 | 2 | Model model = null; |
67 | 2 | Reader reader = null; |
68 | |
try |
69 | |
{ |
70 | 2 | reader = ReaderFactory.newXmlReader( project.getFile() ); |
71 | 2 | model = pomReader.read( reader ); |
72 | |
} |
73 | 0 | catch ( Exception e ) |
74 | |
{ |
75 | 0 | throw new MojoExecutionException( "IOException: " + e.getMessage(), e ); |
76 | |
} |
77 | |
finally |
78 | |
{ |
79 | 2 | IOUtil.close( reader ); |
80 | 2 | } |
81 | |
|
82 | 2 | Set<String> duplicateDependencies = new HashSet<String>(); |
83 | 2 | if ( model.getDependencies() != null ) |
84 | |
{ |
85 | 2 | duplicateDependencies = findDuplicateDependencies( model.getDependencies() ); |
86 | |
} |
87 | |
|
88 | 2 | Set<String> duplicateDependenciesManagement = new HashSet<String>(); |
89 | 2 | if ( model.getDependencyManagement() != null && model.getDependencyManagement().getDependencies() != null ) |
90 | |
{ |
91 | 1 | duplicateDependenciesManagement = |
92 | |
findDuplicateDependencies( model.getDependencyManagement().getDependencies() ); |
93 | |
} |
94 | |
|
95 | 2 | if ( getLog().isInfoEnabled() ) |
96 | |
{ |
97 | 2 | StringBuffer sb = new StringBuffer(); |
98 | |
|
99 | 2 | if ( !duplicateDependencies.isEmpty() ) |
100 | |
{ |
101 | 1 | sb.append( "List of duplicate dependencies defined in <dependencies/> in your pom.xml:\n" ); |
102 | 1 | for ( Iterator<String> it = duplicateDependencies.iterator(); it.hasNext(); ) |
103 | |
{ |
104 | 1 | String dup = it.next(); |
105 | |
|
106 | 1 | sb.append( "\to " + dup ); |
107 | 1 | if ( it.hasNext() ) |
108 | |
{ |
109 | 0 | sb.append( "\n" ); |
110 | |
} |
111 | 1 | } |
112 | |
} |
113 | |
|
114 | 2 | if ( !duplicateDependenciesManagement.isEmpty() ) |
115 | |
{ |
116 | 1 | if ( sb.length() > 0 ) |
117 | |
{ |
118 | 0 | sb.append( "\n" ); |
119 | |
} |
120 | 1 | sb.append( |
121 | |
"List of duplicate dependencies defined in <dependencyManagement/> in " + "your pom.xml:\n" ); |
122 | 1 | for ( Iterator<String> it = duplicateDependenciesManagement.iterator(); it.hasNext(); ) |
123 | |
{ |
124 | 1 | String dup = it.next(); |
125 | |
|
126 | 1 | sb.append( "\to " + dup ); |
127 | 1 | if ( it.hasNext() ) |
128 | |
{ |
129 | 0 | sb.append( "\n" ); |
130 | |
} |
131 | 1 | } |
132 | |
} |
133 | |
|
134 | 2 | if ( sb.length() > 0 ) |
135 | |
{ |
136 | 2 | getLog().info( sb.toString() ); |
137 | |
} |
138 | |
else |
139 | |
{ |
140 | 0 | getLog().info( "No duplicate dependencies found in <dependencies/> or in <dependencyManagement/>" ); |
141 | |
} |
142 | |
} |
143 | 2 | } |
144 | |
|
145 | |
private Set<String> findDuplicateDependencies( List<Dependency> modelDependencies ) |
146 | |
{ |
147 | 3 | List<String> modelDependencies2 = new ArrayList<String>(); |
148 | 3 | for ( Dependency dep : modelDependencies ) |
149 | |
{ |
150 | 8 | modelDependencies2.add( dep.getManagementKey() ); |
151 | |
} |
152 | |
|
153 | 3 | return new HashSet<String>( |
154 | |
CollectionUtils.disjunction( modelDependencies2, new HashSet<String>( modelDependencies2 ) ) ); |
155 | |
} |
156 | |
} |