1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.plugin;
20
21 import javax.inject.Inject;
22
23 import java.io.File;
24 import java.io.IOException;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugins.annotations.Mojo;
29 import org.apache.maven.scm.ScmException;
30 import org.apache.maven.scm.ScmFile;
31 import org.apache.maven.scm.command.status.StatusScmResult;
32 import org.apache.maven.scm.manager.ScmManager;
33 import org.apache.maven.scm.repository.ScmRepository;
34 import org.apache.maven.settings.crypto.SettingsDecrypter;
35
36
37
38
39
40
41
42 @Mojo(name = "status", aggregator = true)
43 public class StatusMojo extends AbstractScmMojo {
44
45 @Inject
46 public StatusMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
47 super(manager, settingsDecrypter);
48 }
49
50
51
52
53 public void execute() throws MojoExecutionException {
54 super.execute();
55
56 try {
57 ScmRepository repository = getScmRepository();
58
59 StatusScmResult result = getScmManager().status(repository, getFileSet());
60
61 checkResult(result);
62
63 File baseDir = getFileSet().getBasedir();
64
65
66 int maxLen = 0;
67
68 for (ScmFile file : result.getChangedFiles()) {
69 maxLen = Math.max(maxLen, file.getStatus().toString().length());
70 }
71
72 for (ScmFile file : result.getChangedFiles()) {
73
74 getLog().info(StringUtils.leftPad(file.getStatus().toString(), maxLen) + " status for "
75 + getRelativePath(baseDir, file.getPath()));
76 }
77 } catch (IOException | ScmException e) {
78 throw new MojoExecutionException("Cannot run status command : ", e);
79 }
80 }
81
82
83
84
85
86
87
88
89 protected String getRelativePath(File baseDir, String path) {
90 if (path.equals(baseDir.getAbsolutePath())) {
91 return ".";
92 } else if (path.indexOf(baseDir.getAbsolutePath()) == 0) {
93
94 return path.substring(baseDir.getAbsolutePath().length() + 1);
95 } else {
96 return path;
97 }
98 }
99 }