587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.scm.ant.taskdefs;
|
|
19 |
|
|
20 |
import java.util.ArrayList;
|
|
21 |
import java.util.List;
|
628
|
22 |
|
|
23 |
import org.apache.maven.scm.ScmException;
|
587
|
24 |
import org.apache.maven.scm.manager.NoSuchScmProviderException;
|
|
25 |
import org.apache.maven.scm.manager.ScmManager;
|
|
26 |
import org.apache.maven.scm.repository.ScmRepository;
|
|
27 |
import org.apache.maven.scm.repository.ScmRepositoryException;
|
|
28 |
import org.apache.tools.ant.BuildException;
|
|
29 |
import org.apache.tools.ant.Task;
|
|
30 |
|
628
|
31 |
import com.nokia.helium.scm.ant.AntScmLogger;
|
588
|
32 |
import com.nokia.helium.scm.ant.actions.AddAction;
|
|
33 |
import com.nokia.helium.scm.ant.actions.BranchAction;
|
|
34 |
import com.nokia.helium.scm.ant.actions.ChangelogAction;
|
|
35 |
import com.nokia.helium.scm.ant.actions.CheckinAction;
|
|
36 |
import com.nokia.helium.scm.ant.actions.CheckoutAction;
|
|
37 |
import com.nokia.helium.scm.ant.actions.DiffAction;
|
|
38 |
import com.nokia.helium.scm.ant.actions.ExportAction;
|
|
39 |
import com.nokia.helium.scm.ant.actions.InfoAction;
|
|
40 |
import com.nokia.helium.scm.ant.actions.InitAction;
|
|
41 |
import com.nokia.helium.scm.ant.actions.PullAction;
|
|
42 |
import com.nokia.helium.scm.ant.actions.RemoveAction;
|
|
43 |
import com.nokia.helium.scm.ant.actions.ScmAction;
|
|
44 |
import com.nokia.helium.scm.ant.actions.StatusAction;
|
|
45 |
import com.nokia.helium.scm.ant.actions.TagAction;
|
|
46 |
import com.nokia.helium.scm.ant.actions.TagsAction;
|
|
47 |
import com.nokia.helium.scm.ant.actions.UpdateAction;
|
587
|
48 |
import com.nokia.maven.scm.manager.BasicExtendedScmManager;
|
|
49 |
import com.nokia.maven.scm.manager.ExtendedScmManager;
|
|
50 |
import com.nokia.maven.scm.provider.hg.HgScmProviderExt;
|
|
51 |
|
|
52 |
/**
|
|
53 |
* This provides Ant support for a range of common SCM operations. They should work on
|
|
54 |
* any Maven based SCM e.g. SVN, Mercurial, synergy. The
|
|
55 |
* underlying SCM tool is defined by the URL format.
|
|
56 |
*
|
|
57 |
* <pre>
|
|
58 |
* <hlm:scm verbose="true" scmUrl="scm:hg:${pkg_detail.source}">
|
|
59 |
* <hlm:checkout basedir="${build.drive}${pkg_detail.dst}"/>
|
|
60 |
* <hlm:tags basedir="${build.drive}${pkg_detail.dst}" reference="hg.tags.id${refid}"/>
|
|
61 |
* <hlm:update basedir="${build.drive}${pkg_detail.dst}">
|
|
62 |
* <hlm:latestTag pattern="${pkg_detail.tag}">
|
|
63 |
* <hlm:tagSet refid="hg.tags.id${refid}" />
|
|
64 |
* </hlm:latestTag>
|
|
65 |
* </hlm:update>
|
|
66 |
* </hlm:scm>
|
|
67 |
* </pre>
|
|
68 |
*
|
|
69 |
* @ant.task name="scm" category="SCM"
|
|
70 |
*/
|
|
71 |
public class ScmTask extends Task {
|
|
72 |
|
|
73 |
private ExtendedScmManager scmManager;
|
|
74 |
|
|
75 |
private String username;
|
|
76 |
|
|
77 |
private String password;
|
|
78 |
|
|
79 |
private String scmUrl;
|
|
80 |
|
|
81 |
private List<ScmAction> actions = new ArrayList<ScmAction>();
|
|
82 |
|
|
83 |
private boolean verbose;
|
|
84 |
|
|
85 |
private boolean failonerror = true;
|
|
86 |
|
|
87 |
|
|
88 |
public ScmTask() {
|
|
89 |
setTaskName("scm");
|
|
90 |
|
628
|
91 |
BasicExtendedScmManager scmManager = new BasicExtendedScmManager();
|
|
92 |
scmManager.addListener(new AntScmLogger(this));
|
|
93 |
this.scmManager = scmManager;
|
|
94 |
HgScmProviderExt provider = new HgScmProviderExt();
|
|
95 |
|
587
|
96 |
// Add all SCM providers we want to use
|
628
|
97 |
scmManager.setScmProvider("hg", provider);
|
587
|
98 |
}
|
|
99 |
|
|
100 |
public ScmManager getScmManager() {
|
|
101 |
return scmManager;
|
|
102 |
}
|
|
103 |
|
|
104 |
/**
|
|
105 |
* Set if the task should use verbose output on Maven SCM framework.
|
|
106 |
*
|
|
107 |
* @param value
|
|
108 |
* @ant.not-required Default is false
|
|
109 |
*/
|
|
110 |
public void setVerbose(boolean value) {
|
|
111 |
verbose = value;
|
|
112 |
}
|
|
113 |
|
|
114 |
/**
|
|
115 |
* Set the username, this field is required while user requires to be
|
|
116 |
* authenticated.
|
|
117 |
*
|
|
118 |
* @param username
|
|
119 |
* The user login
|
|
120 |
* @ant.not-required
|
|
121 |
*/
|
|
122 |
public void setUsername(String username) {
|
|
123 |
this.username = username;
|
|
124 |
}
|
|
125 |
|
|
126 |
/**
|
|
127 |
* Set the password, this field is required while user requires to be
|
|
128 |
* authenticated.
|
|
129 |
*
|
|
130 |
* @param password
|
|
131 |
* The user password
|
|
132 |
* @ant.not-required
|
|
133 |
*/
|
|
134 |
public void setPassword(String password) {
|
|
135 |
this.password = password;
|
|
136 |
}
|
|
137 |
|
|
138 |
/**
|
|
139 |
* Set if the build should keep going while any exception.
|
|
140 |
*
|
|
141 |
* @param failonerror
|
|
142 |
* @ant.not-required Default is true
|
|
143 |
*/
|
|
144 |
public void setFailonerror(boolean failonerror) {
|
|
145 |
this.failonerror = failonerror;
|
|
146 |
}
|
|
147 |
|
|
148 |
public String getScmUrl() {
|
|
149 |
return scmUrl;
|
|
150 |
}
|
|
151 |
|
|
152 |
/**
|
|
153 |
* Sets the SCM URL so the task knows what repository to use.
|
|
154 |
*
|
|
155 |
* @param scmUrl
|
|
156 |
* The URL to connect to
|
|
157 |
* @ant.required
|
|
158 |
*/
|
|
159 |
public void setScmUrl(String scmUrl) {
|
|
160 |
this.scmUrl = scmUrl;
|
|
161 |
}
|
|
162 |
|
|
163 |
/**
|
|
164 |
* Add an SCM add action.
|
|
165 |
*
|
|
166 |
* @param action
|
|
167 |
* The add action.
|
|
168 |
* @ant.optional
|
|
169 |
*/
|
|
170 |
public void addAdd(AddAction action) {
|
|
171 |
addAction(action);
|
|
172 |
}
|
|
173 |
|
|
174 |
public void addChangelog(ChangelogAction action) {
|
|
175 |
addAction(action);
|
|
176 |
}
|
|
177 |
|
|
178 |
public void addBranch(BranchAction action) {
|
|
179 |
addAction(action);
|
|
180 |
}
|
|
181 |
|
|
182 |
public void addCheckin(CheckinAction action) {
|
|
183 |
addAction(action);
|
|
184 |
}
|
|
185 |
|
|
186 |
public void addCheckout(CheckoutAction action) {
|
|
187 |
addAction(action);
|
|
188 |
}
|
|
189 |
|
|
190 |
public void addDiff(DiffAction action) {
|
|
191 |
addAction(action);
|
|
192 |
}
|
|
193 |
|
|
194 |
public void addRemove(RemoveAction action) {
|
|
195 |
addAction(action);
|
|
196 |
}
|
|
197 |
|
|
198 |
public void addStatus(StatusAction action) {
|
|
199 |
addAction(action);
|
|
200 |
}
|
|
201 |
|
|
202 |
public void addUpdate(UpdateAction action) {
|
|
203 |
addAction(action);
|
|
204 |
}
|
|
205 |
|
|
206 |
public ScmAction[] getActions() {
|
|
207 |
return (ScmAction[]) actions.toArray(new ScmAction[0]);
|
|
208 |
}
|
|
209 |
|
|
210 |
// Additional actions not supported by the core Maven API
|
|
211 |
|
|
212 |
public void addPull(PullAction action) {
|
|
213 |
addAction(action);
|
|
214 |
}
|
|
215 |
|
|
216 |
public void addTag(TagAction action) {
|
|
217 |
addAction(action);
|
|
218 |
}
|
|
219 |
|
|
220 |
public void addTags(TagsAction action) {
|
|
221 |
addAction(action);
|
|
222 |
}
|
|
223 |
|
|
224 |
public void addInfo(InfoAction action) {
|
|
225 |
addAction(action);
|
|
226 |
}
|
|
227 |
|
|
228 |
public void addInit(InitAction action) {
|
|
229 |
addAction(action);
|
|
230 |
}
|
|
231 |
|
|
232 |
public void addExport(ExportAction action) {
|
|
233 |
addAction(action);
|
|
234 |
}
|
|
235 |
|
|
236 |
private void addAction(ScmAction action) {
|
|
237 |
action.setTask(this);
|
|
238 |
actions.add(action);
|
|
239 |
}
|
|
240 |
|
|
241 |
public void add(ScmAction action) {
|
|
242 |
addAction(action);
|
|
243 |
}
|
|
244 |
|
|
245 |
@Override
|
|
246 |
public final void execute() {
|
|
247 |
try {
|
|
248 |
log("scm url: " + scmUrl);
|
|
249 |
ScmRepository repository = scmManager.makeScmRepository(scmUrl);
|
|
250 |
// Process all scm commands
|
|
251 |
for (ScmAction action : actions) {
|
|
252 |
action.execute(repository);
|
|
253 |
}
|
|
254 |
} catch (NoSuchScmProviderException ex) {
|
|
255 |
throw new BuildException("Could not find a provider.");
|
|
256 |
} catch (ScmRepositoryException ex) {
|
|
257 |
throw new BuildException(
|
|
258 |
"Error while connecting to the repository: " + ex.getMessage());
|
|
259 |
} catch (BuildException ex) { //need to do this because the TagAction
|
|
260 |
// execute catches the ScmException and throws a BuildException
|
|
261 |
if ( failonerror ) {
|
|
262 |
ex.printStackTrace();
|
|
263 |
throw new BuildException(ex.getMessage());
|
|
264 |
}
|
|
265 |
else {
|
|
266 |
ex.printStackTrace();
|
|
267 |
}
|
|
268 |
} catch (ScmException ex) {
|
|
269 |
if ( failonerror ) {
|
|
270 |
ex.printStackTrace();
|
|
271 |
throw new BuildException(ex.getMessage());
|
|
272 |
}
|
|
273 |
else {
|
|
274 |
ex.printStackTrace();
|
|
275 |
}
|
|
276 |
}
|
|
277 |
}
|
|
278 |
}
|