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