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.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.ReleaseDescriptor;
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 <tt>release:prepare</tt>.
40   * For more info see <a href="http://maven.apache.org/plugins/maven-release-plugin/examples/perform-release.html"
41   * >http://maven.apache.org/plugins/maven-release-plugin/examples/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   * @version $Id: PerformReleaseMojo.java 1707111 2015-10-06 18:05:53Z rfscholte $
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     @Parameter( defaultValue = "true", property = "useReleaseProfile" )
109     private boolean useReleaseProfile;
110 
111     /**
112      * Dry run: don't checkout anything from the scm repository, or modify the checkout.
113      * The goals (by default at least {@code deploy}) will <strong>not</strong> be executed.
114      */
115     @Parameter( defaultValue = "false", property = "dryRun" )
116     private boolean dryRun;
117 
118     /**
119      * Add a new or overwrite the default implementation per provider. 
120      * The key is the scm prefix and the value is the role hint of the
121      * {@link org.apache.maven.scm.provider.ScmProvider}.
122      *
123      * @since 2.5.3
124      * @see ScmManager#setScmProviderImplementation(String, String)
125      */
126     @Parameter
127     private Map<String, String> providerImplementations;
128 
129     /**
130      * The SCM manager.
131      */
132     @Component
133     private ScmManager scmManager;
134 
135     /**
136      * {@inheritDoc}
137      */
138     protected String getAdditionalProfiles()
139     {
140         return releaseProfiles;
141     }
142     
143     /**
144      * {@inheritDoc}
145      */
146     public void execute()
147         throws MojoExecutionException, MojoFailureException
148     {
149         if ( providerImplementations != null )
150         {
151             for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() )
152             {
153                 getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '"
154                     + providerEntry.getValue() + "'." );
155                 scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() );
156             }
157         }
158         
159         // goals may be splitted into multiple line in configuration.
160         // Let's build a single line command
161         if ( goals != null )
162         {
163             goals = StringUtils.join( StringUtils.split( goals ), " " );
164         }
165 
166         try
167         {
168             setDeploymentRepository();
169             // Note that the working directory here is not the same as in the release configuration, so don't reuse that
170             ReleaseDescriptor releaseDescriptor = createReleaseDescriptor();
171             if ( connectionUrl != null )
172             {
173                 releaseDescriptor.setScmSourceUrl( connectionUrl );
174             }
175             
176             if ( username != null )
177             {
178                 releaseDescriptor.setScmUsername( username );
179             }
180             
181             if ( password != null )
182             {
183                 releaseDescriptor.setScmPassword( password );
184             }
185             
186             releaseDescriptor.setLocalCheckout( localCheckout );
187 
188             releaseDescriptor.setCheckoutDirectory( workingDirectory.getAbsolutePath() );
189             releaseDescriptor.setUseReleaseProfile( useReleaseProfile );
190 
191             createGoals();
192             releaseDescriptor.setPerformGoals( goals );
193             
194             ReleasePerformRequest performRequest  = new ReleasePerformRequest();
195             performRequest.setReleaseDescriptor( releaseDescriptor );
196             performRequest.setReleaseEnvironment( getReleaseEnvironment() );
197             performRequest.setReactorProjects( getReactorProjects() );
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 }