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 java.io.File; |
23 | |
import java.io.StringWriter; |
24 | |
import java.util.HashSet; |
25 | |
import java.util.Iterator; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.maven.artifact.Artifact; |
29 | |
import org.apache.maven.plugin.AbstractMojo; |
30 | |
import org.apache.maven.plugin.MojoExecutionException; |
31 | |
import org.apache.maven.plugin.MojoFailureException; |
32 | |
import org.apache.maven.project.MavenProject; |
33 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalysis; |
34 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzer; |
35 | |
import org.apache.maven.shared.dependency.analyzer.ProjectDependencyAnalyzerException; |
36 | |
import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public abstract class AbstractAnalyzeMojo |
47 | |
extends AbstractMojo |
48 | |
{ |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private MavenProject project; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private ProjectDependencyAnalyzer analyzer; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
private boolean failOnWarning; |
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
private boolean verbose; |
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
private boolean ignoreNonCompile; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
private boolean outputXML; |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
private boolean scriptableOutput; |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
private String scriptableFlag; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
private File baseDir; |
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
private File outputDirectory; |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public void execute() |
138 | |
throws MojoExecutionException, MojoFailureException |
139 | |
{ |
140 | 0 | if ( "pom".equals( project.getPackaging() ) ) |
141 | |
{ |
142 | 0 | getLog().info( "Skipping pom project" ); |
143 | 0 | return; |
144 | |
} |
145 | |
|
146 | 0 | if ( outputDirectory == null || !outputDirectory.exists()) |
147 | |
{ |
148 | 0 | getLog().info( "Skipping project with no build directory" ); |
149 | 0 | return; |
150 | |
} |
151 | |
|
152 | 0 | boolean warning = checkDependencies(); |
153 | |
|
154 | 0 | if ( warning && failOnWarning ) |
155 | |
{ |
156 | 0 | throw new MojoExecutionException( "Dependency problems found" ); |
157 | |
} |
158 | 0 | } |
159 | |
|
160 | |
|
161 | |
|
162 | |
private boolean checkDependencies() |
163 | |
throws MojoExecutionException |
164 | |
{ |
165 | |
ProjectDependencyAnalysis analysis; |
166 | |
try |
167 | |
{ |
168 | 0 | analysis = analyzer.analyze( project ); |
169 | |
} |
170 | 0 | catch ( ProjectDependencyAnalyzerException exception ) |
171 | |
{ |
172 | 0 | throw new MojoExecutionException( "Cannot analyze dependencies", exception ); |
173 | 0 | } |
174 | |
|
175 | 0 | Set usedDeclared = analysis.getUsedDeclaredArtifacts(); |
176 | 0 | Set usedUndeclared = analysis.getUsedUndeclaredArtifacts(); |
177 | 0 | Set unusedDeclared = analysis.getUnusedDeclaredArtifacts(); |
178 | |
|
179 | 0 | if ( ignoreNonCompile ) |
180 | |
{ |
181 | 0 | Set filteredUnusedDeclared = new HashSet( unusedDeclared ); |
182 | 0 | Iterator iter = filteredUnusedDeclared.iterator(); |
183 | 0 | while ( iter.hasNext() ) |
184 | |
{ |
185 | 0 | Artifact artifact = (Artifact) iter.next(); |
186 | 0 | if ( !artifact.getScope().equals( Artifact.SCOPE_COMPILE ) ) |
187 | |
{ |
188 | 0 | iter.remove(); |
189 | |
} |
190 | 0 | } |
191 | 0 | unusedDeclared = filteredUnusedDeclared; |
192 | |
} |
193 | |
|
194 | 0 | if ( ( !verbose || usedDeclared.isEmpty() ) && usedUndeclared.isEmpty() && unusedDeclared.isEmpty() ) |
195 | |
{ |
196 | 0 | getLog().info( "No dependency problems found" ); |
197 | 0 | return false; |
198 | |
} |
199 | |
|
200 | 0 | if ( verbose && !usedDeclared.isEmpty() ) |
201 | |
{ |
202 | 0 | getLog().info( "Used declared dependencies found:" ); |
203 | |
|
204 | 0 | logArtifacts( analysis.getUsedDeclaredArtifacts(), false ); |
205 | |
} |
206 | |
|
207 | 0 | if ( !usedUndeclared.isEmpty() ) |
208 | |
{ |
209 | 0 | getLog().warn( "Used undeclared dependencies found:" ); |
210 | |
|
211 | 0 | logArtifacts( usedUndeclared, true ); |
212 | |
} |
213 | |
|
214 | 0 | if ( !unusedDeclared.isEmpty() ) |
215 | |
{ |
216 | 0 | getLog().warn( "Unused declared dependencies found:" ); |
217 | |
|
218 | 0 | logArtifacts( unusedDeclared, true ); |
219 | |
} |
220 | |
|
221 | 0 | if ( outputXML ) |
222 | |
{ |
223 | 0 | writeDependencyXML( usedUndeclared ); |
224 | |
} |
225 | |
|
226 | 0 | if ( scriptableOutput ) |
227 | |
{ |
228 | 0 | writeScriptableOutput( usedUndeclared ); |
229 | |
} |
230 | |
|
231 | 0 | return !usedUndeclared.isEmpty() || !unusedDeclared.isEmpty(); |
232 | |
} |
233 | |
|
234 | |
private void logArtifacts( Set artifacts, boolean warn ) |
235 | |
{ |
236 | 0 | if ( artifacts.isEmpty() ) |
237 | |
{ |
238 | 0 | getLog().info( " None" ); |
239 | |
} |
240 | |
else |
241 | |
{ |
242 | 0 | for ( Iterator iterator = artifacts.iterator(); iterator.hasNext(); ) |
243 | |
{ |
244 | 0 | Artifact artifact = (Artifact) iterator.next(); |
245 | |
|
246 | |
|
247 | 0 | artifact.isSnapshot(); |
248 | |
|
249 | 0 | if ( warn ) |
250 | |
{ |
251 | 0 | getLog().warn( " " + artifact ); |
252 | |
} |
253 | |
else |
254 | |
{ |
255 | 0 | getLog().info( " " + artifact ); |
256 | |
} |
257 | |
|
258 | 0 | } |
259 | |
} |
260 | 0 | } |
261 | |
|
262 | |
private void writeDependencyXML( Set artifacts ) |
263 | |
{ |
264 | 0 | if ( !artifacts.isEmpty() ) |
265 | |
{ |
266 | 0 | getLog().info( "Add the following to your pom to correct the missing dependencies: " ); |
267 | |
|
268 | 0 | StringWriter out = new StringWriter(); |
269 | 0 | PrettyPrintXMLWriter writer = new PrettyPrintXMLWriter( out ); |
270 | |
|
271 | 0 | Iterator iter = artifacts.iterator(); |
272 | 0 | while ( iter.hasNext() ) |
273 | |
{ |
274 | 0 | Artifact artifact = (Artifact) iter.next(); |
275 | |
|
276 | |
|
277 | 0 | artifact.isSnapshot(); |
278 | |
|
279 | 0 | writer.startElement( "dependency" ); |
280 | 0 | writer.startElement( "groupId" ); |
281 | 0 | writer.writeText( artifact.getGroupId() ); |
282 | 0 | writer.endElement(); |
283 | 0 | writer.startElement( "artifactId" ); |
284 | 0 | writer.writeText( artifact.getArtifactId() ); |
285 | 0 | writer.endElement(); |
286 | 0 | writer.startElement( "version" ); |
287 | 0 | writer.writeText( artifact.getBaseVersion() ); |
288 | 0 | writer.endElement(); |
289 | |
|
290 | 0 | if ( !Artifact.SCOPE_COMPILE.equals( artifact.getScope() ) ) |
291 | |
{ |
292 | 0 | writer.startElement( "scope" ); |
293 | 0 | writer.writeText( artifact.getScope() ); |
294 | 0 | writer.endElement(); |
295 | |
} |
296 | 0 | writer.endElement(); |
297 | 0 | } |
298 | |
|
299 | 0 | getLog().info( "\n" + out.getBuffer() ); |
300 | |
} |
301 | 0 | } |
302 | |
|
303 | |
private void writeScriptableOutput( Set artifacts ) |
304 | |
{ |
305 | 0 | if ( !artifacts.isEmpty() ) |
306 | |
{ |
307 | 0 | getLog().info( "Missing dependencies: " ); |
308 | 0 | String pomFile = baseDir.getAbsolutePath() + File.separatorChar + "pom.xml"; |
309 | 0 | StringBuffer buf = new StringBuffer(); |
310 | 0 | Iterator iter = artifacts.iterator(); |
311 | 0 | while ( iter.hasNext() ) |
312 | |
{ |
313 | 0 | Artifact artifact = (Artifact) iter.next(); |
314 | |
|
315 | |
|
316 | 0 | artifact.isSnapshot(); |
317 | |
|
318 | 0 | buf.append( scriptableFlag + ":" + pomFile + ":" + artifact.getDependencyConflictId() + ":" |
319 | |
+ artifact.getClassifier() + ":" + artifact.getBaseVersion() + ":" |
320 | |
+ artifact.getScope() + "\n" ); |
321 | 0 | } |
322 | 0 | getLog().info( "\n" + buf ); |
323 | |
} |
324 | 0 | } |
325 | |
} |