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