1 package org.apache.continuum.buildagent.build.execution.maven.m1;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.continuum.model.project.Project;
23 import org.apache.maven.continuum.model.project.ProjectDependency;
24 import org.apache.maven.continuum.model.project.ProjectDeveloper;
25 import org.apache.maven.continuum.model.project.ProjectNotifier;
26 import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
27 import org.codehaus.plexus.util.StringUtils;
28 import org.codehaus.plexus.util.xml.Xpp3Dom;
29 import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
30 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.File;
35 import java.io.FileNotFoundException;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Properties;
41
42
43
44
45 public class DefaultBuildAgentMavenOneMetadataHelper
46 implements BuildAgentMavenOneMetadataHelper
47 {
48 private static final Logger log = LoggerFactory.getLogger( DefaultBuildAgentMavenOneMetadataHelper.class );
49
50 public void mapMetadata( ContinuumProjectBuildingResult result, File metadata, Project project )
51 throws BuildAgentMavenOneMetadataHelperException
52 {
53 Xpp3Dom mavenProject;
54
55 try
56 {
57 mavenProject = Xpp3DomBuilder.build( new FileReader( metadata ) );
58 }
59 catch ( XmlPullParserException e )
60 {
61 result.addError( ContinuumProjectBuildingResult.ERROR_XML_PARSE );
62
63 log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
64
65 return;
66 }
67 catch ( FileNotFoundException e )
68 {
69 result.addError( ContinuumProjectBuildingResult.ERROR_POM_NOT_FOUND );
70
71 log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
72
73 return;
74 }
75 catch ( IOException e )
76 {
77 result.addError( ContinuumProjectBuildingResult.ERROR_UNKNOWN );
78
79 log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
80
81 return;
82 }
83
84
85
86
87
88
89
90 String extend = getValue( mavenProject, "extend", null );
91
92 if ( extend != null )
93 {
94 result.addError( ContinuumProjectBuildingResult.ERROR_EXTEND );
95
96 log.info( "Cannot use a POM with an 'extend' element." );
97
98 return;
99 }
100
101
102
103
104
105 String groupId;
106
107 String artifactId;
108
109 String id = getValue( mavenProject, "id", null );
110
111 if ( !StringUtils.isEmpty( id ) )
112 {
113 groupId = id;
114
115 artifactId = id;
116 }
117 else
118 {
119 groupId = getValue( mavenProject, "groupId", project.getGroupId() );
120
121 if ( StringUtils.isEmpty( groupId ) )
122 {
123 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_GROUPID );
124
125 log.info( "Missing 'groupId' element in the POM." );
126
127
128 }
129
130 artifactId = getValue( mavenProject, "artifactId", project.getArtifactId() );
131
132 if ( StringUtils.isEmpty( artifactId ) )
133 {
134 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_ARTIFACTID );
135
136 log.info( "Missing 'artifactId' element in the POM." );
137
138
139 }
140 }
141
142
143
144
145
146 String version = getValue( mavenProject, "currentVersion", project.getVersion() );
147
148 if ( StringUtils.isEmpty( project.getVersion() ) && StringUtils.isEmpty( version ) )
149 {
150 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_VERSION );
151
152
153 }
154
155
156
157
158
159 String name = getValue( mavenProject, "name", project.getName() );
160
161 if ( StringUtils.isEmpty( project.getName() ) && StringUtils.isEmpty( name ) )
162 {
163 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_NAME );
164
165
166 }
167
168
169
170
171
172 String shortDescription = getValue( mavenProject, "shortDescription", project.getDescription() );
173
174 String description = getValue( mavenProject, "description", project.getDescription() );
175
176
177
178
179
180 Xpp3Dom repository = mavenProject.getChild( "repository" );
181
182 String scmConnection = null;
183
184 if ( repository == null )
185 {
186 if ( !StringUtils.isEmpty( project.getScmUrl() ) )
187 {
188 scmConnection = project.getScmUrl();
189 }
190 else
191 {
192 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_REPOSITORY );
193
194
195 }
196 }
197 else
198 {
199 scmConnection = getValue( repository, "developerConnection", project.getScmUrl() );
200
201 scmConnection = getValue( repository, "connection", scmConnection );
202
203 if ( StringUtils.isEmpty( scmConnection ) )
204 {
205 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_SCM, name );
206
207
208 }
209 }
210
211
212
213
214
215 Xpp3Dom developers = mavenProject.getChild( "developers" );
216
217 if ( developers != null )
218 {
219 Xpp3Dom[] developersList = developers.getChildren();
220
221 List<ProjectDeveloper> cds = new ArrayList<ProjectDeveloper>();
222
223 for ( Xpp3Dom developer : developersList )
224 {
225 ProjectDeveloper cd = new ProjectDeveloper();
226
227 cd.setScmId( getValue( developer, "id", null ) );
228
229 cd.setName( getValue( developer, "name", null ) );
230
231 cd.setEmail( getValue( developer, "email", null ) );
232
233 cds.add( cd );
234 }
235
236 project.setDevelopers( cds );
237 }
238
239
240
241
242
243 Xpp3Dom dependencies = mavenProject.getChild( "dependencies" );
244
245 if ( dependencies != null )
246 {
247 Xpp3Dom[] dependenciesList = dependencies.getChildren();
248
249 List<ProjectDependency> deps = new ArrayList<ProjectDependency>();
250
251 for ( Xpp3Dom dependency : dependenciesList )
252 {
253 ProjectDependency cd = new ProjectDependency();
254
255 if ( getValue( dependency, "groupId", null ) != null )
256 {
257 cd.setGroupId( getValue( dependency, "groupId", null ) );
258
259 cd.setArtifactId( getValue( dependency, "artifactId", null ) );
260 }
261 else
262 {
263 cd.setGroupId( getValue( dependency, "id", null ) );
264
265 cd.setArtifactId( getValue( dependency, "id", null ) );
266 }
267
268 cd.setVersion( getValue( dependency, "version", null ) );
269
270 deps.add( cd );
271 }
272
273 project.setDependencies( deps );
274 }
275
276
277
278
279
280 Xpp3Dom build = mavenProject.getChild( "build" );
281
282 List<ProjectNotifier> notifiers = new ArrayList<ProjectNotifier>();
283
284
285 if ( build != null )
286 {
287 String nagEmailAddress = getValue( build, "nagEmailAddress", null );
288
289 if ( nagEmailAddress != null )
290 {
291 Properties props = new Properties();
292
293 props.put( "address", nagEmailAddress );
294
295 ProjectNotifier notifier = new ProjectNotifier();
296
297 notifier.setConfiguration( props );
298
299 notifier.setFrom( ProjectNotifier.FROM_PROJECT );
300
301 notifiers.add( notifier );
302 }
303 }
304
305
306 if ( project.getNotifiers() != null && !project.getNotifiers().isEmpty() )
307 {
308 for ( ProjectNotifier notif : (List<ProjectNotifier>) project.getNotifiers() )
309 {
310 if ( notif.isFromUser() )
311 {
312 notifiers.add( notif );
313 }
314 }
315 }
316
317
318
319
320
321 if ( result.hasErrors() )
322 {
323
324 return;
325 }
326
327
328
329
330
331 project.setGroupId( groupId );
332
333 project.setArtifactId( artifactId );
334
335 project.setVersion( version );
336
337 project.setName( name );
338
339 if ( StringUtils.isEmpty( shortDescription ) )
340 {
341 project.setDescription( description );
342 }
343 else
344 {
345 project.setDescription( shortDescription );
346
347 }
348
349 project.setScmUrl( scmConnection );
350
351 project.setNotifiers( notifiers );
352 }
353
354
355
356
357
358 private String getValue( Xpp3Dom dom, String key, String defaultValue )
359 {
360 Xpp3Dom child = dom.getChild( key );
361
362 if ( child == null )
363 {
364 return defaultValue;
365 }
366
367 return child.getValue();
368 }
369 }