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 public void execute()
92 throws MojoExecutionException
93 {
94 super.execute();
95
96 try
97 {
98 SimpleDateFormat dateFormat = null;
99 String tagTimestamp = "";
100 String finalTag = tag;
101
102 if ( addTimestamp )
103 {
104 try
105 {
106 getLog().info( "Using timestamp pattern '" + timestampFormat + "'" );
107 dateFormat = new SimpleDateFormat( timestampFormat );
108 tagTimestamp = dateFormat.format( new Date() );
109 getLog().info( "Using timestamp '" + tagTimestamp + "'" );
110 }
111 catch ( IllegalArgumentException e )
112 {
113 String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
114 getLog().error( msg, e );
115 throw new MojoExecutionException( msg, e );
116 }
117
118 if ( "end".equals( timestampPosition ) )
119 {
120 finalTag += timestampPrefix + tagTimestamp;
121 }
122 else
123 {
124 finalTag = tagTimestamp + timestampPrefix + finalTag;
125 }
126 }
127
128 ScmRepository repository = getScmRepository();
129 ScmProvider provider = getScmManager().getProviderByRepository( repository );
130
131 finalTag = provider.sanitizeTagName( finalTag );
132 getLog().info( "Final Tag Name: '" + finalTag + "'" );
133
134 ScmTagParameters scmTagParameters = new ScmTagParameters( message);
135 scmTagParameters.setRemoteTagging( remoteTagging );
136
137 TagScmResult result = provider.tag( repository, getFileSet(), finalTag, scmTagParameters);
138
139 checkResult( result );
140 }
141 catch ( IOException e )
142 {
143 throw new MojoExecutionException( "Cannot run tag command : ", e );
144 }
145 catch ( ScmException e )
146 {
147 throw new MojoExecutionException( "Cannot run tag command : ", e );
148 }
149 }
150 }