Coverage Report - org.apache.maven.scm.plugin.AbstractScmMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractScmMojo
51%
55/108
39%
31/80
4.062
 
 1  
 package org.apache.maven.scm.plugin;
 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 org.apache.maven.plugin.AbstractMojo;
 23  
 import org.apache.maven.plugin.MojoExecutionException;
 24  
 import org.apache.maven.scm.ScmBranch;
 25  
 import org.apache.maven.scm.ScmException;
 26  
 import org.apache.maven.scm.ScmFileSet;
 27  
 import org.apache.maven.scm.ScmResult;
 28  
 import org.apache.maven.scm.ScmRevision;
 29  
 import org.apache.maven.scm.ScmTag;
 30  
 import org.apache.maven.scm.ScmVersion;
 31  
 import org.apache.maven.scm.manager.ScmManager;
 32  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 33  
 import org.apache.maven.scm.provider.ScmProviderRepositoryWithHost;
 34  
 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
 35  
 import org.apache.maven.scm.repository.ScmRepository;
 36  
 import org.apache.maven.scm.repository.ScmRepositoryException;
 37  
 import org.apache.maven.settings.Server;
 38  
 import org.apache.maven.settings.Settings;
 39  
 import org.codehaus.plexus.util.StringUtils;
 40  
 
 41  
 import java.io.File;
 42  
 import java.io.IOException;
 43  
 import java.util.Iterator;
 44  
 import java.util.Map;
 45  
 import java.util.Properties;
 46  
 
 47  
 /**
 48  
  * @author <a href="evenisse@apache.org">Emmanuel Venisse</a>
 49  
  * @version $Id: AbstractScmMojo.java 654744 2008-05-09 09:59:03Z evenisse $
 50  
  */
 51  22
 public abstract class AbstractScmMojo
 52  
     extends AbstractMojo
 53  
 {
 54  
     /**
 55  
      * The SCM connection URL.
 56  
      *
 57  
      * @parameter expression="${connectionUrl}" default-value="${project.scm.connection}"
 58  
      */
 59  
     private String connectionUrl;
 60  
 
 61  
     /**
 62  
      * The SCM connection URL for developers.
 63  
      *
 64  
      * @parameter expression="${connectionUrl}" default-value="${project.scm.developerConnection}"
 65  
      */
 66  
     private String developerConnectionUrl;
 67  
 
 68  
     /**
 69  
      * The type of connection to use (connection or developerConnection).
 70  
      *
 71  
      * @parameter expression="${connectionType}" default-value="connection"
 72  
      */
 73  
     private String connectionType;
 74  
 
 75  
     /**
 76  
      * The working directory.
 77  
      *
 78  
      * @parameter expression="${workingDirectory}"
 79  
      */
 80  
     private File workingDirectory;
 81  
 
 82  
     /**
 83  
      * The user name (used by svn, starteam and perforce protocol).
 84  
      *
 85  
      * @parameter expression="${username}"
 86  
      */
 87  
     private String username;
 88  
 
 89  
     /**
 90  
      * The user password (used by svn, starteam and perforce protocol).
 91  
      *
 92  
      * @parameter expression="${password}"
 93  
      */
 94  
     private String password;
 95  
 
 96  
     /**
 97  
      * The private key (used by java svn).
 98  
      *
 99  
      * @parameter expression="${privateKey}"
 100  
      */
 101  
     private String privateKey;
 102  
 
 103  
     /**
 104  
      * The passphrase (used by java svn).
 105  
      *
 106  
      * @parameter expression="${passphrase}"
 107  
      */
 108  
     private String passphrase;
 109  
 
 110  
     /**
 111  
      * The url of tags base directory (used by svn protocol). It is not
 112  
      * necessary to set it if you use the standard svn layout
 113  
      * (branches/tags/trunk).
 114  
      *
 115  
      * @parameter expression="${tagBase}"
 116  
      */
 117  
     private String tagBase;
 118  
 
 119  
     /**
 120  
      * Comma separated list of includes file pattern.
 121  
      *
 122  
      * @parameter expression="${includes}"
 123  
      */
 124  
     private String includes;
 125  
 
 126  
     /**
 127  
      * Comma separated list of excludes file pattern.
 128  
      *
 129  
      * @parameter expression="${excludes}"
 130  
      */
 131  
     private String excludes;
 132  
 
 133  
     /**
 134  
      * @parameter expression="${component.org.apache.maven.scm.manager.ScmManager}"
 135  
      * @required
 136  
      * @readonly
 137  
      */
 138  
     private ScmManager manager;
 139  
 
 140  
     /**
 141  
      * The base directory.
 142  
      *
 143  
      * @parameter expression="${basedir}"
 144  
      * @required
 145  
      */
 146  
     private File basedir;
 147  
 
 148  
     /**
 149  
      * @parameter expression="${settings}"
 150  
      * @required
 151  
      * @readonly
 152  
      */
 153  
     private Settings settings;
 154  
 
 155  
     /**
 156  
      * List of System properties to pass to the JUnit tests.
 157  
      *
 158  
      * @parameter
 159  
      */
 160  
     private Properties systemProperties;
 161  
 
 162  
     /**
 163  
      * List of provider implementations.
 164  
      *
 165  
      * @parameter
 166  
      */
 167  
     private Map providerImplementations;
 168  
 
 169  
     public void execute()
 170  
         throws MojoExecutionException
 171  
     {
 172  21
         if ( systemProperties != null )
 173  
         {
 174  
             // Add all system properties configured by the user
 175  0
             Iterator iter = systemProperties.keySet().iterator();
 176  
 
 177  0
             while ( iter.hasNext() )
 178  
             {
 179  0
                 String key = (String) iter.next();
 180  
 
 181  0
                 String value = systemProperties.getProperty( key );
 182  
 
 183  0
                 System.setProperty( key, value );
 184  0
             }
 185  
         }
 186  
 
 187  21
         if ( providerImplementations != null )
 188  
         {
 189  0
             for ( Iterator i = providerImplementations.keySet().iterator(); i.hasNext(); )
 190  
             {
 191  0
                 String providerType = (String) i.next();
 192  0
                 String providerImplementation = (String) providerImplementations.get( providerType );
 193  0
                 getLog().info( "Change the default '" + providerType + "' provider implementation to '" +
 194  
                     providerImplementation + "'." );
 195  0
                 getScmManager().setScmProviderImplementation( providerType, providerImplementation );
 196  0
             }
 197  
         }
 198  21
     }
 199  
 
 200  
     protected void setConnectionType( String connectionType )
 201  
     {
 202  4
         this.connectionType = connectionType;
 203  4
     }
 204  
 
 205  
     public String getConnectionUrl()
 206  
     {
 207  35
         boolean requireDeveloperConnection = !"connection".equals( connectionType.toLowerCase() );
 208  31
         if ( StringUtils.isNotEmpty( connectionUrl ) && !requireDeveloperConnection )
 209  
         {
 210  31
             return connectionUrl;
 211  
         }
 212  0
         else if ( StringUtils.isNotEmpty( developerConnectionUrl ) )
 213  
         {
 214  0
             return developerConnectionUrl;
 215  
         }
 216  0
         if ( requireDeveloperConnection )
 217  
         {
 218  0
             throw new NullPointerException( "You need to define a developerConnectionUrl parameter" );
 219  
         }
 220  
         else
 221  
         {
 222  0
             throw new NullPointerException( "You need to define a connectionUrl parameter" );
 223  
         }
 224  
     }
 225  
 
 226  
     public void setConnectionUrl( String connectionUrl )
 227  
     {
 228  33
         this.connectionUrl = connectionUrl;
 229  33
     }
 230  
 
 231  
     public File getWorkingDirectory()
 232  
     {
 233  10
         if ( workingDirectory == null )
 234  
         {
 235  0
             return basedir;
 236  
         }
 237  
 
 238  10
         return workingDirectory;
 239  
     }
 240  
 
 241  
     public void setWorkingDirectory( File workingDirectory )
 242  
     {
 243  15
         this.workingDirectory = workingDirectory;
 244  15
     }
 245  
 
 246  
     public ScmManager getScmManager()
 247  
     {
 248  36
         return manager;
 249  
     }
 250  
 
 251  
     public ScmFileSet getFileSet()
 252  
         throws IOException
 253  
     {
 254  10
         if ( includes != null || excludes != null )
 255  
         {
 256  0
             return new ScmFileSet( getWorkingDirectory(), includes, excludes );
 257  
         }
 258  
         else
 259  
         {
 260  10
             return new ScmFileSet( getWorkingDirectory() );
 261  
         }
 262  
     }
 263  
 
 264  
     public ScmRepository getScmRepository()
 265  
         throws ScmException
 266  
     {
 267  
         ScmRepository repository;
 268  
 
 269  
         try
 270  
         {
 271  17
             repository = getScmManager().makeScmRepository( getConnectionUrl() );
 272  
 
 273  16
             ScmProviderRepository providerRepo = repository.getProviderRepository();
 274  
 
 275  16
             if ( !StringUtils.isEmpty( username ) )
 276  
             {
 277  0
                 providerRepo.setUser( username );
 278  
             }
 279  
 
 280  16
             if ( !StringUtils.isEmpty( password ) )
 281  
             {
 282  0
                 providerRepo.setPassword( password );
 283  
             }
 284  
 
 285  16
             if ( repository.getProviderRepository() instanceof ScmProviderRepositoryWithHost )
 286  
             {
 287  16
                 ScmProviderRepositoryWithHost repo = (ScmProviderRepositoryWithHost) repository.getProviderRepository();
 288  
 
 289  16
                 loadInfosFromSettings( repo );
 290  
 
 291  16
                 if ( !StringUtils.isEmpty( username ) )
 292  
                 {
 293  0
                     repo.setUser( username );
 294  
                 }
 295  
 
 296  16
                 if ( !StringUtils.isEmpty( password ) )
 297  
                 {
 298  0
                     repo.setPassword( password );
 299  
                 }
 300  
 
 301  16
                 if ( !StringUtils.isEmpty( privateKey ) )
 302  
                 {
 303  0
                     repo.setPrivateKey( privateKey );
 304  
                 }
 305  
 
 306  16
                 if ( !StringUtils.isEmpty( passphrase ) )
 307  
                 {
 308  0
                     repo.setPassphrase( passphrase );
 309  
                 }
 310  
             }
 311  
 
 312  16
             if ( !StringUtils.isEmpty( tagBase ) && repository.getProvider().equals( "svn" ) )
 313  
             {
 314  0
                 SvnScmProviderRepository svnRepo = (SvnScmProviderRepository) repository.getProviderRepository();
 315  
 
 316  0
                 svnRepo.setTagBase( tagBase );
 317  
             }
 318  
         }
 319  0
         catch ( ScmRepositoryException e )
 320  
         {
 321  0
             if ( !e.getValidationMessages().isEmpty() )
 322  
             {
 323  0
                 for ( Iterator i = e.getValidationMessages().iterator(); i.hasNext(); )
 324  
                 {
 325  0
                     String message = (String) i.next();
 326  0
                     getLog().error( message );
 327  0
                 }
 328  
             }
 329  
 
 330  0
             throw new ScmException( "Can't load the scm provider.", e );
 331  
         }
 332  1
         catch ( Exception e )
 333  
         {
 334  1
             throw new ScmException( "Can't load the scm provider.", e );
 335  16
         }
 336  
 
 337  16
         return repository;
 338  
     }
 339  
 
 340  
     /**
 341  
      * Load username password from settings if user has not set them in JVM properties
 342  
      *
 343  
      * @param repo
 344  
      */
 345  
     private void loadInfosFromSettings( ScmProviderRepositoryWithHost repo )
 346  
     {
 347  16
         if ( username == null || password == null )
 348  
         {
 349  16
             String host = repo.getHost();
 350  
 
 351  16
             int port = repo.getPort();
 352  
 
 353  16
             if ( port > 0 )
 354  
             {
 355  0
                 host += ":" + port;
 356  
             }
 357  
 
 358  16
             Server server = this.settings.getServer( host );
 359  
 
 360  16
             if ( server != null )
 361  
             {
 362  0
                 if ( username == null )
 363  
                 {
 364  0
                     username = this.settings.getServer( host ).getUsername();
 365  
                 }
 366  
 
 367  0
                 if ( password == null )
 368  
                 {
 369  0
                     password = this.settings.getServer( host ).getPassword();
 370  
                 }
 371  
 
 372  0
                 if ( privateKey == null )
 373  
                 {
 374  0
                     privateKey = this.settings.getServer( host ).getPrivateKey();
 375  
                 }
 376  
 
 377  0
                 if ( passphrase == null )
 378  
                 {
 379  0
                     passphrase = this.settings.getServer( host ).getPassphrase();
 380  
                 }
 381  
             }
 382  
         }
 383  16
     }
 384  
 
 385  
     public void checkResult( ScmResult result )
 386  
         throws MojoExecutionException
 387  
     {
 388  15
         if ( !result.isSuccess() )
 389  
         {
 390  1
             getLog().error( "Provider message:" );
 391  
 
 392  1
             getLog().error( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
 393  
 
 394  1
             getLog().error( "Command output:" );
 395  
 
 396  1
             getLog().error( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
 397  
 
 398  1
             throw new MojoExecutionException(
 399  
                 "Command failed." + StringUtils.defaultString( result.getProviderMessage() ) );
 400  
         }
 401  14
     }
 402  
 
 403  
     public String getIncludes()
 404  
     {
 405  0
         return includes;
 406  
     }
 407  
 
 408  
     public void setIncludes( String includes )
 409  
     {
 410  0
         this.includes = includes;
 411  0
     }
 412  
 
 413  
     public String getExcludes()
 414  
     {
 415  0
         return excludes;
 416  
     }
 417  
 
 418  
     public void setExcludes( String excludes )
 419  
     {
 420  0
         this.excludes = excludes;
 421  0
     }
 422  
 
 423  
     public ScmVersion getScmVersion( String versionType, String version )
 424  
         throws MojoExecutionException
 425  
     {
 426  11
         if ( StringUtils.isEmpty( versionType ) && StringUtils.isNotEmpty( version ) )
 427  
         {
 428  0
             throw new MojoExecutionException( "You must specify the version type." );
 429  
         }
 430  
 
 431  11
         if ( StringUtils.isEmpty( version ) )
 432  
         {
 433  9
             return null;
 434  
         }
 435  
 
 436  2
         if ( "branch".equals( versionType ) )
 437  
         {
 438  1
             return new ScmBranch( version );
 439  
         }
 440  
 
 441  1
         if ( "tag".equals( versionType ) )
 442  
         {
 443  1
             return new ScmTag( version );
 444  
         }
 445  
 
 446  0
         if ( "revision".equals( versionType ) )
 447  
         {
 448  0
             return new ScmRevision( version );
 449  
         }
 450  
 
 451  0
         throw new MojoExecutionException( "Unknown '" + versionType + "' version type." );
 452  
     }
 453  
 }