1 package org.apache.maven.plugins.enforcer.utils;
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.IOException;
24 import java.io.Reader;
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import org.apache.maven.artifact.Artifact;
29 import org.apache.maven.artifact.factory.ArtifactFactory;
30 import org.apache.maven.artifact.repository.ArtifactRepository;
31 import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
32 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
33 import org.apache.maven.artifact.resolver.ArtifactResolver;
34 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
35 import org.apache.maven.model.Model;
36 import org.apache.maven.model.Parent;
37 import org.apache.maven.model.Plugin;
38 import org.apache.maven.model.ReportPlugin;
39 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
40 import org.apache.maven.plugin.logging.Log;
41 import org.apache.maven.project.MavenProject;
42 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
43 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
44 import org.codehaus.plexus.util.ReaderFactory;
45 import org.codehaus.plexus.util.StringUtils;
46 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
47
48
49
50
51
52
53
54 public class EnforcerRuleUtils
55 {
56
57
58 ArtifactFactory factory;
59
60
61 ArtifactResolver resolver;
62
63
64 ArtifactRepository local;
65
66
67 List<ArtifactRepository> remoteRepositories;
68
69
70 Log log;
71
72
73 MavenProject project;
74
75 private EnforcerRuleHelper helper;
76
77
78
79
80
81
82
83
84
85
86
87 public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
88 List<ArtifactRepository> theRemoteRepositories, MavenProject project, Log theLog )
89 {
90 super();
91 this.factory = theFactory;
92 this.resolver = theResolver;
93 this.local = theLocal;
94 this.remoteRepositories = theRemoteRepositories;
95 this.log = theLog;
96 this.project = project;
97 }
98
99
100
101
102
103
104 @SuppressWarnings( "unchecked" )
105 public EnforcerRuleUtils( EnforcerRuleHelper helper )
106 {
107
108 this.helper = helper;
109
110
111 try
112 {
113 factory = (ArtifactFactory) helper.getComponent( ArtifactFactory.class );
114 resolver = (ArtifactResolver) helper.getComponent( ArtifactResolver.class );
115 local = (ArtifactRepository) helper.evaluate( "${localRepository}" );
116 project = (MavenProject) helper.evaluate( "${project}" );
117 remoteRepositories = project.getRemoteArtifactRepositories();
118 }
119 catch ( ComponentLookupException e )
120 {
121
122 e.printStackTrace();
123 }
124 catch ( ExpressionEvaluationException e )
125 {
126
127 e.printStackTrace();
128 }
129 }
130
131
132
133
134
135
136
137
138
139 private Model readModel( File pom )
140 throws IOException, XmlPullParserException
141 {
142 Reader reader = ReaderFactory.newXmlReader( pom );
143 MavenXpp3Reader xpp3 = new MavenXpp3Reader();
144 Model model = null;
145 try
146 {
147 model = xpp3.read( reader );
148 }
149 finally
150 {
151 reader.close();
152 reader = null;
153 }
154 return model;
155 }
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171 private Model getPomModel( String groupId, String artifactId, String version, File pom )
172 throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
173 {
174 Model model = null;
175
176
177
178
179
180
181
182
183
184 boolean found = false;
185 try
186 {
187 model = readModel( pom );
188
189
190
191 found = checkIfModelMatches( groupId, artifactId, version, model );
192 }
193 catch ( IOException e )
194 {
195
196
197 }
198 catch ( XmlPullParserException e )
199 {
200
201
202 }
203
204
205
206 if ( !found )
207 {
208 Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" );
209 resolver.resolve( pomArtifact, remoteRepositories, local );
210 model = readModel( pomArtifact.getFile() );
211 }
212
213 return model;
214 }
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 public List<Model> getModelsRecursively( String groupId, String artifactId, String version, File pom )
230 throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
231 {
232 List<Model> models = null;
233 Model model = getPomModel( groupId, artifactId, version, pom );
234
235 Parent parent = model.getParent();
236
237
238 if ( parent != null )
239 {
240
241 String relativePath = parent.getRelativePath();
242 if ( StringUtils.isEmpty( relativePath ) )
243 {
244 relativePath = "../pom.xml";
245 }
246
247 File parentPom = new File( pom.getParent(), relativePath );
248
249
250 if ( parentPom.isDirectory() )
251 {
252 parentPom = new File( parentPom, "pom.xml" );
253 }
254
255
256 models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(),
257 parent.getVersion(), parentPom );
258
259 }
260 else
261 {
262
263 models = new ArrayList<Model>();
264 }
265 models.add( model );
266
267 return models;
268 }
269
270
271
272
273
274
275
276
277
278
279 protected boolean checkIfModelMatches( String groupId, String artifactId, String version, Model model )
280 {
281
282 String modelGroup = model.getGroupId();
283 String modelArtifactId = model.getArtifactId();
284 String modelVersion = model.getVersion();
285
286 try
287 {
288 if ( StringUtils.isEmpty( modelGroup ) )
289 {
290 modelGroup = model.getParent().getGroupId();
291 }
292 else
293 {
294
295 modelGroup = (String) helper.evaluate( modelGroup );
296 }
297
298 if ( StringUtils.isEmpty( modelVersion ) )
299 {
300 modelVersion = model.getParent().getVersion();
301 }
302 else
303 {
304
305 modelVersion = (String) helper.evaluate( modelVersion );
306 }
307
308
309 modelArtifactId = (String) helper.evaluate( modelArtifactId );
310 }
311 catch ( NullPointerException e )
312 {
313
314
315
316
317
318 }
319 catch ( ExpressionEvaluationException e )
320 {
321
322 }
323
324 return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils.equals( artifactId,
325 modelArtifactId ) );
326
327 }
328
329 private void resolve( Plugin plugin )
330 {
331 try
332 {
333 plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
334 plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
335 plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
336 }
337 catch ( ExpressionEvaluationException e )
338 {
339
340 }
341 }
342
343 private void resolve( ReportPlugin plugin )
344 {
345 try
346 {
347 plugin.setGroupId( (String) helper.evaluate( plugin.getGroupId() ) );
348 plugin.setArtifactId( (String) helper.evaluate( plugin.getArtifactId() ) );
349 plugin.setVersion( (String) helper.evaluate( plugin.getVersion() ) );
350 }
351 catch ( ExpressionEvaluationException e )
352 {
353
354 }
355 }
356
357 public List<Plugin> resolvePlugins( List<Plugin> plugins )
358 {
359 for ( Plugin plugin : plugins )
360 {
361 resolve( plugin );
362 }
363 return plugins;
364 }
365
366 public List<ReportPlugin> resolveReportPlugins( List<ReportPlugin> reportPlugins )
367 {
368 for ( ReportPlugin plugin : reportPlugins )
369 {
370 resolve( plugin );
371 }
372 return reportPlugins;
373 }
374
375 }