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