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 java.io.IOException;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugins.annotations.Mojo;
27  import org.apache.maven.plugins.annotations.Parameter;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmTagParameters;
30  import org.apache.maven.scm.command.tag.TagScmResult;
31  import org.apache.maven.scm.provider.ScmProvider;
32  import org.apache.maven.scm.repository.ScmRepository;
33  
34  /**
35   * Tag the project.
36   *
37   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
38   * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
39   */
40  @Mojo(name = "tag", aggregator = true)
41  public class TagMojo extends AbstractScmMojo {
42      /**
43       * The tag name.
44       */
45      @Parameter(property = "tag", required = true)
46      private String tag;
47  
48      /**
49       * The message applied to the tag creation.
50       */
51      @Parameter(property = "message")
52      private String message;
53  
54      /**
55       * Set the timestamp format.
56       */
57      @Parameter(property = "timestampFormat", defaultValue = "yyyyMMddHHmmss")
58      private String timestampFormat;
59  
60      /**
61       * Use timestamp tagging.
62       */
63      @Parameter(property = "addTimestamp", defaultValue = "false")
64      private boolean addTimestamp;
65  
66      /**
67       * Define the timestamp position (end or begin).
68       */
69      @Parameter(property = "timestampPosition", defaultValue = "end")
70      private String timestampPosition;
71  
72      /**
73       * Timestamp tag prefix.
74       */
75      @Parameter(property = "timestampPrefix", defaultValue = "-")
76      private String timestampPrefix;
77  
78      /**
79       * currently only implemented with svn scm. Enable a workaround to prevent issue
80       * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
81       *
82       * @since 1.2
83       */
84      @Parameter(property = "remoteTagging", defaultValue = "true")
85      private boolean remoteTagging;
86  
87      /**
88       * Currently only implemented with Subversion. Enable the "--pin-externals"
89       * option in svn copy commands which is new in Subversion 1.9.
90       *
91       * @since 1.11.0
92       *
93       * @see https://subversion.apache.org/docs/release-notes/1.9.html
94       */
95      @Parameter(property = "pinExternals", defaultValue = "false")
96      private boolean pinExternals;
97  
98      /**
99       * Enable the "--sign" in Git
100      *
101      * @since 1.11.0
102      */
103     @Parameter(property = "sign", defaultValue = "false")
104     private boolean sign;
105 
106     /** {@inheritDoc} */
107     public void execute() throws MojoExecutionException {
108         super.execute();
109 
110         try {
111             SimpleDateFormat dateFormat = null;
112             String tagTimestamp = "";
113             String finalTag = tag;
114 
115             if (addTimestamp) {
116                 try {
117                     getLog().info("Using timestamp pattern '" + timestampFormat + "'");
118                     dateFormat = new SimpleDateFormat(timestampFormat);
119                     tagTimestamp = dateFormat.format(new Date());
120                     getLog().info("Using timestamp '" + tagTimestamp + "'");
121                 } catch (IllegalArgumentException e) {
122                     String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
123                     getLog().error(msg, e);
124                     throw new MojoExecutionException(msg, e);
125                 }
126 
127                 if ("end".equals(timestampPosition)) {
128                     finalTag += timestampPrefix + tagTimestamp;
129                 } else {
130                     finalTag = tagTimestamp + timestampPrefix + finalTag;
131                 }
132             }
133 
134             ScmRepository repository = getScmRepository();
135             ScmProvider provider = getScmManager().getProviderByRepository(repository);
136 
137             finalTag = provider.sanitizeTagName(finalTag);
138             getLog().info("Final Tag Name: '" + finalTag + "'");
139 
140             ScmTagParameters scmTagParameters = new ScmTagParameters(message);
141             scmTagParameters.setRemoteTagging(remoteTagging);
142             scmTagParameters.setPinExternals(pinExternals);
143             scmTagParameters.setSign(sign);
144 
145             TagScmResult result = provider.tag(repository, getFileSet(), finalTag, scmTagParameters);
146 
147             checkResult(result);
148         } catch (IOException | ScmException e) {
149             throw new MojoExecutionException("Cannot run tag command : ", e);
150         }
151     }
152 }