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  import org.codehaus.plexus.util.StringUtils;
37  
38  /**
39   * Perform a release from SCM, either from a specified tag, or the tag representing the previous release in
40   * the working copy created by <code>release:prepare</code>.
41   * For more info see <a href="https://maven.apache.org/plugins/maven-release-plugin/usage/perform-release.html"
42   * >https://maven.apache.org/plugins/maven-release-plugin/usage/perform-release.html</a>.
43   *
44   * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
45   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
46   */
47  @Mojo(name = "perform", aggregator = true, requiresProject = false)
48  public class PerformReleaseMojo extends AbstractScmReadReleaseMojo {
49      /**
50       * A space separated list of goals to execute on release perform. Default value is either <code>deploy</code> or
51       * <code>deploy site-deploy</code>, if the project has a &lt;distributionManagement&gt;/&lt;site&gt; element.
52       */
53      @Parameter(property = "goals")
54      String goals;
55  
56      /**
57       * Comma separated profiles to enable on release perform, in addition to active profiles for project execution.
58       *
59       * @since 2.0-beta-8
60       */
61      @Parameter(property = "releaseProfiles")
62      private String releaseProfiles;
63  
64      /**
65       * The checkout directory.
66       */
67      @Parameter(defaultValue = "${project.build.directory}/checkout", property = "workingDirectory", required = true)
68      private File workingDirectory;
69  
70      /**
71       * The SCM URL to checkout from. If omitted, the one from the <code>release.properties</code> file is used, followed
72       * by the URL from the current POM.
73       */
74      @Parameter(property = "connectionUrl")
75      private String connectionUrl;
76  
77      /**
78       * Use a local checkout instead of doing a checkout from the upstream repository.
79       * ATTENTION: This will only work with distributed SCMs which support the file:// protocol
80       * like e.g. git, jgit or hg!
81       *
82       * TODO: we should think about having the defaults for the various SCM providers provided via modello!
83       *
84       * @since 2.0 for release:perform and 2.5.2 for release:stage
85       */
86      @Parameter(defaultValue = "false", property = "localCheckout")
87      private boolean localCheckout;
88  
89      /**
90       * Whether to use the default release profile (Maven 2 and 3) that adds sources and javadocs to the released
91       * artifact, if appropriate. If set to true, the release plugin sets the property "<code>performRelease</code>" to
92       * true, which activates the profile "<code>release-profile</code>" as inherited from
93       * <a href="/ref/3.8.5/maven-model-builder/super-pom.html">the super pom</a>.
94       *
95       * @deprecated The <code>release-profile</code> profile will be removed from future versions of the super POM
96       */
97      @Parameter(defaultValue = "false", property = "useReleaseProfile")
98      @Deprecated
99      private boolean useReleaseProfile;
100 
101     /**
102      * Dry run: don't checkout anything from the scm repository, or modify the checkout.
103      * The goals (by default at least {@code deploy}) will <strong>not</strong> be executed.
104      */
105     @Parameter(defaultValue = "false", property = "dryRun")
106     private boolean dryRun;
107 
108     @Inject
109     public PerformReleaseMojo(ReleaseManager releaseManager, ScmManager scmManager) {
110         super(releaseManager, scmManager);
111     }
112 
113     @Override
114     protected String getAdditionalProfiles() {
115         return releaseProfiles;
116     }
117 
118     @Override
119     public void execute() throws MojoExecutionException, MojoFailureException {
120         super.execute();
121 
122         // goals may be split into multiple lines in configuration.
123         // Let's build a single line command
124         if (goals != null) {
125             goals = StringUtils.join(goals.split("\\s+"), " ");
126         }
127 
128         try {
129             setDeploymentRepository();
130             // Note that the working directory here is not the same as in the release configuration, so don't reuse that
131             ReleaseDescriptorBuilder releaseDescriptor = createReleaseDescriptor();
132             if (connectionUrl != null) {
133                 releaseDescriptor.setScmSourceUrl(connectionUrl);
134             }
135 
136             releaseDescriptor.setLocalCheckout(localCheckout);
137 
138             releaseDescriptor.setCheckoutDirectory(workingDirectory.getAbsolutePath());
139             releaseDescriptor.setUseReleaseProfile(useReleaseProfile);
140 
141             createGoals();
142             releaseDescriptor.setPerformGoals(goals);
143 
144             ReleasePerformRequest performRequest = new ReleasePerformRequest();
145             performRequest.setReleaseDescriptorBuilder(releaseDescriptor);
146             performRequest.setReleaseEnvironment(getReleaseEnvironment());
147             performRequest.setReactorProjects(getReactorProjects());
148             performRequest.setReleaseManagerListener(new DefaultReleaseManagerListener(getLog(), dryRun));
149             performRequest.setDryRun(dryRun);
150 
151             releaseManager.perform(performRequest);
152         } catch (ReleaseExecutionException e) {
153             throw new MojoExecutionException(e.getMessage(), e);
154         } catch (ReleaseFailureException e) {
155             throw new MojoFailureException(e.getMessage(), e);
156         }
157     }
158 
159     /** Just here so it may be overridden by StageReleaseMojo */
160     void setDeploymentRepository() {}
161 
162     /** Just here so it may be overridden by StageReleaseMojo */
163     void createGoals() {
164         if (goals == null) {
165             // set default
166             goals = "deploy";
167             if (project.getDistributionManagement() != null
168                     && project.getDistributionManagement().getSite() != null) {
169                 goals += " site-deploy";
170             }
171         }
172     }
173 }