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 (http://jira.codehaus.org/browse/SCM-406)
084     *      
085     * @since 1.2
086     */    
087    @Parameter( property = "remoteTagging", defaultValue = "true" )
088    private boolean remoteTagging;    
089
090    /** {@inheritDoc} */
091    public void execute()
092        throws MojoExecutionException
093    {
094        super.execute();
095
096        try
097        {
098            SimpleDateFormat dateFormat = null;
099            String tagTimestamp = "";
100            String finalTag = tag;
101
102            if ( addTimestamp )
103            {
104                try
105                {
106                    getLog().info( "Using timestamp pattern '" + timestampFormat + "'" );
107                    dateFormat = new SimpleDateFormat( timestampFormat );
108                    tagTimestamp = dateFormat.format( new Date() );
109                    getLog().info( "Using timestamp '" + tagTimestamp + "'" );
110                }
111                catch ( IllegalArgumentException e )
112                {
113                    String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
114                    getLog().error( msg, e );
115                    throw new MojoExecutionException( msg, e );
116                }
117
118                if ( "end".equals( timestampPosition ) )
119                {
120                    finalTag += timestampPrefix + tagTimestamp;
121                }
122                else
123                {
124                    finalTag = tagTimestamp + timestampPrefix + finalTag;
125                }
126            }
127
128            ScmRepository repository = getScmRepository();
129            ScmProvider provider = getScmManager().getProviderByRepository( repository );
130
131            finalTag = provider.sanitizeTagName( finalTag );
132            getLog().info( "Final Tag Name: '" + finalTag + "'" );
133
134            ScmTagParameters scmTagParameters = new ScmTagParameters( message);
135            scmTagParameters.setRemoteTagging( remoteTagging );
136            
137            TagScmResult result = provider.tag( repository, getFileSet(), finalTag, scmTagParameters);
138
139            checkResult( result );
140        }
141        catch ( IOException e )
142        {
143            throw new MojoExecutionException( "Cannot run tag command : ", e );
144        }
145        catch ( ScmException e )
146        {
147            throw new MojoExecutionException( "Cannot run tag command : ", e );
148        }
149    }
150}