Coverage Report - org.apache.maven.plugins.scmpublish.AbstractScmPublishMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractScmPublishMojo
0 %
0/104
0 %
0/38
6,429
 
 1  
 package org.apache.maven.plugins.scmpublish;
 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.commons.io.FileUtils;
 23  
 import org.apache.maven.execution.MavenSession;
 24  
 import org.apache.maven.plugin.AbstractMojo;
 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.Parameter;
 29  
 import org.apache.maven.scm.CommandParameter;
 30  
 import org.apache.maven.scm.CommandParameters;
 31  
 import org.apache.maven.scm.ScmBranch;
 32  
 import org.apache.maven.scm.ScmException;
 33  
 import org.apache.maven.scm.ScmFileSet;
 34  
 import org.apache.maven.scm.ScmResult;
 35  
 import org.apache.maven.scm.manager.NoSuchScmProviderException;
 36  
 import org.apache.maven.scm.manager.ScmManager;
 37  
 import org.apache.maven.scm.provider.ScmProvider;
 38  
 import org.apache.maven.scm.provider.svn.AbstractSvnScmProvider;
 39  
 import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
 40  
 import org.apache.maven.scm.repository.ScmRepository;
 41  
 import org.apache.maven.scm.repository.ScmRepositoryException;
 42  
 import org.apache.maven.settings.Settings;
 43  
 import org.apache.maven.shared.release.config.ReleaseDescriptor;
 44  
 import org.apache.maven.shared.release.scm.ScmRepositoryConfigurator;
 45  
 
 46  
 import java.io.File;
 47  
 import java.io.IOException;
 48  
 import java.util.Map;
 49  
 
 50  
 /**
 51  
  * Base class for the scm-publish mojos.
 52  
  */
 53  
 public abstract class AbstractScmPublishMojo
 54  
     extends AbstractMojo
 55  
 {
 56  
 
 57  
     /**
 58  
      * Location of the inventory file.
 59  
      */
 60  
     @Parameter ( property = "scmpublish.inventoryFile",
 61  
                  defaultValue = "${project.build.directory}/scmpublish-inventory.js" )
 62  
     protected File inventoryFile;
 63  
 
 64  
     /**
 65  
      * Location of the scm publication tree.
 66  
      */
 67  
     @Parameter ( property = "scmpublish.pubScmUrl", defaultValue = "${project.distributionManagement.site.url}",
 68  
                  required = true )
 69  
     protected String pubScmUrl;
 70  
 
 71  
     /**
 72  
      * Location where the scm check-out is done.
 73  
      */
 74  
     @Parameter ( property = "scmpublish.checkoutDirectory",
 75  
                  defaultValue = "${project.build.directory}/scmpublish-checkout" )
 76  
     protected File checkoutDirectory;
 77  
 
 78  
     /**
 79  
      * Patterns to exclude from the scm tree.
 80  
      */
 81  
     @Parameter
 82  
     protected String excludes;
 83  
 
 84  
     /**
 85  
      * Patterns to include in the scm tree.
 86  
      */
 87  
     @Parameter
 88  
     protected String includes;
 89  
 
 90  
     /**
 91  
      * List of SCM provider implementations.
 92  
      */
 93  
     @Parameter
 94  
     private Map<String, String> providerImplementations;
 95  
 
 96  
     /**
 97  
      * The SCM manager.
 98  
      */
 99  
     @Component
 100  
     private ScmManager scmManager;
 101  
 
 102  
     /**
 103  
      * Tool that gets a configured SCM repository from release configuration.
 104  
      */
 105  
     @Component
 106  
     protected ScmRepositoryConfigurator scmRepositoryConfigurator;
 107  
 
 108  
     /**
 109  
      * The SCM username to use.
 110  
      */
 111  
     @Parameter ( property = "username" )
 112  
     protected String username;
 113  
 
 114  
     /**
 115  
      * The SCM password to use.
 116  
      */
 117  
     @Parameter ( property = "password" )
 118  
     protected String password;
 119  
 
 120  
     /**
 121  
      * Use a local checkout instead of doing a checkout from the upstream repository. ATTENTION: This will only work
 122  
      * with distributed SCMs which support the file:// protocol TODO: we should think about having the defaults for the
 123  
      * various SCM providers provided via modello!
 124  
      */
 125  
     @Parameter ( property = "localCheckout", defaultValue = "false" )
 126  
     protected boolean localCheckout;
 127  
 
 128  
     /**
 129  
      * The outputEncoding parameter of the site plugin. This plugin will corrupt your site
 130  
      * if this does not match the value used by the site plugin.
 131  
      */
 132  
     @Parameter ( property = "outputEncoding", defaultValue = "${project.reporting.outputEncoding}" )
 133  
     protected String siteOutputEncoding;
 134  
 
 135  
     /**
 136  
      * if the checkout directory exists and this flag is activated the plugin will try an update rather
 137  
      * than delete then checkout
 138  
      */
 139  
     @Parameter ( property = "scmpublish.tryUpdate", defaultValue = "false" )
 140  
     protected boolean tryUpdate;
 141  
 
 142  
     /**
 143  
      * Do not delete files to the scm
 144  
      */
 145  
     @Parameter ( property = "scmpublish.skipDeletedFiles", defaultValue = "false" )
 146  
     protected boolean skipDeletedFiles;
 147  
 
 148  
     /**
 149  
      */
 150  
     @Parameter ( defaultValue = "${basedir}", readonly = true )
 151  
     protected File basedir;
 152  
 
 153  
     /**
 154  
      */
 155  
     @Component
 156  
     protected Settings settings;
 157  
 
 158  
     /**
 159  
      */
 160  
     @Component
 161  
     protected MavenSession session;
 162  
 
 163  
     /**
 164  
      * Collections of paths to not delete when checking content to delete.
 165  
      * If your site has subdirectories published by an other mechanism/build
 166  
      */
 167  
     @Parameter
 168  
     protected String[] ignorePathsToDelete;
 169  
 
 170  
     /**
 171  
      * for github you must configure with gh-pages
 172  
      */
 173  
     @Parameter ( property = "scmpublish.scm.branch" )
 174  
     protected String scmBranch;
 175  
 
 176  
     /**
 177  
      * for svn avoid automatic remote url create
 178  
      */
 179  
     @Parameter ( property = "scmpublish.automaticRemotePathCreation", defaultValue = "true" )
 180  
     protected boolean automaticRemotePathCreation;
 181  
 
 182  
     protected ScmProvider scmProvider;
 183  
 
 184  
     protected ScmRepository scmRepository;
 185  
 
 186  
     protected AbstractScmPublishMojo()
 187  
     {
 188  0
         super();
 189  0
     }
 190  
 
 191  
     protected void logInfo( String format, Object... params )
 192  
     {
 193  0
         getLog().info( String.format( format, params ) );
 194  0
     }
 195  
 
 196  
     protected void logError( String format, Object... params )
 197  
     {
 198  0
         getLog().error( String.format( format, params ) );
 199  0
     }
 200  
 
 201  
     protected ReleaseDescriptor setupScm()
 202  
         throws ScmRepositoryException, NoSuchScmProviderException
 203  
     {
 204  
         String scmUrl;
 205  0
         if ( localCheckout )
 206  
         {
 207  
             // in the release phase we have to change the checkout URL
 208  
             // to do a local checkout instead of going over the network.
 209  
 
 210  
             // the first step is a bit tricky, we need to know which provider! like e.g. "scm:jgit:http://"
 211  
             // the offset of 4 is because 'scm:' has 4 characters...
 212  0
             String providerPart = pubScmUrl.substring( 0, pubScmUrl.indexOf( ':', 4 ) );
 213  
 
 214  
             // X TODO: also check the information from releaseDescriptor.getScmRelativePathProjectDirectory()
 215  
             // X TODO: in case our toplevel git directory has no pom.
 216  
             // X TODO: fix pathname once I understand this.
 217  0
             scmUrl = providerPart + ":file://" + "target/localCheckout";
 218  0
             logInfo( "Performing a LOCAL checkout from " + scmUrl );
 219  
         }
 220  
 
 221  0
         ReleaseDescriptor releaseDescriptor = new ReleaseDescriptor();
 222  0
         releaseDescriptor.setInteractive( settings.isInteractiveMode() );
 223  
 
 224  
         //TODO use from settings with decrypt stuff
 225  
 
 226  0
         releaseDescriptor.setScmPassword( password );
 227  0
         releaseDescriptor.setScmUsername( username );
 228  
 
 229  0
         releaseDescriptor.setWorkingDirectory( basedir.getAbsolutePath() );
 230  0
         releaseDescriptor.setLocalCheckout( localCheckout );
 231  0
         releaseDescriptor.setScmSourceUrl( pubScmUrl );
 232  
 
 233  0
         if ( providerImplementations != null )
 234  
         {
 235  0
             for ( Map.Entry<String, String> providerEntry : providerImplementations.entrySet() )
 236  
             {
 237  0
                 getLog().info( "Change the default '" + providerEntry.getKey() + "' provider implementation to '"
 238  
                                    + providerEntry.getValue() + "'." );
 239  0
                 scmManager.setScmProviderImplementation( providerEntry.getKey(), providerEntry.getValue() );
 240  
             }
 241  
         }
 242  
 
 243  0
         scmRepository = scmRepositoryConfigurator.getConfiguredRepository( releaseDescriptor, settings );
 244  
 
 245  0
         scmProvider = scmRepositoryConfigurator.getRepositoryProvider( scmRepository );
 246  
 
 247  0
         return releaseDescriptor;
 248  
     }
 249  
 
 250  
     protected void checkoutExisting()
 251  
         throws MojoExecutionException
 252  
     {
 253  
 
 254  0
         if ( scmProvider instanceof AbstractSvnScmProvider )
 255  
         {
 256  0
             File baseDir = null;
 257  
             try
 258  
             {
 259  
 
 260  0
                 getLog().debug( "use AbstractSvnScmProvider so we can check if remote url exists and create it" );
 261  0
                 AbstractSvnScmProvider svnScmProvider = (AbstractSvnScmProvider) scmProvider;
 262  0
                 String remoteUrl = ( (SvnScmProviderRepository) scmRepository.getProviderRepository() ).getUrl();
 263  0
                 boolean remoteExists = svnScmProvider.remoteUrlExist( scmRepository.getProviderRepository(), null );
 264  
 
 265  0
                 if ( !remoteExists )
 266  
                 {
 267  0
                     if ( automaticRemotePathCreation )
 268  
                     {
 269  0
                         logInfo( "remote url %s not exists so create it", remoteUrl );
 270  
 
 271  
                         // create a temporary directory for svnexec
 272  0
                         baseDir = File.createTempFile( "scm", "tmp" );
 273  0
                         baseDir.delete();
 274  0
                         baseDir.mkdirs();
 275  
                         // to prevent fileSet cannot be empty
 276  0
                         ScmFileSet scmFileSet = new ScmFileSet( baseDir, new File( "" ) );
 277  
 
 278  0
                         CommandParameters commandParameters = new CommandParameters();
 279  0
                         commandParameters.setString( CommandParameter.SCM_MKDIR_CREATE_IN_LOCAL,
 280  
                                                      Boolean.FALSE.toString() );
 281  0
                         commandParameters.setString( CommandParameter.MESSAGE,
 282  
                                                      "automatic path creation: " + remoteUrl );
 283  0
                         svnScmProvider.mkdir( scmRepository.getProviderRepository(), scmFileSet, commandParameters );
 284  
 
 285  
                         // new remote url so force checkout !
 286  0
                         if ( checkoutDirectory.exists() )
 287  
                         {
 288  0
                             FileUtils.deleteDirectory( checkoutDirectory );
 289  
                         }
 290  0
                     }
 291  
                     else
 292  
                     {
 293  
                         //olamy: return ?? that will fail during checkout IMHO :-)
 294  0
                         logInfo( "remote url %s not exists and not create it as field %s configured to false",
 295  
                                  remoteUrl, Boolean.toString( automaticRemotePathCreation ) );
 296  
                     }
 297  
                 }
 298  
             }
 299  0
             catch ( IOException e )
 300  
             {
 301  0
                 throw new MojoExecutionException( e.getMessage(), e );
 302  
             }
 303  0
             catch ( ScmException e )
 304  
             {
 305  0
                 throw new MojoExecutionException( e.getMessage(), e );
 306  
             }
 307  
 
 308  
             finally
 309  
 
 310  
             {
 311  0
                 if ( baseDir != null )
 312  
                 {
 313  
                     try
 314  
                     {
 315  0
                         FileUtils.forceDeleteOnExit( baseDir );
 316  
                     }
 317  0
                     catch ( IOException e )
 318  
                     {
 319  0
                         throw new MojoExecutionException( e.getMessage(), e );
 320  0
                     }
 321  
                 }
 322  
             }
 323  
         }
 324  
 
 325  0
         logInfo( "%s the pub tree from  %s ...", ( tryUpdate ? "Updating" : "Checking out" ), pubScmUrl );
 326  
 
 327  0
         if ( checkoutDirectory.exists() && !tryUpdate )
 328  
 
 329  
         {
 330  
             try
 331  
             {
 332  0
                 FileUtils.deleteDirectory( checkoutDirectory );
 333  
             }
 334  0
             catch ( IOException e )
 335  
             {
 336  0
                 logError( e.getMessage() );
 337  
 
 338  0
                 throw new MojoExecutionException( "Unable to remove old checkout directory: " + e.getMessage(), e );
 339  0
             }
 340  
         }
 341  
 
 342  0
         boolean forceCheckout = false;
 343  
 
 344  0
         if ( !checkoutDirectory.exists() )
 345  
 
 346  
         {
 347  0
             if ( tryUpdate )
 348  
             {
 349  0
                 logInfo( "tryUpdate is configured but no local copy currently available so forcing checkout" );
 350  
             }
 351  0
             checkoutDirectory.mkdirs();
 352  0
             forceCheckout = true;
 353  
         }
 354  
 
 355  
         ScmResult scmResult;
 356  
 
 357  
         try
 358  
 
 359  
         {
 360  0
             ScmFileSet fileSet = new ScmFileSet( checkoutDirectory, includes, excludes );
 361  0
             if ( tryUpdate && !forceCheckout )
 362  
             {
 363  0
                 scmResult = scmProvider.update( scmRepository, fileSet );
 364  
             }
 365  
             else
 366  
             {
 367  0
                 if ( scmBranch == null )
 368  
                 {
 369  0
                     scmResult = scmProvider.checkOut( scmRepository, fileSet );
 370  
                 }
 371  
                 else
 372  
                 {
 373  
 
 374  0
                     ScmBranch scmBranch = new ScmBranch( this.scmBranch );
 375  0
                     scmResult = scmProvider.checkOut( scmRepository, fileSet, scmBranch );
 376  
                 }
 377  
             }
 378  
         }
 379  
 
 380  0
         catch ( ScmException e )
 381  
         {
 382  0
             logError( e.getMessage() );
 383  
 
 384  0
             throw new MojoExecutionException( "An error is occurred in the checkout process: " + e.getMessage(), e );
 385  
         }
 386  
 
 387  0
         catch ( IOException e )
 388  
         {
 389  0
             logError( e.getMessage() );
 390  
 
 391  0
             throw new MojoExecutionException( "An error is occurred in the checkout process: " + e.getMessage(), e );
 392  0
         }
 393  
 
 394  0
         if ( !scmResult.isSuccess() )
 395  
 
 396  
         {
 397  0
             logError( scmResult.getProviderMessage() );
 398  
 
 399  0
             throw new MojoExecutionException(
 400  
                 "Unable to checkout from SCM" + "\nProvider message:\n" + scmResult.getProviderMessage()
 401  
                     + "\nCommand output:\n" + scmResult.getCommandOutput() );
 402  
         }
 403  
 
 404  0
     }
 405  
 
 406  
     public void execute()
 407  
         throws MojoExecutionException, MojoFailureException
 408  
     {
 409  
         // setup the scm plugin with help from release plugin utilities
 410  
         try
 411  
         {
 412  0
             setupScm();
 413  
         }
 414  0
         catch ( ScmRepositoryException e )
 415  
         {
 416  0
             throw new MojoExecutionException( e.getMessage(), e );
 417  
         }
 418  0
         catch ( NoSuchScmProviderException e )
 419  
         {
 420  0
             throw new MojoExecutionException( e.getMessage(), e );
 421  0
         }
 422  
 
 423  0
         boolean tmpCheckout = false;
 424  
 
 425  0
         if ( checkoutDirectory.getPath().contains( "${project." ) )
 426  
         {
 427  
             try
 428  
             {
 429  0
                 tmpCheckout = true;
 430  0
                 checkoutDirectory = File.createTempFile( "maven-scm-publish", ".checkout" );
 431  0
                 checkoutDirectory.delete();
 432  0
                 checkoutDirectory.mkdir();
 433  
             }
 434  0
             catch ( IOException ioe )
 435  
             {
 436  0
                 throw new MojoExecutionException( ioe.getMessage(), ioe );
 437  0
             }
 438  
         }
 439  
 
 440  
         try
 441  
         {
 442  0
             scmPublishExecute();
 443  
         }
 444  
         finally
 445  
         {
 446  0
             if ( tmpCheckout )
 447  
             {
 448  0
                 FileUtils.deleteQuietly( checkoutDirectory );
 449  
             }
 450  
         }
 451  0
     }
 452  
 
 453  
     public abstract void scmPublishExecute()
 454  
         throws MojoExecutionException, MojoFailureException;
 455  
 }