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.io.xpp3.MavenXpp3Reader;
38 import org.apache.maven.plugin.logging.Log;
39 import org.apache.maven.project.MavenProject;
40 import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
41 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
42 import org.codehaus.plexus.util.ReaderFactory;
43 import org.codehaus.plexus.util.StringUtils;
44 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
45
46
47
48
49
50
51
52 public class EnforcerRuleUtils
53 {
54
55
56 ArtifactFactory factory;
57
58
59 ArtifactResolver resolver;
60
61
62 ArtifactRepository local;
63
64
65 List<ArtifactRepository> remoteRepositories;
66
67
68 Log log;
69
70
71 MavenProject project;
72
73 private EnforcerRuleHelper helper;
74
75
76
77
78
79
80
81
82
83
84
85 public EnforcerRuleUtils( ArtifactFactory theFactory, ArtifactResolver theResolver, ArtifactRepository theLocal,
86 List<ArtifactRepository> theRemoteRepositories, MavenProject project, Log theLog )
87 {
88 super();
89 this.factory = theFactory;
90 this.resolver = theResolver;
91 this.local = theLocal;
92 this.remoteRepositories = theRemoteRepositories;
93 this.log = theLog;
94 this.project = project;
95 }
96
97
98
99
100
101
102 @SuppressWarnings( "unchecked" )
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
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
172
173
174 private Model getPomModel ( String groupId, String artifactId, String version, File pom )
175 throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
176 {
177 Model model = null;
178
179
180
181
182
183
184
185
186
187 boolean found = false;
188 try
189 {
190 model = readModel( pom );
191
192
193
194 found = checkIfModelMatches( groupId, artifactId, version, model );
195 }
196 catch ( IOException e )
197 {
198
199
200 }
201 catch ( XmlPullParserException e )
202 {
203
204
205 }
206
207
208
209 if ( !found )
210 {
211 Artifact pomArtifact = factory.createArtifact( groupId, artifactId, version, null, "pom" );
212 resolver.resolve( pomArtifact, remoteRepositories, local );
213 model = readModel( pomArtifact.getFile() );
214 }
215
216 return model;
217 }
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235 public List<Model> getModelsRecursively ( String groupId, String artifactId, String version, File pom )
236 throws ArtifactResolutionException, ArtifactNotFoundException, IOException, XmlPullParserException
237 {
238 List<Model> models = null;
239 Model model = getPomModel( groupId, artifactId, version, pom );
240
241 Parent parent = model.getParent();
242
243
244 if ( parent != null )
245 {
246
247 String relativePath = parent.getRelativePath();
248 if ( StringUtils.isEmpty( relativePath ) )
249 {
250 relativePath = "../pom.xml";
251 }
252
253 File parentPom = new File( pom.getParent(), relativePath );
254
255
256 if ( parentPom.isDirectory() )
257 {
258 parentPom = new File( parentPom, "pom.xml" );
259 }
260
261 models = getModelsRecursively( parent.getGroupId(), parent.getArtifactId(), parent.getVersion(), parentPom );
262 }
263 else
264 {
265
266 models = new ArrayList<Model>();
267 }
268 models.add( model );
269
270 return models;
271 }
272
273
274
275
276
277
278
279
280
281
282
283 protected boolean checkIfModelMatches ( String groupId, String artifactId, String version, Model model )
284 {
285
286 String modelGroup = model.getGroupId();
287 String modelVersion = model.getVersion();
288
289 try
290 {
291 if ( StringUtils.isEmpty( modelGroup ) )
292 {
293 modelGroup = model.getParent().getGroupId();
294 }
295 else
296 {
297
298 modelGroup = (String) helper.evaluate( modelGroup );
299 }
300
301 if ( StringUtils.isEmpty( modelVersion ) )
302 {
303 modelVersion = model.getParent().getVersion();
304 }
305 else
306 {
307
308 modelVersion = (String) helper.evaluate( modelVersion );
309 }
310 }
311 catch ( NullPointerException e )
312 {
313
314
315
316
317
318 }
319 catch ( ExpressionEvaluationException e )
320 {
321
322 }
323 return ( StringUtils.equals( groupId, modelGroup ) && StringUtils.equals( version, modelVersion ) && StringUtils
324 .equals( artifactId, model.getArtifactId() ) );
325 }
326 }