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 java.io.IOException;
022import java.text.SimpleDateFormat;
023import java.util.Date;
024
025import org.apache.maven.plugin.MojoExecutionException;
026import org.apache.maven.plugins.annotations.Mojo;
027import org.apache.maven.plugins.annotations.Parameter;
028import org.apache.maven.scm.ScmException;
029import org.apache.maven.scm.ScmTagParameters;
030import org.apache.maven.scm.command.tag.TagScmResult;
031import org.apache.maven.scm.provider.ScmProvider;
032import org.apache.maven.scm.repository.ScmRepository;
033
034/**
035 * Tag the project.
036 *
037 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
038 * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
039 */
040@Mojo(name = "tag", aggregator = true)
041public class TagMojo extends AbstractScmMojo {
042    /**
043     * The tag name.
044     */
045    @Parameter(property = "tag", required = true)
046    private String tag;
047
048    /**
049     * The message applied to the tag creation.
050     */
051    @Parameter(property = "message")
052    private String message;
053
054    /**
055     * Set the timestamp format.
056     */
057    @Parameter(property = "timestampFormat", defaultValue = "yyyyMMddHHmmss")
058    private String timestampFormat;
059
060    /**
061     * Use timestamp tagging.
062     */
063    @Parameter(property = "addTimestamp", defaultValue = "false")
064    private boolean addTimestamp;
065
066    /**
067     * Define the timestamp position (end or begin).
068     */
069    @Parameter(property = "timestampPosition", defaultValue = "end")
070    private String timestampPosition;
071
072    /**
073     * Timestamp tag prefix.
074     */
075    @Parameter(property = "timestampPrefix", defaultValue = "-")
076    private String timestampPrefix;
077
078    /**
079     * currently only implemented with svn scm. Enable a workaround to prevent issue
080     * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
081     *
082     * @since 1.2
083     */
084    @Parameter(property = "remoteTagging", defaultValue = "true")
085    private boolean remoteTagging;
086
087    /**
088     * Currently only implemented with Subversion. Enable the "--pin-externals"
089     * option in svn copy commands which is new in Subversion 1.9.
090     *
091     * @since 1.11.0
092     *
093     * @see https://subversion.apache.org/docs/release-notes/1.9.html
094     */
095    @Parameter(property = "pinExternals", defaultValue = "false")
096    private boolean pinExternals;
097
098    /**
099     * 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}