View Javadoc

1   package org.apache.maven.continuum.project.builder.maven;
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.continuum.dao.ProjectGroupDao;
23  import org.apache.maven.continuum.builddefinition.BuildDefinitionService;
24  import org.apache.maven.continuum.builddefinition.BuildDefinitionServiceException;
25  import org.apache.maven.continuum.execution.maven.m1.MavenOneBuildExecutor;
26  import org.apache.maven.continuum.execution.maven.m1.MavenOneMetadataHelper;
27  import org.apache.maven.continuum.execution.maven.m1.MavenOneMetadataHelperException;
28  import org.apache.maven.continuum.model.project.BuildDefinition;
29  import org.apache.maven.continuum.model.project.BuildDefinitionTemplate;
30  import org.apache.maven.continuum.model.project.Project;
31  import org.apache.maven.continuum.model.project.ProjectGroup;
32  import org.apache.maven.continuum.project.builder.AbstractContinuumProjectBuilder;
33  import org.apache.maven.continuum.project.builder.ContinuumProjectBuilder;
34  import org.apache.maven.continuum.project.builder.ContinuumProjectBuilderException;
35  import org.apache.maven.continuum.project.builder.ContinuumProjectBuildingResult;
36  import org.apache.maven.continuum.store.ContinuumStoreException;
37  import org.codehaus.plexus.util.StringUtils;
38  
39  import java.io.File;
40  import java.net.URL;
41  import java.util.List;
42  
43  /**
44   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
45   * @version $Id: MavenOneContinuumProjectBuilder.java 1392223 2012-10-01 06:58:57Z brett $
46   * @plexus.component role="org.apache.maven.continuum.project.builder.ContinuumProjectBuilder"
47   * role-hint="maven-one-builder"
48   */
49  public class MavenOneContinuumProjectBuilder
50      extends AbstractContinuumProjectBuilder
51      implements ContinuumProjectBuilder
52  {
53      public static final String ID = "maven-one-builder";
54  
55      /**
56       * @plexus.requirement
57       */
58      private BuildDefinitionService buildDefinitionService;
59  
60      /**
61       * @plexus.requirement
62       */
63      private MavenOneMetadataHelper metadataHelper;
64  
65      /**
66       * @plexus.requirement
67       */
68      private ProjectGroupDao projectGroupDao;
69  
70      // ----------------------------------------------------------------------
71      // ProjectCreator Implementation
72      // ----------------------------------------------------------------------
73  
74      public ContinuumProjectBuildingResult buildProjectsFromMetadata( URL url, String username, String password )
75          throws ContinuumProjectBuilderException
76      {
77          return buildProjectsFromMetadata( url, username, password, true, false );
78      }
79  
80      public ContinuumProjectBuildingResult buildProjectsFromMetadata( URL url, String username, String password,
81                                                                       boolean recursiveProjects,
82                                                                       boolean checkoutInSingleDirectory )
83          throws ContinuumProjectBuilderException
84      {
85          try
86          {
87              return buildProjectsFromMetadata( url, username, password, recursiveProjects,
88                                                buildDefinitionService.getDefaultMavenOneBuildDefinitionTemplate(),
89                                                false );
90          }
91          catch ( BuildDefinitionServiceException e )
92          {
93              throw new ContinuumProjectBuilderException( e.getMessage(), e );
94          }
95      }
96  
97      public ContinuumProjectBuildingResult buildProjectsFromMetadata( URL url, String username, String password,
98                                                                       boolean recursiveProjects,
99                                                                       BuildDefinitionTemplate buildDefinitionTemplate,
100                                                                      boolean checkoutInSingleDirectory )
101         throws ContinuumProjectBuilderException
102     {
103         return buildProjectsFromMetadata( url, username, password, buildDefinitionTemplate, null );
104     }
105 
106     public ContinuumProjectBuildingResult buildProjectsFromMetadata( URL url, String username, String password,
107                                                                      boolean recursiveProjects,
108                                                                      BuildDefinitionTemplate buildDefinitionTemplate,
109                                                                      boolean checkoutInSingleDirectory,
110                                                                      int projectGroupId )
111         throws ContinuumProjectBuilderException
112     {
113         ProjectGroup projectGroup = null;
114         if ( projectGroupId > 0 )
115         {
116             try
117             {
118                 projectGroup = projectGroupDao.getProjectGroupWithBuildDetailsByProjectGroupId( projectGroupId );
119             }
120             catch ( ContinuumStoreException e )
121             {
122                 throw new ContinuumProjectBuilderException( e.getMessage(), e );
123             }
124         }
125 
126         return buildProjectsFromMetadata( url, username, password, buildDefinitionTemplate, projectGroup );
127     }
128 
129     private ContinuumProjectBuildingResult buildProjectsFromMetadata( URL url, String username, String password,
130                                                                       BuildDefinitionTemplate buildDefinitionTemplate,
131                                                                       ProjectGroup projectGroup )
132     {
133         ContinuumProjectBuildingResult result = new ContinuumProjectBuildingResult();
134 
135         File pomFile;
136 
137         pomFile = createMetadataFile( result, url, username, password );
138 
139         if ( pomFile == null )
140         {
141             return result;
142         }
143 
144         Project project = new Project();
145 
146         try
147         {
148             metadataHelper.mapMetadata( result, pomFile, project, true );
149 
150             if ( result.hasErrors() )
151             {
152                 return result;
153             }
154             for ( BuildDefinition bd : (List<BuildDefinition>) buildDefinitionTemplate.getBuildDefinitions() )
155             {
156                 BuildDefinition cloneBuildDefinition = buildDefinitionService.cloneBuildDefinition( bd );
157                 cloneBuildDefinition.setTemplate( false );
158                 project.addBuildDefinition( cloneBuildDefinition );
159             }
160             result.addProject( project, MavenOneBuildExecutor.ID );
161         }
162         catch ( MavenOneMetadataHelperException e )
163         {
164             log.error( "Unknown error while processing metadata", e );
165 
166             result.addError( ContinuumProjectBuildingResult.ERROR_UNKNOWN );
167         }
168         finally
169         {
170             if ( pomFile.exists() )
171             {
172                 pomFile.delete();
173             }
174         }
175 
176         if ( projectGroup == null )
177         {
178             projectGroup = new ProjectGroup();
179 
180             // ----------------------------------------------------------------------
181             // Group id
182             // ----------------------------------------------------------------------
183 
184             if ( StringUtils.isEmpty( project.getGroupId() ) )
185             {
186                 result.addError( ContinuumProjectBuildingResult.ERROR_MISSING_GROUPID );
187             }
188 
189             projectGroup.setGroupId( project.getGroupId() );
190 
191             // ----------------------------------------------------------------------
192             // Name
193             // ----------------------------------------------------------------------
194 
195             String name = project.getName();
196 
197             if ( StringUtils.isEmpty( name ) )
198             {
199                 name = project.getGroupId();
200             }
201 
202             projectGroup.setName( name );
203 
204             // ----------------------------------------------------------------------
205             // Description
206             // ----------------------------------------------------------------------
207 
208             projectGroup.setDescription( project.getDescription() );
209         }
210 
211         result.addProjectGroup( projectGroup );
212 
213         return result;
214     }
215 
216     public BuildDefinitionTemplate getDefaultBuildDefinitionTemplate()
217         throws ContinuumProjectBuilderException
218     {
219         try
220         {
221             return buildDefinitionService.getDefaultMavenOneBuildDefinitionTemplate();
222         }
223         catch ( BuildDefinitionServiceException e )
224         {
225             throw new ContinuumProjectBuilderException( e.getMessage(), e );
226         }
227     }
228 }