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