Coverage Report - org.apache.maven.plugins.scmpublish.ScmPublishPublishMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmPublishPublishMojo
0 %
0/120
0 %
0/46
5,222
 
 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.commons.io.FilenameUtils;
 24  
 import org.apache.commons.io.IOUtils;
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 import org.apache.maven.plugin.MojoFailureException;
 27  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 28  
 import org.apache.maven.plugins.annotations.Mojo;
 29  
 import org.apache.maven.plugins.annotations.Parameter;
 30  
 import org.apache.maven.scm.CommandParameter;
 31  
 import org.apache.maven.scm.CommandParameters;
 32  
 import org.apache.maven.scm.ScmBranch;
 33  
 import org.apache.maven.scm.ScmException;
 34  
 import org.apache.maven.scm.ScmFileSet;
 35  
 import org.apache.maven.scm.command.add.AddScmResult;
 36  
 import org.apache.maven.scm.command.checkin.CheckInScmResult;
 37  
 import org.apache.maven.scm.command.remove.RemoveScmResult;
 38  
 
 39  
 import java.io.BufferedReader;
 40  
 import java.io.File;
 41  
 import java.io.FileInputStream;
 42  
 import java.io.FileOutputStream;
 43  
 import java.io.IOException;
 44  
 import java.io.InputStreamReader;
 45  
 import java.io.OutputStreamWriter;
 46  
 import java.io.PrintWriter;
 47  
 import java.util.ArrayList;
 48  
 import java.util.Arrays;
 49  
 import java.util.Collection;
 50  
 import java.util.HashSet;
 51  
 import java.util.List;
 52  
 import java.util.Set;
 53  
 import java.util.TreeSet;
 54  
 
 55  
 /**
 56  
  * Compare the list of files now on disk to the original inventory, then fire off scm adds and deletes as needed.
 57  
  *
 58  
  * @deprecated superseded by publish-scm which does the same in on step only and has more features
 59  
  */
 60  
 @Mojo ( name = "publish", defaultPhase = LifecyclePhase.POST_SITE, aggregator = true )
 61  0
 public class ScmPublishPublishMojo
 62  
     extends AbstractScmPublishMojo
 63  
 {
 64  
 
 65  
     /**
 66  
      * Display list of added, deleted, and changed files, but do not do any actual SCM operations.
 67  
      */
 68  
     @Parameter ( property = "scmpublish.dryRun" )
 69  
     private boolean dryRun;
 70  
 
 71  
     /**
 72  
      * Run add and delete commands, but leave the actually checkin for the user to run manually.
 73  
      */
 74  
     @Parameter ( property = "scmpublish.skipCheckin" )
 75  
     private boolean skipCheckin;
 76  
 
 77  
     /**
 78  
      * SCM log/checkin comment for this publication.
 79  
      */
 80  
     @Parameter ( property = "scmpublish.checkinComment", defaultValue = "Site checkin for project ${project.name}" )
 81  
     private String checkinComment;
 82  
 
 83  
     /**
 84  
      * Filename extensions of files which need new line normalization.
 85  
      */
 86  0
     private final static String[] NORMALIZE_EXTENSIONS = { "html", "css", "js" };
 87  
 
 88  
     /**
 89  
      * extra file extensions to normalize line ending (will be added to list html,css,js)
 90  
      */
 91  
     @Parameter
 92  
     protected String[] extraNormalizeExtensions;
 93  
 
 94  
     private File relativize( File base, File file )
 95  
     {
 96  0
         return new File( base.toURI().relativize( file.toURI() ).getPath() );
 97  
     }
 98  
 
 99  
     protected boolean requireNormalizeNewlines( File f )
 100  
         throws IOException
 101  
     {
 102  0
         List<String> extensions = Arrays.asList( NORMALIZE_EXTENSIONS );
 103  0
         if ( extraNormalizeExtensions != null )
 104  
         {
 105  0
             extensions.addAll( Arrays.asList( extraNormalizeExtensions ) );
 106  
         }
 107  
 
 108  0
         return FilenameUtils.isExtension( f.getName(), extensions );
 109  
     }
 110  
 
 111  
     private void normalizeNewlines( File f )
 112  
         throws IOException
 113  
     {
 114  0
         File tmpFile = null;
 115  0
         BufferedReader in = null;
 116  0
         PrintWriter out = null;
 117  
         try
 118  
         {
 119  0
             tmpFile = File.createTempFile( "maven-site-scm-plugin-", ".tmp" );
 120  0
             FileUtils.copyFile( f, tmpFile );
 121  0
             in = new BufferedReader( new InputStreamReader( new FileInputStream( tmpFile ), siteOutputEncoding ) );
 122  0
             out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( f ), siteOutputEncoding ) );
 123  
             String line;
 124  0
             while ( ( line = in.readLine() ) != null )
 125  
             {
 126  0
                 if ( in.ready() )
 127  
                 {
 128  0
                     out.println( line );
 129  
                 }
 130  
                 else
 131  
                 {
 132  0
                     out.print( line );
 133  
                 }
 134  
             }
 135  
         }
 136  
         finally
 137  
         {
 138  0
             IOUtils.closeQuietly( out );
 139  0
             IOUtils.closeQuietly( in );
 140  0
             FileUtils.deleteQuietly( tmpFile );
 141  0
         }
 142  0
     }
 143  
 
 144  
     private void normalizeNewLines( Set<File> files )
 145  
         throws MojoFailureException
 146  
     {
 147  0
         for ( File f : files )
 148  
         {
 149  
             try
 150  
             {
 151  0
                 if ( requireNormalizeNewlines( f ) )
 152  
                 {
 153  0
                     normalizeNewlines( f );
 154  
                 }
 155  
             }
 156  0
             catch ( IOException e )
 157  
             {
 158  0
                 throw new MojoFailureException( "Failed to normalize newlines in " + f.getAbsolutePath(), e );
 159  0
             }
 160  
         }
 161  0
     }
 162  
 
 163  
     public void scmPublishExecute()
 164  
         throws MojoExecutionException, MojoFailureException
 165  
     {
 166  0
         if ( siteOutputEncoding == null )
 167  
         {
 168  0
             getLog().warn( "No output encoding, defaulting to UTF-8." );
 169  0
             siteOutputEncoding = "utf-8";
 170  
         }
 171  
 
 172  
         // read in the list left behind by prepare; fail if it's not there.
 173  0
         List<File> inventory = ScmPublishInventory.readInventory( inventoryFile );
 174  
 
 175  
         // what files are in stock now?
 176  0
         Collection<File> newInventory =
 177  
             ScmPublishInventory.listInventoryFiles( checkoutDirectory, scmProvider.getScmSpecificFilename() );
 178  
 
 179  0
         Set<File> deleted = new HashSet<File>( inventory );
 180  0
         deleted.removeAll( newInventory ); // old - new = deleted. (Added is the complete new inventory at this point.)
 181  
 
 182  0
         Set<File> added = new HashSet<File>( newInventory );
 183  0
         added.removeAll( inventory ); // new - old = added.
 184  
 
 185  0
         Set<File> updated = new HashSet<File>( newInventory );
 186  0
         updated.retainAll( inventory ); // set intersection
 187  
 
 188  0
         logInfo( "Publish files: %d addition(s), %d update(s), %d delete(s)", added.size(), updated.size(),
 189  
                  deleted.size() );
 190  
 
 191  0
         if ( isDryRun() )
 192  
         {
 193  0
             for ( File addedFile : added )
 194  
             {
 195  0
                 logInfo( "Added %s", addedFile.getAbsolutePath() );
 196  
             }
 197  0
             for ( File deletedFile : deleted )
 198  
             {
 199  0
                 logInfo( "Deleted %s", deletedFile.getAbsolutePath() );
 200  
             }
 201  0
             for ( File updatedFile : updated )
 202  
             {
 203  0
                 logInfo( "Updated %s", updatedFile.getAbsolutePath() );
 204  
             }
 205  0
             return;
 206  
         }
 207  
 
 208  0
         if ( !added.isEmpty() )
 209  
         {
 210  0
             normalizeNewLines( added );
 211  
 
 212  0
             addFiles( added );
 213  
         }
 214  
 
 215  0
         if ( !deleted.isEmpty() )
 216  
         {
 217  0
             deleteFiles( deleted );
 218  
         }
 219  
 
 220  0
         normalizeNewLines( updated );
 221  
 
 222  0
         checkinFiles();
 223  0
     }
 224  
 
 225  
     /**
 226  
      * Check-in content from scm checkout.
 227  
      *
 228  
      * @throws MojoExecutionException
 229  
      */
 230  
     protected void checkinFiles()
 231  
         throws MojoExecutionException
 232  
     {
 233  0
         if ( skipCheckin )
 234  
         {
 235  0
             return;
 236  
         }
 237  
 
 238  0
         ScmFileSet updatedFileSet = new ScmFileSet( checkoutDirectory );
 239  
         try
 240  
         {
 241  0
             logInfo( "Checkin to the scm" );
 242  
 
 243  0
             CheckInScmResult checkinResult =
 244  
                 scmProvider.checkIn( scmRepository, updatedFileSet, new ScmBranch( scmBranch ), checkinComment );
 245  0
             if ( !checkinResult.isSuccess() )
 246  
             {
 247  0
                 logError( "checkin operation failed: %s",
 248  
                           checkinResult.getProviderMessage() + " " + checkinResult.getCommandOutput() );
 249  0
                 throw new MojoExecutionException( "Failed to checkin files: " + checkinResult.getProviderMessage() + " "
 250  
                                                       + checkinResult.getCommandOutput() );
 251  
             }
 252  0
             logInfo( "Checkin %d file(s) to revision: %s", checkinResult.getCheckedInFiles().size(),
 253  
                      checkinResult.getScmRevision() );
 254  
         }
 255  0
         catch ( ScmException e )
 256  
         {
 257  0
             throw new MojoExecutionException( "Failed to perform checkin SCM", e );
 258  0
         }
 259  0
     }
 260  
 
 261  
     protected void deleteFiles( Collection<File> deleted )
 262  
         throws MojoExecutionException
 263  
     {
 264  0
         if ( skipDeletedFiles )
 265  
         {
 266  0
             logInfo( "deleting files is skipped" );
 267  0
             return;
 268  
         }
 269  0
         List<File> deletedList = new ArrayList<File>();
 270  0
         for ( File f : deleted )
 271  
         {
 272  0
             deletedList.add( relativize( checkoutDirectory, f ) );
 273  
         }
 274  0
         ScmFileSet deletedFileSet = new ScmFileSet( checkoutDirectory, deletedList );
 275  
         try
 276  
         {
 277  0
             getLog().debug( "deleting files: " + deletedList );
 278  
 
 279  0
             RemoveScmResult deleteResult =
 280  
                 scmProvider.remove( scmRepository, deletedFileSet, "Deleting obsolete site files." );
 281  0
             if ( !deleteResult.isSuccess() )
 282  
             {
 283  0
                 logError( "delete operation failed: %s",
 284  
                           deleteResult.getProviderMessage() + " " + deleteResult.getCommandOutput() );
 285  0
                 throw new MojoExecutionException( "Failed to delete files: " + deleteResult.getProviderMessage() + " "
 286  
                                                       + deleteResult.getCommandOutput() );
 287  
             }
 288  
         }
 289  0
         catch ( ScmException e )
 290  
         {
 291  0
             throw new MojoExecutionException( "Failed to delete removed files to SCM", e );
 292  0
         }
 293  0
     }
 294  
 
 295  
     /**
 296  
      * Add files to scm.
 297  
      *
 298  
      * @param added files to be added
 299  
      * @throws MojoFailureException
 300  
      * @throws MojoExecutionException
 301  
      */
 302  
     protected void addFiles( Collection<File> added )
 303  
         throws MojoFailureException, MojoExecutionException
 304  
     {
 305  0
         List<File> addedList = new ArrayList<File>();
 306  0
         Set<File> createdDirs = new HashSet<File>();
 307  0
         Set<File> dirsToAdd = new TreeSet<File>();
 308  
 
 309  0
         createdDirs.add( relativize( checkoutDirectory, checkoutDirectory ) );
 310  
 
 311  0
         for ( File f : added )
 312  
         {
 313  0
             for ( File dir = f.getParentFile(); !dir.equals( checkoutDirectory ); dir = dir.getParentFile() )
 314  
             {
 315  0
                 File relativized = relativize( checkoutDirectory, dir );
 316  
                 //  we do the best we can with the directories
 317  0
                 if ( createdDirs.add( relativized ) )
 318  
                 {
 319  0
                     dirsToAdd.add( relativized );
 320  
                 }
 321  
                 else
 322  
                 {
 323  
                     break;
 324  
                 }
 325  
             }
 326  0
             addedList.add( relativize( checkoutDirectory, f ) );
 327  
         }
 328  
 
 329  0
         for ( File relativized : dirsToAdd )
 330  
         {
 331  
             try
 332  
             {
 333  0
                 ScmFileSet fileSet = new ScmFileSet( checkoutDirectory, relativized );
 334  0
                 getLog().debug( "scm add directory: " + relativized );
 335  0
                 AddScmResult addDirResult = scmProvider.add( scmRepository, fileSet, "Adding directory" );
 336  0
                 if ( !addDirResult.isSuccess() )
 337  
                 {
 338  0
                     getLog().debug( " Error adding directory " + relativized + ": " + addDirResult.getCommandOutput() );
 339  
                 }
 340  
             }
 341  0
             catch ( ScmException e )
 342  
             {
 343  
                 //
 344  0
             }
 345  
         }
 346  
 
 347  
         // remove directories already added !
 348  0
         addedList.removeAll( dirsToAdd );
 349  
 
 350  0
         ScmFileSet addedFileSet = new ScmFileSet( checkoutDirectory, addedList );
 351  0
         getLog().debug( "scm add files: " + addedList );
 352  
         try
 353  
         {
 354  0
             CommandParameters commandParameters = new CommandParameters();
 355  0
             commandParameters.setString( CommandParameter.MESSAGE, "Adding new site files." );
 356  0
             commandParameters.setString( CommandParameter.FORCE_ADD, Boolean.TRUE.toString() );
 357  0
             AddScmResult addResult = scmProvider.add( scmRepository, addedFileSet, commandParameters );
 358  0
             if ( !addResult.isSuccess() )
 359  
             {
 360  0
                 logError( "add operation failed: %s",
 361  
                           addResult.getProviderMessage() + " " + addResult.getCommandOutput() );
 362  0
                 throw new MojoExecutionException(
 363  
                     "Failed to add new files: " + addResult.getProviderMessage() + " " + addResult.getCommandOutput() );
 364  
             }
 365  
         }
 366  0
         catch ( ScmException e )
 367  
         {
 368  0
             throw new MojoExecutionException( "Failed to add new files to SCM", e );
 369  0
         }
 370  0
     }
 371  
 
 372  
     public boolean isDryRun()
 373  
     {
 374  0
         return dryRun;
 375  
     }
 376  
 }