Coverage Report - org.apache.maven.plugins.release.AbstractReleaseMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractReleaseMojo
76%
38/50
60%
12/20
2
 
 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.Iterator;
 24  
 import java.util.List;
 25  
 import java.util.Map;
 26  
 
 27  
 import org.apache.maven.model.Profile;
 28  
 import org.apache.maven.plugin.AbstractMojo;
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.apache.maven.plugin.MojoFailureException;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.apache.maven.scm.manager.ScmManager;
 33  
 import org.apache.maven.settings.Settings;
 34  
 import org.apache.maven.shared.release.ReleaseManager;
 35  
 import org.apache.maven.shared.release.config.ReleaseDescriptor;
 36  
 import org.apache.maven.shared.release.env.DefaultReleaseEnvironment;
 37  
 import org.apache.maven.shared.release.env.ReleaseEnvironment;
 38  
 import org.codehaus.plexus.util.StringUtils;
 39  
 
 40  
 /**
 41  
  * Base class with shared configuration.
 42  
  *
 43  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 44  
  * @version $Id: org.apache.maven.plugins.release.AbstractReleaseMojo.html 816525 2012-05-08 11:35:10Z hboutemy $
 45  
  */
 46  42
 public abstract class AbstractReleaseMojo
 47  
     extends AbstractMojo
 48  
 {
 49  
     /**
 50  
      * The SCM username to use.
 51  
      *
 52  
      * @parameter expression="${username}"
 53  
      */
 54  
     private String username;
 55  
 
 56  
     /**
 57  
      * The SCM password to use.
 58  
      *
 59  
      * @parameter expression="${password}"
 60  
      */
 61  
     private String password;
 62  
 
 63  
     /**
 64  
      * The SCM tag to use.
 65  
      *
 66  
      * @parameter expression="${tag}" alias="releaseLabel"
 67  
      */
 68  
     private String tag;
 69  
 
 70  
     /**
 71  
      * The tag base directory in SVN, you must define it if you don't use the standard svn layout (trunk/tags/branches).
 72  
      * For example, <code>http://svn.apache.org/repos/asf/maven/plugins/tags</code>. The URL is an SVN URL and does not
 73  
      * include the SCM provider and protocol.
 74  
      *
 75  
      * @parameter expression="${tagBase}"
 76  
      */
 77  
     private String tagBase;
 78  
 
 79  
     /**
 80  
      * @parameter expression="${basedir}"
 81  
      * @required
 82  
      * @readonly
 83  
      */
 84  
     protected File basedir;
 85  
 
 86  
     /**
 87  
      * @parameter expression="${settings}"
 88  
      * @required
 89  
      * @readonly
 90  
      */
 91  
     protected Settings settings;
 92  
 
 93  
     /**
 94  
      * @parameter expression="${project}"
 95  
      * @required
 96  
      * @readonly
 97  
      */
 98  
     protected MavenProject project;
 99  
 
 100  
     /**
 101  
      * @component
 102  
      */
 103  
     protected ReleaseManager releaseManager;
 104  
 
 105  
     /**
 106  
      * Additional arguments to pass to the Maven executions, separated by spaces.
 107  
      *
 108  
      * @parameter expression="${arguments}" alias="prepareVerifyArgs"
 109  
      */
 110  
     private String arguments;
 111  
 
 112  
     /**
 113  
      * The file name of the POM to execute any goals against.
 114  
      *
 115  
      * @parameter expression="${pomFileName}"
 116  
      */
 117  
     private String pomFileName;
 118  
 
 119  
     /**
 120  
      * The message prefix to use for all SCM changes.
 121  
      *
 122  
      * @parameter expression="${scmCommentPrefix}" default-value="[maven-release-plugin] "
 123  
      * @since 2.0-beta-5
 124  
      */
 125  
     private String scmCommentPrefix;
 126  
 
 127  
     /**
 128  
      * @parameter expression="${reactorProjects}"
 129  
      * @required
 130  
      * @readonly
 131  
      */
 132  
     protected List reactorProjects;
 133  
 
 134  
     /**
 135  
      * List of provider implementations.
 136  
      *
 137  
      * @parameter
 138  
      * @since 2.0-beta-6
 139  
      */
 140  
     private Map providerImplementations;
 141  
 
 142  
     /**
 143  
      * The M2_HOME parameter to use for forked Maven invocations.
 144  
      *
 145  
      * @parameter default-value="${maven.home}"
 146  
      * @since 2.0-beta-8
 147  
      */
 148  
     protected File mavenHome;
 149  
 
 150  
     /**
 151  
      * The JAVA_HOME parameter to use for forked Maven invocations.
 152  
      *
 153  
      * @parameter default-value="${java.home}"
 154  
      * @since 2.0-beta-8
 155  
      */
 156  
     protected File javaHome;
 157  
 
 158  
     /**
 159  
      * The command-line local repository directory in use for this build (if specified).
 160  
      *
 161  
      * @parameter default-value="${maven.repo.local}"
 162  
      * @since 2.0-beta-8
 163  
      */
 164  
     protected File localRepoDirectory;
 165  
 
 166  
     /**
 167  
      * Role hint of the {@link org.apache.maven.shared.release.exec.MavenExecutor} implementation to use.
 168  
      *
 169  
      * @parameter expression="${mavenExecutorId}" default-value="invoker"
 170  
      * @since 2.0-beta-8
 171  
      */
 172  
     protected String mavenExecutorId;
 173  
 
 174  
     /**
 175  
      * Use a local checkout instead of doing a checkout from the upstream repository.
 176  
      * ATTENTION: This will only work with distributed SCMs which support the file:// protocol
 177  
      * like e.g. git, jgit or hg!
 178  
      *
 179  
      * TODO: we should think about having the defaults for the various SCM providers provided via modello!
 180  
      *
 181  
      * @parameter expression="${localCheckout}" default-value="false"
 182  
      * @since 2.0
 183  
      */
 184  
     private boolean localCheckout;
 185  
 
 186  
     /**
 187  
      * The SCM manager.
 188  
      *
 189  
      * @component
 190  
      */
 191  
     private ScmManager scmManager;
 192  
 
 193  
 
 194  
     /**
 195  
      * Gets the enviroment settings configured for this release.
 196  
      *
 197  
      * @return The release environment, never <code>null</code>.
 198  
      */
 199  
     protected ReleaseEnvironment getReleaseEnvironment()
 200  
     {
 201  39
         return new DefaultReleaseEnvironment().setSettings( settings )
 202  
                                               .setJavaHome( javaHome )
 203  
                                               .setMavenHome( mavenHome )
 204  
                                               .setLocalRepositoryDirectory( localRepoDirectory )
 205  
                                               .setMavenExecutorId( mavenExecutorId );
 206  
     }
 207  
 
 208  
     /**
 209  
      * {@inheritDoc}
 210  
      */
 211  
     public void execute()
 212  
         throws MojoExecutionException, MojoFailureException
 213  
     {
 214  42
         if ( providerImplementations != null )
 215  
         {
 216  0
             for ( Iterator i = providerImplementations.keySet().iterator(); i.hasNext(); )
 217  
             {
 218  0
                 String providerType = (String) i.next();
 219  0
                 String providerImplementation = (String) providerImplementations.get( providerType );
 220  0
                 getLog().info( "Change the default '" + providerType + "' provider implementation to '"
 221  
                     + providerImplementation + "'." );
 222  0
                 scmManager.setScmProviderImplementation( providerType, providerImplementation );
 223  0
             }
 224  
         }
 225  42
     }
 226  
 
 227  
     /**
 228  
      * Creates the release descriptor from the various goal parameters.
 229  
      *
 230  
      * @return The release descriptor, never <code>null</code>.
 231  
      */
 232  
     protected ReleaseDescriptor createReleaseDescriptor()
 233  
     {
 234  39
         ReleaseDescriptor descriptor = new ReleaseDescriptor();
 235  
 
 236  39
         descriptor.setInteractive( settings.isInteractiveMode() );
 237  
 
 238  39
         descriptor.setScmPassword( password );
 239  39
         descriptor.setScmReleaseLabel( tag );
 240  39
         descriptor.setScmTagBase( tagBase );
 241  39
         descriptor.setScmUsername( username );
 242  39
         descriptor.setScmCommentPrefix( scmCommentPrefix );
 243  
 
 244  39
         descriptor.setWorkingDirectory( basedir.getAbsolutePath() );
 245  
 
 246  39
         descriptor.setPomFileName( pomFileName );
 247  
 
 248  39
         descriptor.setLocalCheckout( localCheckout );
 249  
 
 250  39
         List profiles = project.getActiveProfiles();
 251  
 
 252  39
         String arguments = this.arguments;
 253  39
         if ( profiles != null && !profiles.isEmpty() )
 254  
         {
 255  6
             if ( !StringUtils.isEmpty( arguments ) )
 256  
             {
 257  3
                 arguments += " -P ";
 258  
             }
 259  
             else
 260  
             {
 261  3
                 arguments = "-P ";
 262  
             }
 263  
 
 264  6
             for ( Iterator it = profiles.iterator(); it.hasNext(); )
 265  
             {
 266  12
                 Profile profile = (Profile) it.next();
 267  
 
 268  12
                 arguments += profile.getId();
 269  12
                 if ( it.hasNext() )
 270  
                 {
 271  6
                     arguments += ",";
 272  
                 }
 273  12
             }
 274  
 
 275  6
             String additionalProfiles = getAdditionalProfiles();
 276  6
             if ( additionalProfiles != null )
 277  
             {
 278  0
                 if ( !profiles.isEmpty() )
 279  
                 {
 280  0
                     arguments += ",";
 281  
                 }
 282  0
                 arguments += additionalProfiles;
 283  
             }
 284  
         }
 285  39
         descriptor.setAdditionalArguments( arguments );
 286  
 
 287  39
         return descriptor;
 288  
     }
 289  
 
 290  
     /**
 291  
      * Gets the comma separated list of additional profiles for the release build.
 292  
      *
 293  
      * @return additional profiles to enable during release
 294  
      */
 295  
     protected String getAdditionalProfiles()
 296  
     {
 297  0
         return null;
 298  
     }
 299  
 
 300  
     /**
 301  
      * Sets the component used to perform release actions.
 302  
      *
 303  
      * @param releaseManager The release manager implementation to use, must not be <code>null</code>.
 304  
      */
 305  
     void setReleaseManager( ReleaseManager releaseManager )
 306  
     {
 307  42
         this.releaseManager = releaseManager;
 308  42
     }
 309  
 
 310  
     /**
 311  
      * Gets the effective settings for this build.
 312  
      *
 313  
      * @return The effective settings for this build, never <code>null</code>.
 314  
      */
 315  
     Settings getSettings()
 316  
     {
 317  0
         return settings;
 318  
     }
 319  
 
 320  
     /**
 321  
      * Sets the base directory of the build.
 322  
      *
 323  
      * @param basedir The build's base directory, must not be <code>null</code>.
 324  
      */
 325  
     public void setBasedir( File basedir )
 326  
     {
 327  42
         this.basedir = basedir;
 328  42
     }
 329  
 
 330  
     /**
 331  
      * Gets the list of projects in the build reactor.
 332  
      *
 333  
      * @return The list of reactor project, never <code>null</code>.
 334  
      */
 335  
     public List getReactorProjects()
 336  
     {
 337  3
         return reactorProjects;
 338  
     }
 339  
 
 340  
     /**
 341  
      * Add additional arguments.
 342  
      *
 343  
      * @param argument The argument to add, must not be <code>null</code>.
 344  
      */
 345  
     protected void addArgument( String argument )
 346  
     {
 347  3
         if ( arguments != null )
 348  
         {
 349  0
             arguments += " " + argument;
 350  
         }
 351  
         else
 352  
         {
 353  3
             arguments = argument;
 354  
         }
 355  3
     }
 356  
 }