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.io.File;
22  import java.util.Map;
23  
24  import org.apache.maven.plugin.MojoExecutionException;
25  import org.apache.maven.plugin.MojoFailureException;
26  import org.apache.maven.plugins.annotations.Component;
27  import org.apache.maven.plugins.annotations.Mojo;
28  import org.apache.maven.plugins.annotations.Parameter;
29  import org.apache.maven.scm.manager.ScmManager;
30  import org.apache.maven.shared.release.DefaultReleaseManagerListener;
31  import org.apache.maven.shared.release.ReleaseExecutionException;
32  import org.apache.maven.shared.release.ReleaseFailureException;
33  import org.apache.maven.shared.release.ReleasePerformRequest;
34  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
35  import org.codehaus.plexus.util.StringUtils;
36  
37  /**
38   * Perform a release from SCM, either from a specified tag, or the tag representing the previous release in
39   * the working copy created by <code>release:prepare</code>.
40   * For more info see <a href="https://maven.apache.org/plugins/maven-release-plugin/usage/perform-release.html"
41   * >https://maven.apache.org/plugins/maven-release-plugin/usage/perform-release.html</a>.
42   *
43   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
44   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
45   */
46  @Mojo(name = "perform", aggregator = true, requiresProject = false)
47  public class PerformReleaseMojo extends AbstractReleaseMojo {
48      /**
49       * A space separated list of goals to execute on release perform. Default value is either <code>deploy</code> or
50       * <code>deploy site-deploy</code>, if the project has a &lt;distributionManagement&gt;/&lt;site&gt; element.
51       */
52      @Parameter(property = "goals")
53      String goals;
54  
55      /**
56       * Comma separated profiles to enable on release perform, in addition to active profiles for project execution.
57       *
58       * @since 2.0-beta-8
59       */
60      @Parameter(property = "releaseProfiles")
61      private String releaseProfiles;
62  
63      /**
64       * The checkout directory.
65       */
66      @Parameter(defaultValue = "${project.build.directory}/checkout", property = "workingDirectory", required = true)
67      private File workingDirectory;
68  
69      /**
70       * The SCM URL to checkout from. If omitted, the one from the <code>release.properties</code> file is used, followed
71       * by the URL from the current POM.
72       */
73      @Parameter(property = "connectionUrl")
74      private String connectionUrl;
75  
76      /**
77       * Use a local checkout instead of doing a checkout from the upstream repository.
78       * ATTENTION: This will only work with distributed SCMs which support the file:// protocol
79       * like e.g. git, jgit or hg!
80       *
81       * TODO: we should think about having the defaults for the various SCM providers provided via modello!
82       *
83       * @since 2.0 for release:perform and 2.5.2 for release:stage
84       */
85      @Parameter(defaultValue = "false", property = "localCheckout")
86      private boolean localCheckout;
87  
88      /**
89       * The SCM username to use.
90       */
91      @Parameter(property = "username")
92      private String username;
93  
94      /**
95       * The SCM password to use.
96       */
97      @Parameter(property = "password")
98      private String password;
99  
100     /**
101      * When cloning a repository if it should be a shallow clone or a full clone.
102      */
103     @Parameter(defaultValue = "true", property = "scmShallowClone")
104     private boolean scmShallowClone = true;
105 
106     /**
107      * Whether to use the default release profile (Maven 2 and 3) that adds sources and javadocs to the released
108      * artifact, if appropriate. If set to true, the release plugin sets the property "<code>performRelease</code>" to
109      * true, which activates the profile "<code>release-profile</code>" as inherited from
110      * <a href="/ref/3.8.5/maven-model-builder/super-pom.html">the super pom</a>.
111      *
112      * @deprecated The <code>release-profile</code> profile will be removed from future versions of the super POM
113      */
114     @Parameter(defaultValue = "false", property = "useReleaseProfile")
115     @Deprecated
116     private boolean useReleaseProfile;
117 
118     /**
119      * Dry run: don't checkout anything from the scm repository, or modify the checkout.
120      * The goals (by default at least {@code deploy}) will <strong>not</strong> be executed.
121      */
122     @Parameter(defaultValue = "false", property = "dryRun")
123     private boolean dryRun;
124 
125     /**
126      * Add a new or overwrite the default implementation per provider.
127      * The key is the scm prefix and the value is the role hint of the
128      * {@link org.apache.maven.scm.provider.ScmProvider}.
129      *
130      * @since 2.5.3
131      * @see ScmManager#setScmProviderImplementation(String, String)
132      */
133     @Parameter
134     private Map<String, String> providerImplementations;
135 
136     /**
137      * The SCM manager.
138      */
139     @Component
140     private ScmManager scmManager;
141 
142     @Override
143     protected String getAdditionalProfiles() {
144         return releaseProfiles;
145     }
146 
147     @Override
148     public void execute() throws MojoExecutionException, MojoFailureException {
149         if (providerImplementations != null) {
150             for (Map.Entry<String, String> providerEntry : providerImplementations.entrySet()) {
151                 getLog().info("Change the default '" + providerEntry.getKey() + "' provider implementation to '"
152                         + providerEntry.getValue() + "'.");
153                 scmManager.setScmProviderImplementation(providerEntry.getKey(), providerEntry.getValue());
154             }
155         }
156 
157         // goals may be splitted into multiple line in configuration.
158         // Let's build a single line command
159         if (goals != null) {
160             goals = StringUtils.join(StringUtils.split(goals), " ");
161         }
162 
163         try {
164             setDeploymentRepository();
165             // Note that the working directory here is not the same as in the release configuration, so don't reuse that
166             ReleaseDescriptorBuilder releaseDescriptor = createReleaseDescriptor();
167             if (connectionUrl != null) {
168                 releaseDescriptor.setScmSourceUrl(connectionUrl);
169             }
170 
171             if (username != null) {
172                 releaseDescriptor.setScmUsername(username);
173             }
174 
175             if (password != null) {
176                 releaseDescriptor.setScmPassword(password);
177             }
178 
179             releaseDescriptor.setScmShallowClone(scmShallowClone);
180 
181             releaseDescriptor.setLocalCheckout(localCheckout);
182 
183             releaseDescriptor.setCheckoutDirectory(workingDirectory.getAbsolutePath());
184             releaseDescriptor.setUseReleaseProfile(useReleaseProfile);
185 
186             createGoals();
187             releaseDescriptor.setPerformGoals(goals);
188 
189             ReleasePerformRequest performRequest = new ReleasePerformRequest();
190             performRequest.setReleaseDescriptorBuilder(releaseDescriptor);
191             performRequest.setReleaseEnvironment(getReleaseEnvironment());
192             performRequest.setReactorProjects(getReactorProjects());
193             performRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
194             performRequest.setDryRun(dryRun);
195 
196             releaseManager.perform(performRequest);
197         } catch (ReleaseExecutionException e) {
198             throw new MojoExecutionException(e.getMessage(), e);
199         } catch (ReleaseFailureException e) {
200             throw new MojoFailureException(e.getMessage(), e);
201         }
202     }
203 
204     /** Just here so it may be overridden by StageReleaseMojo */
205     void setDeploymentRepository() {}
206 
207     /** Just here so it may be overridden by StageReleaseMojo */
208     void createGoals() {
209         if (goals == null) {
210             // set default
211             goals = "deploy";
212             if (project.getDistributionManagement() != null
213                     && project.getDistributionManagement().getSite() != null) {
214                 goals += " site-deploy";
215             }
216         }
217     }
218 }