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       * When cloning a repository if it should be a shallow clone or a full clone.
94       */
95      @Parameter( defaultValue = "true", property = "scmShallowClone" )
96      private boolean scmShallowClone = true;
97  
98      /**
99       * Implemented with git will or not push changes to the upstream repository.
100      * <code>true</code> by default to preserve backward compatibility.
101      * @since 2.1
102      */
103     @Parameter( defaultValue = "true", property = "pushChanges" )
104     private boolean pushChanges = true;
105 
106     /**
107      * A workItem for SCMs like RTC, TFS etc, that may require additional
108      * information to perform a pushChange operation.
109      *
110      * @since 3.0.0-M5
111      */
112     @Parameter( property = "workItem" )
113     private String workItem;
114 
115     /**
116      * Add a new or overwrite the default implementation per provider. 
117      * The key is the scm prefix and the value is the role hint of the
118      * {@link org.apache.maven.scm.provider.ScmProvider}.
119      *
120      * @since 2.0-beta-6
121      * @see ScmManager#setScmProviderImplementation(String, String)
122      */
123     @Parameter
124     private Map<String, String> providerImplementations;
125 
126     /**
127      * The SCM manager.
128      */
129     @Component
130     private ScmManager scmManager;
131 
132     @Override
133     public void execute()
134         throws MojoExecutionException, MojoFailureException
135     {
136         if ( providerImplementations != null )
137         {
138             for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() )
139             {
140                 getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '"
141                     + providerEntry.getValue() + "'." );
142                 scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() );
143             }
144         }
145     }
146 
147     @Override
148     protected ReleaseDescriptorBuilder createReleaseDescriptor()
149     {
150         ReleaseDescriptorBuilder descriptor = super.createReleaseDescriptor();
151 
152         descriptor.setScmPassword( password );
153         descriptor.setScmReleaseLabel( tag );
154         descriptor.setScmTagNameFormat( tagNameFormat );
155         descriptor.setScmTagBase( tagBase );
156         descriptor.setScmUsername( username );
157         descriptor.setScmCommentPrefix( scmCommentPrefix );
158         descriptor.setScmShallowClone( scmShallowClone );
159 
160         descriptor.setPushChanges( pushChanges );
161         descriptor.setWorkItem( workItem );
162         
163         if ( project.getScm() != null )
164         {
165             if ( project.getScm().getDeveloperConnection() != null )
166             {
167                 descriptor.setScmSourceUrl( project.getScm().getDeveloperConnection() );
168             }
169             else if ( project.getScm().getConnection() != null )
170             {
171                 descriptor.setScmSourceUrl( project.getScm().getConnection() );
172             }
173         }
174         
175         // As long as Scm.getId() does not exist, read it as a property
176         descriptor.setScmId( project.getProperties().getProperty( "project.scm.id" ) );
177         
178         for ( MavenProject reactorProject : session.getProjects() )
179         {
180             if ( reactorProject.getScm() != null )
181             {
182                 String projectId =
183                     ArtifactUtils.versionlessKey( reactorProject.getGroupId(), reactorProject.getArtifactId() );
184                 
185                 descriptor.addOriginalScmInfo( projectId, buildScm( reactorProject ) );
186             }
187         }
188 
189         return descriptor;
190     }
191     
192     /**
193      * <p>buildScm.</p>
194      *
195      * @param project a {@link org.apache.maven.project.MavenProject} object
196      * @return a {@link org.apache.maven.model.Scm} object
197      */
198     protected Scm buildScm( MavenProject project )
199     {
200         Scm scm;
201         if ( project.getOriginalModel().getScm() == null )
202         {
203             scm = null;
204         }
205         else
206         {
207             scm = new Scm();
208             scm.setConnection( project.getOriginalModel().getScm().getConnection() );
209             scm.setDeveloperConnection( project.getOriginalModel().getScm().getDeveloperConnection() );
210             scm.setTag( project.getOriginalModel().getScm().getTag() );
211             scm.setUrl( project.getOriginalModel().getScm().getUrl() );
212         }
213         return scm;
214     }
215 }