1 | package org.apache.maven.continuum.core.action; |
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.util.Date; |
24 | import java.util.List; |
25 | import java.util.Map; |
26 | |
27 | import org.apache.continuum.dao.BuildResultDao; |
28 | import org.apache.continuum.dao.ProjectDao; |
29 | import org.apache.continuum.utils.ContinuumUtils; |
30 | import org.apache.maven.continuum.configuration.ConfigurationService; |
31 | import org.apache.maven.continuum.execution.ContinuumBuildCancelledException; |
32 | import org.apache.maven.continuum.execution.ContinuumBuildExecutionResult; |
33 | import org.apache.maven.continuum.execution.ContinuumBuildExecutor; |
34 | import org.apache.maven.continuum.execution.manager.BuildExecutorManager; |
35 | import org.apache.maven.continuum.model.project.BuildDefinition; |
36 | import org.apache.maven.continuum.model.project.BuildResult; |
37 | import org.apache.maven.continuum.model.project.Project; |
38 | import org.apache.maven.continuum.model.project.ProjectDependency; |
39 | import org.apache.maven.continuum.model.scm.ScmResult; |
40 | import org.apache.maven.continuum.notification.ContinuumNotificationDispatcher; |
41 | import org.apache.maven.continuum.project.ContinuumProjectState; |
42 | |
43 | /** |
44 | * @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a> |
45 | * @version $Id: ExecuteBuilderContinuumAction.java 786891 2009-06-20 19:31:57Z jzurbano $ |
46 | * @plexus.component role="org.codehaus.plexus.action.Action" |
47 | * role-hint="execute-builder" |
48 | */ |
49 | public class ExecuteBuilderContinuumAction |
50 | extends AbstractContinuumAction |
51 | { |
52 | private static final String KEY_CANCELLED = "cancelled"; |
53 | |
54 | /** |
55 | * @plexus.requirement |
56 | */ |
57 | private ConfigurationService configurationService; |
58 | |
59 | /** |
60 | * @plexus.requirement |
61 | */ |
62 | private BuildExecutorManager buildExecutorManager; |
63 | |
64 | /** |
65 | * @plexus.requirement |
66 | */ |
67 | private BuildResultDao buildResultDao; |
68 | |
69 | /** |
70 | * @plexus.requirement |
71 | */ |
72 | private ProjectDao projectDao; |
73 | |
74 | /** |
75 | * @plexus.requirement |
76 | */ |
77 | private ContinuumNotificationDispatcher notifier; |
78 | |
79 | public void execute( Map context ) |
80 | throws Exception |
81 | { |
82 | // ---------------------------------------------------------------------- |
83 | // Get parameters from the context |
84 | // ---------------------------------------------------------------------- |
85 | |
86 | Project project = projectDao.getProject( getProject( context ).getId() ); |
87 | |
88 | BuildDefinition buildDefinition = getBuildDefinition( context ); |
89 | |
90 | int trigger = getTrigger( context ); |
91 | |
92 | ScmResult scmResult = getScmResult( context ); |
93 | |
94 | List<ProjectDependency> updatedDependencies = getUpdatedDependencies( context ); |
95 | |
96 | ContinuumBuildExecutor buildExecutor = buildExecutorManager.getBuildExecutor( project.getExecutorId() ); |
97 | |
98 | // ---------------------------------------------------------------------- |
99 | // Make the buildResult |
100 | // ---------------------------------------------------------------------- |
101 | |
102 | BuildResult buildResult = new BuildResult(); |
103 | |
104 | buildResult.setStartTime( new Date().getTime() ); |
105 | |
106 | buildResult.setState( ContinuumProjectState.BUILDING ); |
107 | |
108 | buildResult.setTrigger( trigger ); |
109 | |
110 | buildResult.setScmResult( scmResult ); |
111 | |
112 | buildResult.setModifiedDependencies( updatedDependencies ); |
113 | |
114 | buildResult.setBuildDefinition( getBuildDefinition( context ) ); |
115 | |
116 | buildResultDao.addBuildResult( project, buildResult ); |
117 | |
118 | AbstractContinuumAction.setBuildId( context, Integer.toString( buildResult.getId() ) ); |
119 | |
120 | setCancelled( context, false ); |
121 | |
122 | buildResult = buildResultDao.getBuildResult( buildResult.getId() ); |
123 | |
124 | try |
125 | { |
126 | notifier.runningGoals( project, buildDefinition, buildResult ); |
127 | |
128 | File buildOutputFile = configurationService.getBuildOutputFile( buildResult.getId(), project.getId() ); |
129 | |
130 | ContinuumBuildExecutionResult result = buildExecutor.build( project, buildDefinition, buildOutputFile ); |
131 | |
132 | buildResult.setState( result.getExitCode() == 0 ? ContinuumProjectState.OK : ContinuumProjectState.FAILED ); |
133 | |
134 | buildResult.setExitCode( result.getExitCode() ); |
135 | } |
136 | catch ( ContinuumBuildCancelledException e ) |
137 | { |
138 | getLogger().info( "Cancelled build" ); |
139 | |
140 | buildResult.setState( ContinuumProjectState.CANCELLED ); |
141 | |
142 | setCancelled( context, true ); |
143 | } |
144 | catch ( Throwable e ) |
145 | { |
146 | getLogger().error( "Error running buildResult", e ); |
147 | |
148 | buildResult.setState( ContinuumProjectState.ERROR ); |
149 | |
150 | buildResult.setError( ContinuumUtils.throwableToString( e ) ); |
151 | } |
152 | finally |
153 | { |
154 | project = projectDao.getProject( project.getId() ); |
155 | |
156 | if ( buildResult.getState() == ContinuumProjectState.CANCELLED ) |
157 | { |
158 | project.setState( project.getOldState() ); |
159 | |
160 | project.setOldState( 0 ); |
161 | |
162 | int buildResultId = getOldBuildId( context ); |
163 | |
164 | project.setLatestBuildId( buildResultId ); |
165 | |
166 | buildResultDao.removeBuildResult( buildResult ); |
167 | } |
168 | else |
169 | { |
170 | buildResult.setEndTime( new Date().getTime() ); |
171 | |
172 | if ( buildResult.getState() == ContinuumProjectState.OK ) |
173 | { |
174 | project.setBuildNumber( project.getBuildNumber() + 1 ); |
175 | } |
176 | |
177 | project.setLatestBuildId( buildResult.getId() ); |
178 | |
179 | buildResult.setBuildNumber( project.getBuildNumber() ); |
180 | |
181 | if ( buildResult.getState() != ContinuumProjectState.OK && |
182 | buildResult.getState() != ContinuumProjectState.FAILED && |
183 | buildResult.getState() != ContinuumProjectState.ERROR ) |
184 | { |
185 | buildResult.setState( ContinuumProjectState.ERROR ); |
186 | } |
187 | |
188 | project.setState( buildResult.getState() ); |
189 | |
190 | // ---------------------------------------------------------------------- |
191 | // Copy over the buildResult result |
192 | // ---------------------------------------------------------------------- |
193 | |
194 | buildResultDao.updateBuildResult( buildResult ); |
195 | |
196 | buildResult = buildResultDao.getBuildResult( buildResult.getId() ); |
197 | |
198 | notifier.goalsCompleted( project, buildDefinition, buildResult ); |
199 | } |
200 | |
201 | AbstractContinuumAction.setProject( context, project ); |
202 | |
203 | projectDao.updateProject( project ); |
204 | |
205 | // ---------------------------------------------------------------------- |
206 | // Backup test result files |
207 | // ---------------------------------------------------------------------- |
208 | //TODO: Move as a plugin |
209 | buildExecutor.backupTestFiles( project, buildResult.getId() ); |
210 | } |
211 | } |
212 | |
213 | public static boolean isCancelled( Map<String, Object> context ) |
214 | { |
215 | return getBoolean( context, KEY_CANCELLED ); |
216 | } |
217 | |
218 | private static void setCancelled( Map<String, Object> context, boolean cancelled ) |
219 | { |
220 | context.put( KEY_CANCELLED, cancelled ); |
221 | } |
222 | } |