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 release perform. 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 release perform, 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      * When cloning a repository if it should be a shallow clone or a full clone.
105      */
106     @Parameter( defaultValue = "true", property = "scmShallowClone" )
107     private boolean scmShallowClone = true;
108 
109     /**
110      * Whether to use the default release profile (Maven 2 and 3) that adds sources and javadocs to the released
111      * artifact, if appropriate. If set to true, the release plugin sets the property "<code>performRelease</code>" to
112      * true, which activates the profile "<code>release-profile</code>" as inherited from
113      * <a href="/ref/3.8.5/maven-model-builder/super-pom.html">the super pom</a>.
114      *
115      * @deprecated The <code>release-profile</code> profile will be removed from future versions of the super POM
116      */
117     @Parameter( defaultValue = "false", property = "useReleaseProfile" )
118     @Deprecated
119     private boolean useReleaseProfile;
120 
121     /**
122      * Dry run: don't checkout anything from the scm repository, or modify the checkout.
123      * The goals (by default at least {@code deploy}) will <strong>not</strong> be executed.
124      */
125     @Parameter( defaultValue = "false", property = "dryRun" )
126     private boolean dryRun;
127 
128     /**
129      * Add a new or overwrite the default implementation per provider.
130      * The key is the scm prefix and the value is the role hint of the
131      * {@link org.apache.maven.scm.provider.ScmProvider}.
132      *
133      * @since 2.5.3
134      * @see ScmManager#setScmProviderImplementation(String, String)
135      */
136     @Parameter
137     private Map<String, String> providerImplementations;
138 
139     /**
140      * The SCM manager.
141      */
142     @Component
143     private ScmManager scmManager;
144 
145     @Override
146     protected String getAdditionalProfiles()
147     {
148         return releaseProfiles;
149     }
150 
151     @Override
152     public void execute()
153         throws MojoExecutionException, MojoFailureException
154     {
155         if ( providerImplementations != null )
156         {
157             for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() )
158             {
159                 getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '"
160                     + providerEntry.getValue() + "'." );
161                 scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() );
162             }
163         }
164 
165         // goals may be splitted into multiple line in configuration.
166         // Let's build a single line command
167         if ( goals != null )
168         {
169             goals = StringUtils.join( StringUtils.split( goals ), " " );
170         }
171 
172         try
173         {
174             setDeploymentRepository();
175             // Note that the working directory here is not the same as in the release configuration, so don't reuse that
176             ReleaseDescriptorBuilder releaseDescriptor = createReleaseDescriptor();
177             if ( connectionUrl != null )
178             {
179                 releaseDescriptor.setScmSourceUrl( connectionUrl );
180             }
181 
182             if ( username != null )
183             {
184                 releaseDescriptor.setScmUsername( username );
185             }
186 
187             if ( password != null )
188             {
189                 releaseDescriptor.setScmPassword( password );
190             }
191 
192             releaseDescriptor.setScmShallowClone( scmShallowClone );
193 
194             releaseDescriptor.setLocalCheckout( localCheckout );
195 
196             releaseDescriptor.setCheckoutDirectory( workingDirectory.getAbsolutePath() );
197             releaseDescriptor.setUseReleaseProfile( useReleaseProfile );
198 
199             createGoals();
200             releaseDescriptor.setPerformGoals( goals );
201 
202             ReleasePerformRequest performRequest  = new ReleasePerformRequest();
203             performRequest.setReleaseDescriptorBuilder( releaseDescriptor );
204             performRequest.setReleaseEnvironment( getReleaseEnvironment() );
205             performRequest.setReactorProjects( getReactorProjects() );
206             performRequest.setReleaseManagerListener( new DefaultReleaseManagerListener( getLog(), dryRun ) );
207             performRequest.setDryRun( dryRun );
208 
209             releaseManager.perform( performRequest );
210         }
211         catch ( ReleaseExecutionException e )
212         {
213             throw new MojoExecutionException( e.getMessage(), e );
214         }
215         catch ( ReleaseFailureException e )
216         {
217             throw new MojoFailureException( e.getMessage(), e );
218         }
219     }
220 
221     /** Just here so it may be overridden by StageReleaseMojo */
222     void setDeploymentRepository()
223     {
224     }
225 
226     /** Just here so it may be overridden by StageReleaseMojo */
227     void createGoals()
228     {
229         if ( goals == null )
230         {
231             // set default
232             goals = "deploy";
233             if ( project.getDistributionManagement() != null
234                 && project.getDistributionManagement().getSite() != null )
235             {
236                 goals += " site-deploy";
237             }
238         }
239     }
240 }