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