001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.plugin;
020
021import javax.inject.Inject;
022
023import java.io.IOException;
024import java.text.SimpleDateFormat;
025import java.util.Date;
026
027import org.apache.maven.plugin.MojoExecutionException;
028import org.apache.maven.plugins.annotations.Mojo;
029import org.apache.maven.plugins.annotations.Parameter;
030import org.apache.maven.scm.CommandParameters.SignOption;
031import org.apache.maven.scm.ScmException;
032import org.apache.maven.scm.ScmTagParameters;
033import org.apache.maven.scm.command.tag.TagScmResult;
034import org.apache.maven.scm.manager.ScmManager;
035import org.apache.maven.scm.provider.ScmProvider;
036import org.apache.maven.scm.repository.ScmRepository;
037import org.apache.maven.settings.crypto.SettingsDecrypter;
038
039/**
040 * Tag the project.
041 *
042 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
043 * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
044 */
045@Mojo(name = "tag", aggregator = true)
046public class TagMojo extends AbstractScmMojo {
047    /**
048     * The tag name.
049     */
050    @Parameter(property = "tag", required = true)
051    private String tag;
052
053    /**
054     * The message applied to the tag creation.
055     */
056    @Parameter(property = "message")
057    private String message;
058
059    /**
060     * Set the timestamp format.
061     */
062    @Parameter(property = "timestampFormat", defaultValue = "yyyyMMddHHmmss")
063    private String timestampFormat;
064
065    /**
066     * Use timestamp tagging.
067     */
068    @Parameter(property = "addTimestamp", defaultValue = "false")
069    private boolean addTimestamp;
070
071    /**
072     * Define the timestamp position (end or begin).
073     */
074    @Parameter(property = "timestampPosition", defaultValue = "end")
075    private String timestampPosition;
076
077    /**
078     * Timestamp tag prefix.
079     */
080    @Parameter(property = "timestampPrefix", defaultValue = "-")
081    private String timestampPrefix;
082
083    /**
084     * Currently only implemented with svn scm. Enable a workaround to prevent issue
085     * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
086     *
087     * @since 1.2
088     */
089    @Parameter(property = "remoteTagging", defaultValue = "true")
090    private boolean remoteTagging;
091
092    /**
093     * Currently only implemented with Subversion. Enable the "--pin-externals"
094     * option in svn copy commands which is new in Subversion 1.9.
095     *
096     * @see https://subversion.apache.org/docs/release-notes/1.9.html
097     * @since 1.11.0
098     */
099    @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}