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