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