1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.client.cli;
20
21 import java.io.File;
22 import java.util.List;
23
24 import org.apache.maven.scm.ScmBranch;
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFile;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmResult;
29 import org.apache.maven.scm.ScmRevision;
30 import org.apache.maven.scm.ScmTag;
31 import org.apache.maven.scm.ScmVersion;
32 import org.apache.maven.scm.command.checkin.CheckInScmResult;
33 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
34 import org.apache.maven.scm.command.update.UpdateScmResult;
35 import org.apache.maven.scm.manager.NoSuchScmProviderException;
36 import org.apache.maven.scm.manager.ScmManager;
37 import org.apache.maven.scm.repository.ScmRepository;
38 import org.apache.maven.scm.repository.ScmRepositoryException;
39 import org.codehaus.plexus.ContainerConfiguration;
40 import org.codehaus.plexus.DefaultContainerConfiguration;
41 import org.codehaus.plexus.DefaultPlexusContainer;
42 import org.codehaus.plexus.PlexusConstants;
43 import org.codehaus.plexus.PlexusContainer;
44 import org.codehaus.plexus.PlexusContainerException;
45 import org.codehaus.plexus.context.Context;
46 import org.codehaus.plexus.context.DefaultContext;
47
48
49
50
51
52 public class MavenScmCli {
53 private PlexusContainer plexus;
54
55 private ScmManager scmManager;
56
57
58
59
60
61 public MavenScmCli() throws Exception {
62 plexus = createPlexusContainer();
63 scmManager = plexus.lookup(ScmManager.class);
64 }
65
66 private PlexusContainer createPlexusContainer() {
67 final Context context = new DefaultContext();
68 String path = System.getProperty("basedir");
69 if (path == null) {
70 path = new File("").getAbsolutePath();
71 }
72 context.put("basedir", path);
73
74 ContainerConfiguration plexusConfiguration = new DefaultContainerConfiguration();
75 plexusConfiguration
76 .setName("maven-scm-cli")
77 .setContext(context.getContextData())
78 .setClassPathScanning(PlexusConstants.SCANNING_CACHE)
79 .setAutoWiring(true);
80 try {
81 return new DefaultPlexusContainer(plexusConfiguration);
82 } catch (PlexusContainerException e) {
83 throw new IllegalStateException("Could not create Plexus container", e);
84 }
85 }
86
87 public void stop() {
88 try {
89 plexus.dispose();
90 } catch (RuntimeException ex) {
91
92 }
93 }
94
95
96
97
98
99 public static void main(String[] args) {
100 MavenScmCli cli;
101
102 try {
103 cli = new MavenScmCli();
104 } catch (Exception ex) {
105 System.err.println("Error while starting Maven SCM.");
106
107 ex.printStackTrace(System.err);
108
109 return;
110 }
111
112 String scmUrl;
113
114 String command;
115
116 if (args.length < 3) {
117 System.err.println(
118 "Usage: maven-scm-client <command> <working directory> <scm url> [<scmVersion> [<scmVersionType>]]");
119 System.err.println("scmVersion is a branch name/tag name/revision number.");
120 System.err.println(
121 "scmVersionType can be 'branch', 'tag', 'revision'. " + "The default value is 'revision'.");
122
123 return;
124 }
125
126 command = args[0];
127
128
129 File workingDirectory = new File(args[1]).getAbsoluteFile();
130
131 scmUrl = args[2];
132
133 ScmVersion scmVersion = null;
134 if (args.length > 3) {
135 String version = args[3];
136
137 if (args.length > 4) {
138 String type = args[4];
139
140 if ("tag".equals(type)) {
141 scmVersion = new ScmTag(version);
142 } else if ("branch".equals(type)) {
143 scmVersion = new ScmBranch(version);
144 } else if ("revision".equals(type)) {
145 scmVersion = new ScmRevision(version);
146 } else {
147 throw new IllegalArgumentException("'" + type + "' version type isn't known.");
148 }
149 } else {
150 scmVersion = new ScmRevision(args[3]);
151 }
152 }
153
154 cli.execute(scmUrl, command, workingDirectory, scmVersion);
155
156 cli.stop();
157 }
158
159
160
161
162
163 public void execute(String scmUrl, String command, File workingDirectory, ScmVersion version) {
164 ScmRepository repository;
165
166 try {
167 repository = scmManager.makeScmRepository(scmUrl);
168 } catch (NoSuchScmProviderException ex) {
169 System.err.println("Could not find a provider.");
170
171 return;
172 } catch (ScmRepositoryException ex) {
173 System.err.println("Error while connecting to the repository");
174
175 ex.printStackTrace(System.err);
176
177 return;
178 }
179
180 try {
181 if (command.equals("checkout")) {
182 checkOut(repository, workingDirectory, version);
183 } else if (command.equals("checkin")) {
184 checkIn(repository, workingDirectory, version);
185 } else if (command.equals("update")) {
186 update(repository, workingDirectory, version);
187 } else {
188 System.err.println("Unknown SCM command '" + command + "'.");
189 }
190 } catch (ScmException ex) {
191 System.err.println("Error while executing the SCM command.");
192
193 ex.printStackTrace(System.err);
194 }
195 }
196
197
198
199
200
201 private void checkOut(ScmRepository scmRepository, File workingDirectory, ScmVersion version) throws ScmException {
202 if (workingDirectory.exists()) {
203 System.err.println("The working directory already exist: '" + workingDirectory.getAbsolutePath() + "'.");
204
205 return;
206 }
207
208 if (!workingDirectory.mkdirs()) {
209 System.err.println(
210 "Error while making the working directory: '" + workingDirectory.getAbsolutePath() + "'.");
211
212 return;
213 }
214
215 CheckOutScmResult result = scmManager.checkOut(scmRepository, new ScmFileSet(workingDirectory), version);
216
217 if (!result.isSuccess()) {
218 showError(result);
219
220 return;
221 }
222
223 List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
224
225 System.out.println("Checked out these files: ");
226
227 for (ScmFile file : checkedOutFiles) {
228 System.out.println(" " + file.getPath());
229 }
230 }
231
232 private void checkIn(ScmRepository scmRepository, File workingDirectory, ScmVersion version) throws ScmException {
233 if (!workingDirectory.exists()) {
234 System.err.println("The working directory doesn't exist: '" + workingDirectory.getAbsolutePath() + "'.");
235
236 return;
237 }
238
239 String message = "";
240
241 CheckInScmResult result = scmManager.checkIn(scmRepository, new ScmFileSet(workingDirectory), version, message);
242
243 if (!result.isSuccess()) {
244 showError(result);
245
246 return;
247 }
248
249 List<ScmFile> checkedInFiles = result.getCheckedInFiles();
250
251 System.out.println("Checked in these files: ");
252
253 for (ScmFile file : checkedInFiles) {
254 System.out.println(" " + file.getPath());
255 }
256 }
257
258 private void update(ScmRepository scmRepository, File workingDirectory, ScmVersion version) throws ScmException {
259 if (!workingDirectory.exists()) {
260 System.err.println("The working directory doesn't exist: '" + workingDirectory.getAbsolutePath() + "'.");
261
262 return;
263 }
264
265 UpdateScmResult result = scmManager.update(scmRepository, new ScmFileSet(workingDirectory), version);
266
267 if (!result.isSuccess()) {
268 showError(result);
269
270 return;
271 }
272
273 List<ScmFile> updatedFiles = result.getUpdatedFiles();
274
275 System.out.println("Updated these files: ");
276
277 for (ScmFile file : updatedFiles) {
278 System.out.println(" " + file.getPath());
279 }
280 }
281
282
283
284
285
286 private void showError(ScmResult result) {
287 System.err.println("There was a error while executing the SCM command.");
288
289 String providerMessage = result.getProviderMessage();
290
291 if (!(providerMessage == null || providerMessage.isEmpty())) {
292 System.err.println("Error message from the provider: " + providerMessage);
293 } else {
294 System.err.println("The provider didn't give a error message.");
295 }
296
297 String output = result.getCommandOutput();
298
299 if (!(output == null || output.isEmpty())) {
300 System.err.println("Command output:");
301
302 System.err.println(output);
303 }
304 }
305 }