View Javadoc

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 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.notification.AbstractContinuumNotifier;
27  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
28  import org.codehaus.plexus.util.StringUtils;
29  import org.codehaus.plexus.util.xml.Xpp3Dom;
30  import org.codehaus.plexus.util.xml.Xpp3DomBuilder;
31  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import java.io.File;
36  import java.io.FileNotFoundException;
37  import java.io.FileReader;
38  import java.io.IOException;
39  import java.util.ArrayList;
40  import java.util.List;
41  import java.util.Properties;
42  
43  /**
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   * @version $Id: DefaultMavenOneMetadataHelper.java 1372260 2012-08-13 04:29:09Z brett $
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, true );
65      }
66  
67      public void mapMetadata( ContinuumProjectBuildingResult result, File metadata, Project project,
68                               boolean updateDefinition )
69          throws MavenOneMetadataHelperException
70      {
71          Xpp3Dom mavenProject;
72  
73          try
74          {
75              mavenProject = Xpp3DomBuilder.build( new FileReader( metadata ) );
76          }
77          catch ( XmlPullParserException e )
78          {
79              result.addError( ContinuumProjectBuildingResult.ERROR_XML_PARSE );
80  
81              log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
82  
83              return;
84          }
85          catch ( FileNotFoundException e )
86          {
87              result.addError( ContinuumProjectBuildingResult.ERROR_POM_NOT_FOUND );
88  
89              log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
90  
91              return;
92          }
93          catch ( IOException e )
94          {
95              result.addError( ContinuumProjectBuildingResult.ERROR_UNKNOWN );
96  
97              log.info( "Error while reading maven POM (" + e.getMessage() + ").", e );
98  
99              return;
100         }
101 
102         // ----------------------------------------------------------------------
103         // We cannot deal with projects that use the <extend/> element because
104         // we don't have the whole source tree and we might be missing elements
105         // that are present in the parent.
106         // ----------------------------------------------------------------------
107 
108         String extend = getValue( mavenProject, "extend", null );
109 
110         if ( extend != null )
111         {
112             result.addError( ContinuumProjectBuildingResult.ERROR_EXTEND );
113 
114             log.info( "Cannot use a POM with an 'extend' element." );
115 
116             return;
117         }
118 
119         // ----------------------------------------------------------------------
120         // Artifact and group id
121         // ----------------------------------------------------------------------
122 
123         String groupId;
124 
125         String artifactId;
126 
127         String id = getValue( mavenProject, "id", null );
128 
129         if ( !StringUtils.isEmpty( id ) )
130         {
131             groupId = id;
132 
133             artifactId = id;
134         }
135         else
136         {
137             groupId = getValue( mavenProject, "groupId", project.getGroupId() );
138 
139             if ( StringUtils.isEmpty( groupId ) )
140             {
141                 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_GROUPID );
142 
143                 log.info( "Missing 'groupId' element in the POM." );
144 
145                 // Do not throw an exception or return here, gather up as many results as possible first.
146             }
147 
148             artifactId = getValue( mavenProject, "artifactId", project.getArtifactId() );
149 
150             if ( StringUtils.isEmpty( artifactId ) )
151             {
152                 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_ARTIFACTID );
153 
154                 log.info( "Missing 'artifactId' element in the POM." );
155 
156                 // Do not throw an exception or return here, gather up as many results as possible first.
157             }
158         }
159 
160         // ----------------------------------------------------------------------
161         // version
162         // ----------------------------------------------------------------------
163 
164         String version = getValue( mavenProject, "currentVersion", project.getVersion() );
165 
166         if ( StringUtils.isEmpty( project.getVersion() ) && StringUtils.isEmpty( version ) )
167         {
168             result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_VERSION );
169 
170             // Do not throw an exception or return here, gather up as many results as possible first.
171         }
172 
173         // ----------------------------------------------------------------------
174         // name
175         // ----------------------------------------------------------------------
176 
177         String name = getValue( mavenProject, "name", project.getName() );
178 
179         if ( StringUtils.isEmpty( project.getName() ) && StringUtils.isEmpty( name ) )
180         {
181             result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_NAME );
182 
183             // Do not throw an exception or return here, gather up as many results as possible first.
184         }
185 
186         // ----------------------------------------------------------------------
187         // description
188         // ----------------------------------------------------------------------
189 
190         String shortDescription = getValue( mavenProject, "shortDescription", project.getDescription() );
191 
192         String description = getValue( mavenProject, "description", project.getDescription() );
193 
194         // ----------------------------------------------------------------------
195         // scm
196         // ----------------------------------------------------------------------
197 
198         Xpp3Dom repository = mavenProject.getChild( "repository" );
199 
200         String scmConnection = null;
201 
202         if ( repository == null )
203         {
204             if ( !StringUtils.isEmpty( project.getScmUrl() ) )
205             {
206                 scmConnection = project.getScmUrl();
207             }
208             else
209             {
210                 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_REPOSITORY );
211 
212                 // Do not throw an exception or return here, gather up as many results as possible first.
213             }
214         }
215         else
216         {
217             scmConnection = getValue( repository, "developerConnection", project.getScmUrl() );
218 
219             scmConnection = getValue( repository, "connection", scmConnection );
220 
221             if ( StringUtils.isEmpty( scmConnection ) )
222             {
223                 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_SCM, name );
224 
225                 // Do not throw an exception or return here, gather up as many results as possible first.
226             }
227         }
228 
229         // ----------------------------------------------------------------------
230         // Developers
231         // ----------------------------------------------------------------------
232 
233         Xpp3Dom developers = mavenProject.getChild( "developers" );
234 
235         if ( developers != null )
236         {
237             Xpp3Dom[] developersList = developers.getChildren();
238 
239             List<ProjectDeveloper> cds = new ArrayList<ProjectDeveloper>();
240 
241             for ( Xpp3Dom developer : developersList )
242             {
243                 ProjectDeveloper cd = new ProjectDeveloper();
244 
245                 cd.setScmId( getValue( developer, "id", null ) );
246 
247                 cd.setName( getValue( developer, "name", null ) );
248 
249                 cd.setEmail( getValue( developer, "email", null ) );
250 
251                 cds.add( cd );
252             }
253 
254             project.setDevelopers( cds );
255         }
256 
257         // ----------------------------------------------------------------------
258         // Dependencies
259         // ----------------------------------------------------------------------
260 
261         Xpp3Dom dependencies = mavenProject.getChild( "dependencies" );
262 
263         if ( dependencies != null )
264         {
265             Xpp3Dom[] dependenciesList = dependencies.getChildren();
266 
267             List<ProjectDependency> deps = new ArrayList<ProjectDependency>();
268 
269             for ( Xpp3Dom dependency : dependenciesList )
270             {
271                 ProjectDependency cd = new ProjectDependency();
272 
273                 if ( getValue( dependency, "groupId", null ) != null )
274                 {
275                     cd.setGroupId( getValue( dependency, "groupId", null ) );
276 
277                     cd.setArtifactId( getValue( dependency, "artifactId", null ) );
278                 }
279                 else
280                 {
281                     cd.setGroupId( getValue( dependency, "id", null ) );
282 
283                     cd.setArtifactId( getValue( dependency, "id", null ) );
284                 }
285 
286                 cd.setVersion( getValue( dependency, "version", null ) );
287 
288                 deps.add( cd );
289             }
290 
291             project.setDependencies( deps );
292         }
293 
294         // ----------------------------------------------------------------------
295         // notifiers
296         // ----------------------------------------------------------------------
297 
298         Xpp3Dom build = mavenProject.getChild( "build" );
299 
300         List<ProjectNotifier> notifiers = new ArrayList<ProjectNotifier>();
301 
302         // Add project Notifier
303         if ( build != null )
304         {
305             String nagEmailAddress = getValue( build, "nagEmailAddress", null );
306 
307             if ( nagEmailAddress != null )
308             {
309                 Properties props = new Properties();
310 
311                 props.put( AbstractContinuumNotifier.ADDRESS_FIELD, nagEmailAddress );
312 
313                 ProjectNotifier notifier = new ProjectNotifier();
314 
315                 notifier.setConfiguration( props );
316 
317                 notifier.setFrom( ProjectNotifier.FROM_PROJECT );
318 
319                 notifiers.add( notifier );
320             }
321         }
322 
323         // Add all user notifiers
324         if ( project.getNotifiers() != null && !project.getNotifiers().isEmpty() )
325         {
326             for ( ProjectNotifier notif : (List<ProjectNotifier>) project.getNotifiers() )
327             {
328                 if ( notif.isFromUser() )
329                 {
330                     notifiers.add( notif );
331                 }
332             }
333         }
334 
335         // ----------------------------------------------------------------------
336         // Handle Errors / Results
337         // ----------------------------------------------------------------------
338 
339         if ( result.hasErrors() )
340         {
341             // prevent project creation if there are errors.
342             return;
343         }
344 
345         // ----------------------------------------------------------------------
346         // Make the project
347         // ----------------------------------------------------------------------
348 
349         project.setGroupId( groupId );
350 
351         project.setArtifactId( artifactId );
352 
353         if ( updateDefinition )
354         {
355             project.setVersion( version );
356 
357             project.setName( name );
358         }
359 
360         if ( StringUtils.isEmpty( shortDescription ) )
361         {
362             project.setDescription( description );
363         }
364         else
365         {
366             project.setDescription( shortDescription );
367         }
368 
369         project.setScmUrl( scmConnection );
370 
371         project.setNotifiers( notifiers );
372     }
373 
374     // ----------------------------------------------------------------------
375     //
376     // ----------------------------------------------------------------------
377 
378     private String getValue( Xpp3Dom dom, String key, String defaultValue )
379     {
380         Xpp3Dom child = dom.getChild( key );
381 
382         if ( child == null )
383         {
384             return defaultValue;
385         }
386 
387         return child.getValue();
388     }
389 }