View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.plugin;
20  
21  import javax.inject.Inject;
22  
23  import java.io.IOException;
24  import java.text.SimpleDateFormat;
25  import java.util.Date;
26  
27  import org.apache.maven.plugin.MojoExecutionException;
28  import org.apache.maven.plugins.annotations.Mojo;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.scm.CommandParameters.SignOption;
31  import org.apache.maven.scm.ScmException;
32  import org.apache.maven.scm.ScmTagParameters;
33  import org.apache.maven.scm.command.tag.TagScmResult;
34  import org.apache.maven.scm.manager.ScmManager;
35  import org.apache.maven.scm.provider.ScmProvider;
36  import org.apache.maven.scm.repository.ScmRepository;
37  import org.apache.maven.settings.crypto.SettingsDecrypter;
38  
39  /**
40   * Tag the project.
41   *
42   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
43   * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
44   */
45  @Mojo(name = "tag", aggregator = true)
46  public class TagMojo extends AbstractScmMojo {
47      /**
48       * The tag name.
49       */
50      @Parameter(property = "tag", required = true)
51      private String tag;
52  
53      /**
54       * The message applied to the tag creation.
55       */
56      @Parameter(property = "message")
57      private String message;
58  
59      /**
60       * Set the timestamp format.
61       */
62      @Parameter(property = "timestampFormat", defaultValue = "yyyyMMddHHmmss")
63      private String timestampFormat;
64  
65      /**
66       * Use timestamp tagging.
67       */
68      @Parameter(property = "addTimestamp", defaultValue = "false")
69      private boolean addTimestamp;
70  
71      /**
72       * Define the timestamp position (end or begin).
73       */
74      @Parameter(property = "timestampPosition", defaultValue = "end")
75      private String timestampPosition;
76  
77      /**
78       * Timestamp tag prefix.
79       */
80      @Parameter(property = "timestampPrefix", defaultValue = "-")
81      private String timestampPrefix;
82  
83      /**
84       * Currently only implemented with svn scm. Enable a workaround to prevent issue
85       * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
86       *
87       * @since 1.2
88       */
89      @Parameter(property = "remoteTagging", defaultValue = "true")
90      private boolean remoteTagging;
91  
92      /**
93       * Currently only implemented with Subversion. Enable the "--pin-externals"
94       * option in svn copy commands which is new in Subversion 1.9.
95       *
96       * @see https://subversion.apache.org/docs/release-notes/1.9.html
97       * @since 1.11.0
98       */
99      @Parameter(property = "pinExternals", defaultValue = "false")
100     private boolean pinExternals;
101 
102     /**
103      * Enable the "--sign" in Git.
104      * Same as {@link #signOption} set to either {@link SignOption#FORCE_SIGN} or {@link SignOption#DEFAULT}.
105      *
106      * @since 1.11.0
107      * @deprecated since 2.2.1, use {@link #signOption} instead
108      */
109     @Parameter(property = "sign", defaultValue = "false")
110     private boolean sign;
111 
112     /**
113      * Toggles the signing for the tag command (only applicable to SCMs that support signing).
114      *
115      * @since 2.2.1
116      */
117     @Parameter(property = "signOption")
118     private SignOption signOption = SignOption.DEFAULT;
119 
120     @Inject
121     public TagMojo(ScmManager manager, SettingsDecrypter settingsDecrypter) {
122         super(manager, settingsDecrypter);
123     }
124 
125     /**
126      * {@inheritDoc}
127      */
128     public void execute() throws MojoExecutionException {
129         super.execute();
130 
131         try {
132             SimpleDateFormat dateFormat = null;
133             String tagTimestamp = "";
134             String finalTag = tag;
135 
136             if (addTimestamp) {
137                 try {
138                     getLog().debug("Using timestamp pattern '" + timestampFormat + "'");
139                     dateFormat = new SimpleDateFormat(timestampFormat);
140                     tagTimestamp = dateFormat.format(new Date());
141                     getLog().debug("Using timestamp '" + tagTimestamp + "'");
142                 } catch (IllegalArgumentException e) {
143                     String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
144                     throw new MojoExecutionException(msg, e);
145                 }
146 
147                 if ("end".equals(timestampPosition)) {
148                     finalTag += timestampPrefix + tagTimestamp;
149                 } else {
150                     finalTag = tagTimestamp + timestampPrefix + finalTag;
151                 }
152             }
153 
154             ScmRepository repository = getScmRepository();
155             ScmProvider provider = getScmManager().getProviderByRepository(repository);
156 
157             finalTag = provider.sanitizeTagName(finalTag);
158             getLog().debug("Final Tag Name: '" + finalTag + "'");
159 
160             ScmTagParameters scmTagParameters = new ScmTagParameters(message);
161             scmTagParameters.setRemoteTagging(remoteTagging);
162             scmTagParameters.setPinExternals(pinExternals);
163             if (signOption != null) {
164                 scmTagParameters.setSignOption(signOption);
165             } else if (sign) {
166                 getLog().warn("The 'sign' parameter is deprecated, use 'signOption' instead.");
167                 scmTagParameters.setSign(sign);
168             }
169 
170             TagScmResult result = provider.tag(repository, getFileSet(), finalTag, scmTagParameters);
171 
172             checkResult(result);
173         } catch (IOException | ScmException e) {
174             throw new MojoExecutionException("Cannot run tag command : ", e);
175         }
176     }
177 }