001package org.apache.maven.scm.plugin;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.plugin.MojoExecutionException;
023import org.apache.maven.plugins.annotations.Mojo;
024import org.apache.maven.plugins.annotations.Parameter;
025import org.apache.maven.scm.ScmException;
026import org.apache.maven.scm.ScmTagParameters;
027import org.apache.maven.scm.command.tag.TagScmResult;
028import org.apache.maven.scm.provider.ScmProvider;
029import org.apache.maven.scm.repository.ScmRepository;
030
031import java.io.IOException;
032import java.text.SimpleDateFormat;
033import java.util.Date;
034
035/**
036 * Tag the project.
037 *
038 * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
039 * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
040 */
041@Mojo( name = "tag", aggregator = true )
042public class TagMojo
043    extends AbstractScmMojo
044{
045    /**
046     * The tag name.
047     */
048    @Parameter( property = "tag", required = true )
049    private String tag;
050
051    /**
052     * The message applied to the tag creation.
053     */
054    @Parameter( property = "message" )
055    private String message;
056
057    /**
058     * Set the timestamp format.
059     */
060    @Parameter( property = "timestampFormat", defaultValue = "yyyyMMddHHmmss" )
061    private String timestampFormat;
062
063    /**
064     * Use timestamp tagging.
065     */
066    @Parameter( property = "addTimestamp", defaultValue = "false" )
067    private boolean addTimestamp;
068
069    /**
070     * Define the timestamp position (end or begin).
071     */
072    @Parameter( property = "timestampPosition", defaultValue = "end" )
073    private String timestampPosition;
074
075    /**
076     * Timestamp tag prefix.
077     */
078    @Parameter( property = "timestampPrefix", defaultValue = "-" )
079    private String timestampPrefix;
080    
081    /**
082     * currently only implemented with svn scm. Enable a workaround to prevent issue 
083     * due to svn client > 1.5.0 (https://issues.apache.org/jira/browse/SCM-406)
084     *      
085     * @since 1.2
086     */    
087    @Parameter( property = "remoteTagging", defaultValue = "true" )
088    private boolean remoteTagging;    
089
090    /**
091     * Currently only implemented with Subversion. Enable the "--pin-externals"
092     * option in svn copy commands which is new in Subversion 1.9.
093     *
094     * @since 1.11.0
095     *
096     * @see https://subversion.apache.org/docs/release-notes/1.9.html
097     */
098    @Parameter( property = "pinExternals", defaultValue = "false" )
099    private boolean pinExternals;
100
101    /**
102     * Enable the "--sign" in Git
103     *
104     * @since 1.11.0
105     */
106    @Parameter( property = "sign", defaultValue = "false" )
107    private boolean sign;
108
109    /** {@inheritDoc} */
110    public void execute()
111        throws MojoExecutionException
112    {
113        super.execute();
114
115        try
116        {
117            SimpleDateFormat dateFormat = null;
118            String tagTimestamp = "";
119            String finalTag = tag;
120
121            if ( addTimestamp )
122            {
123                try
124                {
125                    getLog().info( "Using timestamp pattern '" + timestampFormat + "'" );
126                    dateFormat = new SimpleDateFormat( timestampFormat );
127                    tagTimestamp = dateFormat.format( new Date() );
128                    getLog().info( "Using timestamp '" + tagTimestamp + "'" );
129                }
130                catch ( IllegalArgumentException e )
131                {
132                    String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
133                    getLog().error( msg, e );
134                    throw new MojoExecutionException( msg, e );
135                }
136
137                if ( "end".equals( timestampPosition ) )
138                {
139                    finalTag += timestampPrefix + tagTimestamp;
140                }
141                else
142                {
143                    finalTag = tagTimestamp + timestampPrefix + finalTag;
144                }
145            }
146
147            ScmRepository repository = getScmRepository();
148            ScmProvider provider = getScmManager().getProviderByRepository( repository );
149
150            finalTag = provider.sanitizeTagName( finalTag );
151            getLog().info( "Final Tag Name: '" + finalTag + "'" );
152
153            ScmTagParameters scmTagParameters = new ScmTagParameters( message );
154            scmTagParameters.setRemoteTagging( remoteTagging );
155            scmTagParameters.setPinExternals( pinExternals );
156            scmTagParameters.setSign( sign );
157
158            TagScmResult result = provider.tag( repository, getFileSet(), finalTag, scmTagParameters );
159
160            checkResult( result );
161        }
162        catch ( IOException | ScmException e )
163        {
164            throw new MojoExecutionException( "Cannot run tag command : ", e );
165        }
166    }
167}