View Javadoc
1   package org.apache.maven.scm.plugin;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.apache.maven.plugins.annotations.Mojo;
24  import org.apache.maven.plugins.annotations.Parameter;
25  import org.apache.maven.scm.ScmException;
26  import org.apache.maven.scm.ScmTagParameters;
27  import org.apache.maven.scm.command.tag.TagScmResult;
28  import org.apache.maven.scm.provider.ScmProvider;
29  import org.apache.maven.scm.repository.ScmRepository;
30  
31  import java.io.IOException;
32  import java.text.SimpleDateFormat;
33  import java.util.Date;
34  
35  /**
36   * Tag the project.
37   *
38   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
39   * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
40   */
41  @Mojo( name = "tag", aggregator = true )
42  public class TagMojo
43      extends AbstractScmMojo
44  {
45      /**
46       * The tag name.
47       */
48      @Parameter( property = "tag", required = true )
49      private String tag;
50  
51      /**
52       * The message applied to the tag creation.
53       */
54      @Parameter( property = "message" )
55      private String message;
56  
57      /**
58       * Set the timestamp format.
59       */
60      @Parameter( property = "timestampFormat", defaultValue = "yyyyMMddHHmmss" )
61      private String timestampFormat;
62  
63      /**
64       * Use timestamp tagging.
65       */
66      @Parameter( property = "addTimestamp", defaultValue = "false" )
67      private boolean addTimestamp;
68  
69      /**
70       * Define the timestamp position (end or begin).
71       */
72      @Parameter( property = "timestampPosition", defaultValue = "end" )
73      private String timestampPosition;
74  
75      /**
76       * Timestamp tag prefix.
77       */
78      @Parameter( property = "timestampPrefix", defaultValue = "-" )
79      private String timestampPrefix;
80      
81      /**
82       * currently only implemented with svn scm. Enable a workaround to prevent issue 
83       * due to svn client > 1.5.0 (http://jira.codehaus.org/browse/SCM-406)
84       *      
85       * @since 1.2
86       */    
87      @Parameter( property = "remoteTagging", defaultValue = "true" )
88      private boolean remoteTagging;    
89  
90      /** {@inheritDoc} */
91      public void execute()
92          throws MojoExecutionException
93      {
94          super.execute();
95  
96          try
97          {
98              SimpleDateFormat dateFormat = null;
99              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 }