001    package 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    
022    import org.apache.maven.plugin.MojoExecutionException;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmTagParameters;
025    import org.apache.maven.scm.command.tag.TagScmResult;
026    import org.apache.maven.scm.provider.ScmProvider;
027    import org.apache.maven.scm.repository.ScmRepository;
028    
029    import java.io.IOException;
030    import java.text.SimpleDateFormat;
031    import java.util.Date;
032    
033    /**
034     * Tag the project.
035     *
036     * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
037     * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
038     * @version $Id: TagMojo.java 757186 2009-03-22 13:23:00Z olamy $
039     * @goal tag
040     * @aggregator
041     */
042    public class TagMojo
043        extends AbstractScmMojo
044    {
045        /**
046         * The tag name.
047         *
048         * @parameter expression="${tag}"
049         * @required
050         */
051        private String tag;
052    
053        /**
054         * The message applied to the tag creation.
055         *
056         * @parameter expression="${message}"
057         */
058        private String message;
059    
060        /**
061         * Set the timestamp format.
062         *
063         * @parameter expression="${timestampFormat}" default-value="yyyyMMddHHmmss"
064         */
065        private String timestampFormat;
066    
067        /**
068         * Use timestamp tagging.
069         *
070         * @parameter expression="${addTimestamp}" default-value="false"
071         */
072        private boolean addTimestamp;
073    
074        /**
075         * Define the timestamp position (end or begin).
076         *
077         * @parameter expression="${timestampPosition}" default-value="end"
078         */
079        private String timestampPosition;
080    
081        /**
082         * Timestamp tag prefix.
083         *
084         * @parameter expression="${timestampPrefix}" default-value="-"
085         */
086        private String timestampPrefix;
087        
088        /**
089         * currently only implemented with svn scm. Enable a workaround to prevent issue 
090         * due to svn client > 1.5.0 (http://jira.codehaus.org/browse/SCM-406)
091         *      
092         * 
093         * @parameter expression="${remoteTagging}" default-value="true"
094         * @since 1.2
095         */    
096        private boolean remoteTagging;    
097    
098        /** {@inheritDoc} */
099        public void execute()
100            throws MojoExecutionException
101        {
102            super.execute();
103    
104            try
105            {
106                SimpleDateFormat dateFormat = null;
107                String tagTimestamp = "";
108                String finalTag = tag;
109    
110                if ( addTimestamp )
111                {
112                    try
113                    {
114                        getLog().info( "Using timestamp pattern '" + timestampFormat + "'" );
115                        dateFormat = new SimpleDateFormat( timestampFormat );
116                        tagTimestamp = dateFormat.format( new Date() );
117                        getLog().info( "Using timestamp '" + tagTimestamp + "'" );
118                    }
119                    catch ( IllegalArgumentException e )
120                    {
121                        String msg = "The timestamp format '" + timestampFormat + "' is invalid.";
122                        getLog().error( msg, e );
123                        throw new MojoExecutionException( msg, e );
124                    }
125    
126                    if ( "end".equals( timestampPosition ) )
127                    {
128                        finalTag += timestampPrefix + tagTimestamp;
129                    }
130                    else
131                    {
132                        finalTag = tagTimestamp + timestampPrefix + finalTag;
133                    }
134                }
135    
136                ScmRepository repository = getScmRepository();
137                ScmProvider provider = getScmManager().getProviderByRepository( repository );
138    
139                finalTag = provider.sanitizeTagName( finalTag );
140                getLog().info( "Final Tag Name: '" + finalTag + "'" );
141    
142                ScmTagParameters scmTagParameters = new ScmTagParameters( message);
143                scmTagParameters.setRemoteTagging( remoteTagging );
144                
145                TagScmResult result = provider.tag( repository, getFileSet(), finalTag, scmTagParameters);
146    
147                checkResult( result );
148            }
149            catch ( IOException e )
150            {
151                throw new MojoExecutionException( "Cannot run tag command : ", e );
152            }
153            catch ( ScmException e )
154            {
155                throw new MojoExecutionException( "Cannot run tag command : ", e );
156            }
157        }
158    }