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 javax.inject.Inject;
22  
23  import java.io.File;
24  
25  import org.apache.maven.plugin.MojoExecutionException;
26  import org.apache.maven.plugin.MojoFailureException;
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.ReleaseManager;
34  import org.apache.maven.shared.release.ReleasePerformRequest;
35  import org.apache.maven.shared.release.config.ReleaseDescriptorBuilder;
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 AbstractScmReadReleaseMojo {
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       * Whether to use the default release profile (Maven 2 and 3) that adds sources and javadocs to the released
90       * artifact, if appropriate. If set to true, the release plugin sets the property "<code>performRelease</code>" to
91       * true, which activates the profile "<code>release-profile</code>" as inherited from
92       * <a href="/ref/3.8.5/maven-model-builder/super-pom.html">the super pom</a>.
93       *
94       * @deprecated The <code>release-profile</code> profile will be removed from future versions of the super POM
95       */
96      @Parameter(defaultValue = "false", property = "useReleaseProfile")
97      @Deprecated
98      private boolean useReleaseProfile;
99  
100     /**
101      * Dry run: don't checkout anything from the scm repository, or modify the checkout.
102      * The goals (by default at least {@code deploy}) will <strong>not</strong> be executed.
103      */
104     @Parameter(defaultValue = "false", property = "dryRun")
105     private boolean dryRun;
106 
107     @Inject
108     public PerformReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
109         super(releaseManager, scmManager);
110     }
111 
112     @Override
113     protected String getAdditionalProfiles() {
114         return releaseProfiles;
115     }
116 
117     @Override
118     public void execute() throws MojoExecutionException, MojoFailureException {
119         super.execute();
120 
121         // goals may be split into multiple lines in configuration.
122         // Let's build a single line command
123         if (goals != null) {
124             goals = String.join(" ", goals.split("\\s+"));
125         }
126 
127         try {
128             setDeploymentRepository();
129             // Note that the working directory here is not the same as in the release configuration, so don't reuse that
130             ReleaseDescriptorBuilder releaseDescriptor = createReleaseDescriptor();
131             if (connectionUrl != null) {
132                 releaseDescriptor.setScmSourceUrl(connectionUrl);
133             }
134 
135             releaseDescriptor.setLocalCheckout(localCheckout);
136 
137             releaseDescriptor.setCheckoutDirectory(workingDirectory.getAbsolutePath());
138             releaseDescriptor.setUseReleaseProfile(useReleaseProfile);
139 
140             createGoals();
141             releaseDescriptor.setPerformGoals(goals);
142 
143             ReleasePerformRequest performRequest = new ReleasePerformRequest();
144             performRequest.setReleaseDescriptorBuilder(releaseDescriptor);
145             performRequest.setReleaseEnvironment(getReleaseEnvironment());
146             performRequest.setReactorProjects(getReactorProjects());
147             performRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
148             performRequest.setDryRun(dryRun);
149 
150             releaseManager.perform(performRequest);
151         } catch (ReleaseExecutionException e) {
152             throw new MojoExecutionException(e.getMessage(), e);
153         } catch (ReleaseFailureException e) {
154             throw new MojoFailureException(e.getMessage(), e);
155         }
156     }
157 
158     /** Just here so it may be overridden by StageReleaseMojo. */
159     void setDeploymentRepository() {}
160 
161     /** Just here so it may be overridden by StageReleaseMojo. */
162     void createGoals() {
163         if (goals == null) {
164             // set default
165             goals = "deploy";
166             if (project.getDistributionManagement() != null
167                     && project.getDistributionManagement().getSite() != null) {
168                 goals += " site-deploy";
169             }
170         }
171     }
172 }