1 | package org.apache.maven.continuum.execution.maven.m1; |
2 | |
3 | /* |
4 | * Licensed to the Apache Software Foundation (ASF) under one |
5 | * or more contributor license agreements. See the NOTICE file |
6 | * distributed with this work for additional information |
7 | * regarding copyright ownership. The ASF licenses this file |
8 | * to you under the Apache License, Version 2.0 (the |
9 | * "License"); you may not use this file except in compliance |
10 | * with the License. You may obtain a copy of the License at |
11 | * |
12 | * http://www.apache.org/licenses/LICENSE-2.0 |
13 | * |
14 | * Unless required by applicable law or agreed to in writing, |
15 | * software distributed under the License is distributed on an |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
17 | * KIND, either express or implied. See the License for the |
18 | * specific language governing permissions and limitations |
19 | * under the License. |
20 | */ |
21 | |
22 | import java.io.File; |
23 | import java.io.FileNotFoundException; |
24 | import java.io.FileReader; |
25 | import java.io.IOException; |
26 | import java.util.ArrayList; |
27 | import java.util.List; |
28 | import java.util.Properties; |
29 | |
30 | import org.apache.maven.continuum.model.project.Project; |
31 | import org.apache.maven.continuum.model.project.ProjectDependency; |
32 | import org.apache.maven.continuum.model.project.ProjectDeveloper; |
33 | import org.apache.maven.continuum.model.project.ProjectNotifier; |
34 | import org.apache.maven.continuum.notification.AbstractContinuumNotifier; |
35 | import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult; |
36 | import org.codehaus.plexus.util.StringUtils; |
37 | import org.codehaus.plexus.util.xml.Xpp3Dom; |
38 | import org.codehaus.plexus.util.xml.Xpp3DomBuilder; |
39 | import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
40 | import org.slf4j.Logger; |
41 | import org.slf4j.LoggerFactory; |
42 | |
43 | /** |
44 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> |
45 | * @version $Id: DefaultMavenOneMetadataHelper.java 764863 2009-04-14 16:28:12Z evenisse $ |
46 | * @plexus.component role="org.apache.maven.continuum.execution.maven.m1.MavenOneMetadataHelper" |
47 | * role-hint="default" |
48 | */ |
49 | public class DefaultMavenOneMetadataHelper |
50 | implements MavenOneMetadataHelper |
51 | { |
52 | private static final Logger log = LoggerFactory.getLogger( DefaultMavenOneMetadataHelper.class ); |
53 | |
54 | // ---------------------------------------------------------------------- |
55 | // MavenOneMetadataHelper Implementation |
56 | // ---------------------------------------------------------------------- |
57 | |
58 | /** |
59 | * @deprecated Use {@link #mapMetadata(ContinuumProjectBuildingResult,File,Project)} instead |
60 | */ |
61 | public void mapMetadata( File metadata, Project project ) |
62 | throws MavenOneMetadataHelperException |
63 | { |
64 | mapMetadata( new ContinuumProjectBuildingResult(), metadata, project ); |
65 | } |
66 | |
67 | public void mapMetadata( ContinuumProjectBuildingResult result, File metadata, Project project ) |
68 | throws MavenOneMetadataHelperException |
69 | { |
70 | Xpp3Dom mavenProject; |
71 | |
72 | try |
73 | { |
74 | mavenProject = Xpp3DomBuilder.build( new FileReader( metadata ) ); |
75 | } |
76 | catch ( XmlPullParserException e ) |
77 | { |
78 | result.addError( ContinuumProjectBuildingResult.ERROR_XML_PARSE ); |
79 | |
80 | log.info( "Error while reading maven POM (" + e.getMessage() + ").", e ); |
81 | |
82 | return; |
83 | } |
84 | catch ( FileNotFoundException e ) |
85 | { |
86 | result.addError( ContinuumProjectBuildingResult.ERROR_POM_NOT_FOUND ); |
87 | |
88 | log.info( "Error while reading maven POM (" + e.getMessage() + ").", e ); |
89 | |
90 | return; |
91 | } |
92 | catch ( IOException e ) |
93 | { |
94 | result.addError( ContinuumProjectBuildingResult.ERROR_UNKNOWN ); |
95 | |
96 | log.info( "Error while reading maven POM (" + e.getMessage() + ").", e ); |
97 | |
98 | return; |
99 | } |
100 | |
101 | // ---------------------------------------------------------------------- |
102 | // We cannot deal with projects that use the <extend/> element because |
103 | // we don't have the whole source tree and we might be missing elements |
104 | // that are present in the parent. |
105 | // ---------------------------------------------------------------------- |
106 | |
107 | String extend = getValue( mavenProject, "extend", null ); |
108 | |
109 | if ( extend != null ) |
110 | { |
111 | result.addError( ContinuumProjectBuildingResult.ERROR_EXTEND ); |
112 | |
113 | log.info( "Cannot use a POM with an 'extend' element." ); |
114 | |
115 | return; |
116 | } |
117 | |
118 | // ---------------------------------------------------------------------- |
119 | // Artifact and group id |
120 | // ---------------------------------------------------------------------- |
121 | |
122 | String groupId; |
123 | |
124 | String artifactId; |
125 | |
126 | String id = getValue( mavenProject, "id", null ); |
127 | |
128 | if ( !StringUtils.isEmpty( id ) ) |
129 | { |
130 | groupId = id; |
131 | |
132 | artifactId = id; |
133 | } |
134 | else |
135 | { |
136 | groupId = getValue( mavenProject, "groupId", project.getGroupId() ); |
137 | |
138 | if ( StringUtils.isEmpty( groupId ) ) |
139 | { |
140 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_GROUPID ); |
141 | |
142 | log.info( "Missing 'groupId' element in the POM." ); |
143 | |
144 | // Do not throw an exception or return here, gather up as many results as possible first. |
145 | } |
146 | |
147 | artifactId = getValue( mavenProject, "artifactId", project.getArtifactId() ); |
148 | |
149 | if ( StringUtils.isEmpty( artifactId ) ) |
150 | { |
151 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_ARTIFACTID ); |
152 | |
153 | log.info( "Missing 'artifactId' element in the POM." ); |
154 | |
155 | // Do not throw an exception or return here, gather up as many results as possible first. |
156 | } |
157 | } |
158 | |
159 | // ---------------------------------------------------------------------- |
160 | // version |
161 | // ---------------------------------------------------------------------- |
162 | |
163 | String version = getValue( mavenProject, "currentVersion", project.getVersion() ); |
164 | |
165 | if ( StringUtils.isEmpty( project.getVersion() ) && StringUtils.isEmpty( version ) ) |
166 | { |
167 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_VERSION ); |
168 | |
169 | // Do not throw an exception or return here, gather up as many results as possible first. |
170 | } |
171 | |
172 | // ---------------------------------------------------------------------- |
173 | // name |
174 | // ---------------------------------------------------------------------- |
175 | |
176 | String name = getValue( mavenProject, "name", project.getName() ); |
177 | |
178 | if ( StringUtils.isEmpty( project.getName() ) && StringUtils.isEmpty( name ) ) |
179 | { |
180 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_NAME ); |
181 | |
182 | // Do not throw an exception or return here, gather up as many results as possible first. |
183 | } |
184 | |
185 | // ---------------------------------------------------------------------- |
186 | // description |
187 | // ---------------------------------------------------------------------- |
188 | |
189 | String shortDescription = getValue( mavenProject, "shortDescription", project.getDescription() ); |
190 | |
191 | String description = getValue( mavenProject, "description", project.getDescription() ); |
192 | |
193 | // ---------------------------------------------------------------------- |
194 | // scm |
195 | // ---------------------------------------------------------------------- |
196 | |
197 | Xpp3Dom repository = mavenProject.getChild( "repository" ); |
198 | |
199 | String scmConnection = null; |
200 | |
201 | if ( repository == null ) |
202 | { |
203 | if ( !StringUtils.isEmpty( project.getScmUrl() ) ) |
204 | { |
205 | scmConnection = project.getScmUrl(); |
206 | } |
207 | else |
208 | { |
209 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_REPOSITORY ); |
210 | |
211 | // Do not throw an exception or return here, gather up as many results as possible first. |
212 | } |
213 | } |
214 | else |
215 | { |
216 | scmConnection = getValue( repository, "developerConnection", project.getScmUrl() ); |
217 | |
218 | scmConnection = getValue( repository, "connection", scmConnection ); |
219 | |
220 | if ( StringUtils.isEmpty( scmConnection ) ) |
221 | { |
222 | result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_SCM, name ); |
223 | |
224 | // Do not throw an exception or return here, gather up as many results as possible first. |
225 | } |
226 | } |
227 | |
228 | // ---------------------------------------------------------------------- |
229 | // Developers |
230 | // ---------------------------------------------------------------------- |
231 | |
232 | Xpp3Dom developers = mavenProject.getChild( "developers" ); |
233 | |
234 | if ( developers != null ) |
235 | { |
236 | Xpp3Dom[] developersList = developers.getChildren(); |
237 | |
238 | List<ProjectDeveloper> cds = new ArrayList<ProjectDeveloper>(); |
239 | |
240 | for ( Xpp3Dom developer : developersList ) |
241 | { |
242 | ProjectDeveloper cd = new ProjectDeveloper(); |
243 | |
244 | cd.setScmId( getValue( developer, "id", null ) ); |
245 | |
246 | cd.setName( getValue( developer, "name", null ) ); |
247 | |
248 | cd.setEmail( getValue( developer, "email", null ) ); |
249 | |
250 | cds.add( cd ); |
251 | } |
252 | |
253 | project.setDevelopers( cds ); |
254 | } |
255 | |
256 | // ---------------------------------------------------------------------- |
257 | // Dependencies |
258 | // ---------------------------------------------------------------------- |
259 | |
260 | Xpp3Dom dependencies = mavenProject.getChild( "dependencies" ); |
261 | |
262 | if ( dependencies != null ) |
263 | { |
264 | Xpp3Dom[] dependenciesList = dependencies.getChildren(); |
265 | |
266 | List<ProjectDependency> deps = new ArrayList<ProjectDependency>(); |
267 | |
268 | for ( Xpp3Dom dependency : dependenciesList ) |
269 | { |
270 | ProjectDependency cd = new ProjectDependency(); |
271 | |
272 | if ( getValue( dependency, "groupId", null ) != null ) |
273 | { |
274 | cd.setGroupId( getValue( dependency, "groupId", null ) ); |
275 | |
276 | cd.setArtifactId( getValue( dependency, "artifactId", null ) ); |
277 | } |
278 | else |
279 | { |
280 | cd.setGroupId( getValue( dependency, "id", null ) ); |
281 | |
282 | cd.setArtifactId( getValue( dependency, "id", null ) ); |
283 | } |
284 | |
285 | cd.setVersion( getValue( dependency, "version", null ) ); |
286 | |
287 | deps.add( cd ); |
288 | } |
289 | |
290 | project.setDependencies( deps ); |
291 | } |
292 | |
293 | // ---------------------------------------------------------------------- |
294 | // notifiers |
295 | // ---------------------------------------------------------------------- |
296 | |
297 | Xpp3Dom build = mavenProject.getChild( "build" ); |
298 | |
299 | List<ProjectNotifier> notifiers = new ArrayList<ProjectNotifier>(); |
300 | |
301 | // Add project Notifier |
302 | if ( build != null ) |
303 | { |
304 | String nagEmailAddress = getValue( build, "nagEmailAddress", null ); |
305 | |
306 | if ( nagEmailAddress != null ) |
307 | { |
308 | Properties props = new Properties(); |
309 | |
310 | props.put( AbstractContinuumNotifier.ADDRESS_FIELD, nagEmailAddress ); |
311 | |
312 | ProjectNotifier notifier = new ProjectNotifier(); |
313 | |
314 | notifier.setConfiguration( props ); |
315 | |
316 | notifier.setFrom( ProjectNotifier.FROM_PROJECT ); |
317 | |
318 | notifiers.add( notifier ); |
319 | } |
320 | } |
321 | |
322 | // Add all user notifiers |
323 | if ( project.getNotifiers() != null && !project.getNotifiers().isEmpty() ) |
324 | { |
325 | for ( ProjectNotifier notif : (List<ProjectNotifier>) project.getNotifiers() ) |
326 | { |
327 | if ( notif.isFromUser() ) |
328 | { |
329 | notifiers.add( notif ); |
330 | } |
331 | } |
332 | } |
333 | |
334 | // ---------------------------------------------------------------------- |
335 | // Handle Errors / Results |
336 | // ---------------------------------------------------------------------- |
337 | |
338 | if ( result.hasErrors() ) |
339 | { |
340 | // prevent project creation if there are errors. |
341 | return; |
342 | } |
343 | |
344 | // ---------------------------------------------------------------------- |
345 | // Make the project |
346 | // ---------------------------------------------------------------------- |
347 | |
348 | project.setGroupId( groupId ); |
349 | |
350 | project.setArtifactId( artifactId ); |
351 | |
352 | project.setVersion( version ); |
353 | |
354 | project.setName( name ); |
355 | |
356 | if ( StringUtils.isEmpty( shortDescription ) ) |
357 | { |
358 | project.setDescription( description ); |
359 | } |
360 | else |
361 | { |
362 | project.setDescription( shortDescription ); |
363 | } |
364 | |
365 | project.setScmUrl( scmConnection ); |
366 | |
367 | project.setNotifiers( notifiers ); |
368 | } |
369 | |
370 | // ---------------------------------------------------------------------- |
371 | // |
372 | // ---------------------------------------------------------------------- |
373 | |
374 | private String getValue( Xpp3Dom dom, String key, String defaultValue ) |
375 | { |
376 | Xpp3Dom child = dom.getChild( key ); |
377 | |
378 | if ( child == null ) |
379 | { |
380 | return defaultValue; |
381 | } |
382 | |
383 | return child.getValue(); |
384 | } |
385 | } |