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.scm.ScmException;
24  import org.apache.maven.scm.ScmTagParameters;
25  import org.apache.maven.scm.command.tag.TagScmResult;
26  import org.apache.maven.scm.provider.ScmProvider;
27  import org.apache.maven.scm.repository.ScmRepository;
28  
29  import java.io.IOException;
30  import java.text.SimpleDateFormat;
31  import java.util.Date;
32  
33  /**
34   * Tag the project.
35   *
36   * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
37   * @author <a href="saden1@gmil.com">Sharmarke Aden</a>
38   * @version $Id: TagMojo.java 757186 2009-03-22 13:23:00Z olamy $
39   * @goal tag
40   * @aggregator
41   */
42  public class TagMojo
43      extends AbstractScmMojo
44  {
45      /**
46       * The tag name.
47       *
48       * @parameter expression="${tag}"
49       * @required
50       */
51      private String tag;
52  
53      /**
54       * The message applied to the tag creation.
55       *
56       * @parameter expression="${message}"
57       */
58      private String message;
59  
60      /**
61       * Set the timestamp format.
62       *
63       * @parameter expression="${timestampFormat}" default-value="yyyyMMddHHmmss"
64       */
65      private String timestampFormat;
66  
67      /**
68       * Use timestamp tagging.
69       *
70       * @parameter expression="${addTimestamp}" default-value="false"
71       */
72      private boolean addTimestamp;
73  
74      /**
75       * Define the timestamp position (end or begin).
76       *
77       * @parameter expression="${timestampPosition}" default-value="end"
78       */
79      private String timestampPosition;
80  
81      /**
82       * Timestamp tag prefix.
83       *
84       * @parameter expression="${timestampPrefix}" default-value="-"
85       */
86      private String timestampPrefix;
87      
88      /**
89       * currently only implemented with svn scm. Enable a workaround to prevent issue 
90       * due to svn client > 1.5.0 (http://jira.codehaus.org/browse/SCM-406)
91       *      
92       * 
93       * @parameter expression="${remoteTagging}" default-value="true"
94       * @since 1.2
95       */    
96      private boolean remoteTagging;    
97  
98      /** {@inheritDoc} */
99      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 }