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 java.io.IOException;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24
25 import org.apache.maven.plugin.MojoExecutionException;
26 import org.apache.maven.plugins.annotations.Mojo;
27 import org.apache.maven.plugins.annotations.Parameter;
28 import org.apache.maven.scm.ScmException;
29 import org.apache.maven.scm.ScmTagParameters;
30 import org.apache.maven.scm.command.tag.TagScmResult;
31 import org.apache.maven.scm.provider.ScmProvider;
32 import org.apache.maven.scm.repository.ScmRepository;
33
34
35
36
37
38
39
40 @Mojo(name = "tag", aggregator = true)
41 public class TagMojo extends AbstractScmMojo {
42
43
44
45 @Parameter(property = "tag", required = true)
46 private String tag;
47
48
49
50
51 @Parameter(property = "message")
52 private String message;
53
54
55
56
57 @Parameter(property = "timestampFormat", defaultValue = "yyyyMMddHHmmss")
58 private String timestampFormat;
59
60
61
62
63 @Parameter(property = "addTimestamp", defaultValue = "false")
64 private boolean addTimestamp;
65
66
67
68
69 @Parameter(property = "timestampPosition", defaultValue = "end")
70 private String timestampPosition;
71
72
73
74
75 @Parameter(property = "timestampPrefix", defaultValue = "-")
76 private String timestampPrefix;
77
78
79
80
81
82
83
84 @Parameter(property = "remoteTagging", defaultValue = "true")
85 private boolean remoteTagging;
86
87
88
89
90
91
92
93
94
95 @Parameter(property = "pinExternals", defaultValue = "false")
96 private boolean pinExternals;
97
98
99
100
101
102
103 @Parameter(property = "sign", defaultValue = "false")
104 private boolean sign;
105
106
107 public void execute() throws MojoExecutionException {
108 super.execute();
109
110 try {
111 SimpleDateFormat dateFormat = null;
112 String tagTimestamp = "";
113 String finalTag = tag;
114
115 if (addTimestamp) {
116 try {
117 getLog().info("Using timestamp pattern '" + timestampFormat + "'");
118 dateFormat = new SimpleDateFormat(timestampFormat);
119 tagTimestamp = dateFormat.format(new Date());
120 getLog().info("Using timestamp '" + tagTimestamp + "'");
121 } catch (IllegalArgumentException e) {
122 String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
123 getLog().error(msg, e);
124 throw new MojoExecutionException(msg, e);
125 }
126
127 if ("end".equals(timestampPosition)) {
128 finalTag += timestampPrefix + tagTimestamp;
129 } else {
130 finalTag = tagTimestamp + timestampPrefix + finalTag;
131 }
132 }
133
134 ScmRepository repository = getScmRepository();
135 ScmProvider provider = getScmManager().getProviderByRepository(repository);
136
137 finalTag = provider.sanitizeTagName(finalTag);
138 getLog().info("Final Tag Name: '" + finalTag + "'");
139
140 ScmTagParameters scmTagParameters = new ScmTagParameters(message);
141 scmTagParameters.setRemoteTagging(remoteTagging);
142 scmTagParameters.setPinExternals(pinExternals);
143 scmTagParameters.setSign(sign);
144
145 TagScmResult result = provider.tag(repository, getFileSet(), finalTag, scmTagParameters);
146
147 checkResult(result);
148 } catch (IOException | ScmException e) {
149 throw new MojoExecutionException("Cannot run tag command : ", e);
150 }
151 }
152 }