View Javadoc
1   package org.apache.maven.plugins.release;
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 java.util.Map;
23  
24  import org.apache.maven.artifact.ArtifactUtils;
25  import org.apache.maven.model.Scm;
26  import org.apache.maven.plugin.MojoExecutionException;
27  import org.apache.maven.plugin.MojoFailureException;
28  import org.apache.maven.plugins.annotations.Component;
29  import org.apache.maven.plugins.annotations.Parameter;
30  import org.apache.maven.project.MavenProject;
31  import org.apache.maven.scm.manager.ScmManager;
32  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
33  
34  /**
35   * Abstract Mojo containing SCM parameters
36   *
37   * @author Robert Scholte
38   */
39  // Extra layer since 2.4. Don't use @since doclet, these would be inherited by the subclasses
40  public abstract class AbstractScmReleaseMojo
41      extends AbstractReleaseMojo
42  {
43      /**
44       * The SCM username to use.
45       */
46      @Parameter( property = "username" )
47      private String username;
48  
49      /**
50       * The SCM password to use.
51       */
52      @Parameter( property = "password" )
53      private String password;
54  
55      /**
56       * The SCM tag to use.
57       */
58      @Parameter( alias = "releaseLabel", property = "tag" )
59      private String tag;
60  
61      /**
62       * Format to use when generating the tag name if none is specified. Property interpolation is performed on the
63       * tag, but in order to ensure that the interpolation occurs during release, you must use <code>@{...}</code>
64       * to reference the properties rather than <code>${...}</code>. The following properties are available:
65       * <ul>
66       *     <li><code>groupId</code> or <code>project.groupId</code> - The groupId of the root project.
67       *     <li><code>artifactId</code> or <code>project.artifactId</code> - The artifactId of the root project.
68       *     <li><code>version</code> or <code>project.version</code> - The release version of the root project.
69       * </ul>
70       *
71       * @since 2.2.0
72       */
73      @Parameter( defaultValue = "@{project.artifactId}-@{project.version}", property = "tagNameFormat" )
74      private String tagNameFormat;
75  
76      /**
77       * The tag base directory in SVN, you must define it if you don't use the standard svn layout (trunk/tags/branches).
78       * For example, <code>http://svn.apache.org/repos/asf/maven/plugins/tags</code>. The URL is an SVN URL and does not
79       * include the SCM provider and protocol.
80       */
81      @Parameter( property = "tagBase" )
82      private String tagBase;
83  
84      /**
85       * The message prefix to use for all SCM changes.
86       *
87       * @since 2.0-beta-5
88       */
89      @Parameter( defaultValue = "[maven-release-plugin] ", property = "scmCommentPrefix" )
90      private String scmCommentPrefix;
91  
92      /**
93       * Implemented with git will or not push changes to the upstream repository.
94       * <code>true</code> by default to preserve backward compatibility.
95       * @since 2.1
96       */
97      @Parameter( defaultValue = "true", property = "pushChanges" )
98      private boolean pushChanges = true;
99  
100     /**
101      * A workItem for SCMs like RTC, TFS etc, that may require additional
102      * information to perform a pushChange operation.
103      *
104      * @since 3.0.0-M5
105      */
106     @Parameter( property = "workItem" )
107     private String workItem;
108 
109     /**
110      * Add a new or overwrite the default implementation per provider. 
111      * The key is the scm prefix and the value is the role hint of the
112      * {@link org.apache.maven.scm.provider.ScmProvider}.
113      *
114      * @since 2.0-beta-6
115      * @see ScmManager#setScmProviderImplementation(String, String)
116      */
117     @Parameter
118     private Map<String, String> providerImplementations;
119 
120     /**
121      * The SCM manager.
122      */
123     @Component
124     private ScmManager scmManager;
125 
126     @Override
127     public void execute()
128         throws MojoExecutionException, MojoFailureException
129     {
130         if ( providerImplementations != null )
131         {
132             for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() )
133             {
134                 getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '"
135                     + providerEntry.getValue() + "'." );
136                 scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() );
137             }
138         }
139     }
140 
141     @Override
142     protected ReleaseDescriptorBuilder createReleaseDescriptor()
143     {
144         ReleaseDescriptorBuilder descriptor = super.createReleaseDescriptor();
145 
146         descriptor.setScmPassword( password );
147         descriptor.setScmReleaseLabel( tag );
148         descriptor.setScmTagNameFormat( tagNameFormat );
149         descriptor.setScmTagBase( tagBase );
150         descriptor.setScmUsername( username );
151         descriptor.setScmCommentPrefix( scmCommentPrefix );
152 
153         descriptor.setPushChanges( pushChanges );
154         descriptor.setWorkItem( workItem );
155         
156         if ( project.getScm() != null )
157         {
158             if ( project.getScm().getDeveloperConnection() != null )
159             {
160                 descriptor.setScmSourceUrl( project.getScm().getDeveloperConnection() );
161             }
162             else if ( project.getScm().getConnection() != null )
163             {
164                 descriptor.setScmSourceUrl( project.getScm().getConnection() );
165             }
166         }
167         
168         // As long as Scm.getId() does not exist, read it as a property
169         descriptor.setScmId( project.getProperties().getProperty( "project.scm.id" ) );
170         
171         for ( MavenProject reactorProject : session.getProjects() )
172         {
173             if ( reactorProject.getScm() != null )
174             {
175                 String projectId =
176                     ArtifactUtils.versionlessKey( reactorProject.getGroupId(), reactorProject.getArtifactId() );
177                 
178                 descriptor.addOriginalScmInfo( projectId, buildScm( reactorProject ) );
179             }
180         }
181 
182         return descriptor;
183     }
184     
185     /**
186      * <p>buildScm.</p>
187      *
188      * @param project a {@link org.apache.maven.project.MavenProject} object
189      * @return a {@link org.apache.maven.model.Scm} object
190      */
191     protected Scm buildScm( MavenProject project )
192     {
193         Scm scm;
194         if ( project.getOriginalModel().getScm() == null )
195         {
196             scm = null;
197         }
198         else
199         {
200             scm = new Scm();
201             scm.setConnection( project.getOriginalModel().getScm().getConnection() );
202             scm.setDeveloperConnection( project.getOriginalModel().getScm().getDeveloperConnection() );
203             scm.setTag( project.getOriginalModel().getScm().getTag() );
204             scm.setUrl( project.getOriginalModel().getScm().getUrl() );
205         }
206         return scm;
207     }
208 }