Coverage Report - org.apache.maven.plugins.scmpublish.ScmPublishPublishScmMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
ScmPublishPublishScmMojo
0 %
0/84
0 %
0/54
9
 
 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.IOUtils;
 24  
 import org.apache.commons.io.filefilter.NameFileFilter;
 25  
 import org.apache.commons.io.filefilter.NotFileFilter;
 26  
 import org.apache.maven.plugin.MojoExecutionException;
 27  
 import org.apache.maven.plugin.MojoFailureException;
 28  
 import org.apache.maven.plugins.annotations.Component;
 29  
 import org.apache.maven.plugins.annotations.Mojo;
 30  
 import org.apache.maven.plugins.annotations.Parameter;
 31  
 import org.apache.maven.project.MavenProject;
 32  
 import org.codehaus.plexus.util.MatchPatterns;
 33  
 
 34  
 import java.io.BufferedReader;
 35  
 import java.io.File;
 36  
 import java.io.FileInputStream;
 37  
 import java.io.FileOutputStream;
 38  
 import java.io.IOException;
 39  
 import java.io.InputStreamReader;
 40  
 import java.io.OutputStreamWriter;
 41  
 import java.io.PrintWriter;
 42  
 import java.util.ArrayList;
 43  
 import java.util.Arrays;
 44  
 import java.util.Collections;
 45  
 import java.util.HashSet;
 46  
 import java.util.List;
 47  
 import java.util.Set;
 48  
 
 49  
 /**
 50  
  * Publish a content to scm in one step. By default, content is taken from default site staging directory
 51  
  * <code>${project.build.directory}/staging</code>.
 52  
  * Can be used without project, so usable to update any SCM with any content.
 53  
  */
 54  
 @Mojo( name = "publish-scm", aggregator = true, requiresProject = false )
 55  0
 public class ScmPublishPublishScmMojo
 56  
     extends ScmPublishPublishMojo
 57  
 {
 58  
     /**
 59  
      * The content to be published.
 60  
      */
 61  
     @Parameter( property = "scmpublish.content", defaultValue = "${project.build.directory}/staging" )
 62  
     private File content;
 63  
 
 64  
     /**
 65  
      */
 66  
     @Component
 67  
     protected MavenProject project;
 68  
 
 69  0
     private List<File> deleted = new ArrayList<File>();
 70  
 
 71  0
     private List<File> added = new ArrayList<File>();
 72  
 
 73  0
     private List<File> updated = new ArrayList<File>();
 74  
 
 75  
     /**
 76  
      * Update scm checkout directory with content.
 77  
      *
 78  
      * @param checkout        the scm checkout directory
 79  
      * @param dir             the content to put in scm (can be <code>null</code>)
 80  
      * @param doNotDeleteDirs directory names that should not be deleted from scm even if not in new content:
 81  
      *                        used for modules, which content is available only when staging
 82  
      * @throws IOException
 83  
      */
 84  
     private void update( File checkout, File dir, List<String> doNotDeleteDirs )
 85  
         throws IOException
 86  
     {
 87  0
         String[] files =
 88  
             checkout.list( new NotFileFilter( new NameFileFilter( scmProvider.getScmSpecificFilename() ) ) );
 89  
 
 90  0
         Set<String> checkoutContent = new HashSet<String>( Arrays.asList( files ) );
 91  0
         List<String> dirContent = ( dir != null ) ? Arrays.asList( dir.list() ) : Collections.<String>emptyList();
 92  
 
 93  0
         Set<String> deleted = new HashSet<String>( checkoutContent );
 94  0
         deleted.removeAll( dirContent );
 95  
 
 96  0
         MatchPatterns ignoreDeleteMatchPatterns = null;
 97  0
         List<String> pathsAsList = new ArrayList<String>( 0 );
 98  0
         if ( ignorePathsToDelete != null && ignorePathsToDelete.length > 0 )
 99  
         {
 100  0
             ignoreDeleteMatchPatterns = MatchPatterns.from( ignorePathsToDelete );
 101  0
             pathsAsList = Arrays.asList( ignorePathsToDelete );
 102  
         }
 103  
 
 104  0
         for ( String name : deleted )
 105  
         {
 106  0
             if ( ignoreDeleteMatchPatterns != null && ignoreDeleteMatchPatterns.matches( name, true ) )
 107  
             {
 108  0
                 getLog().debug( name + " match one of the patterns '" + pathsAsList + "': do not add to deleted files" );
 109  0
                 continue;
 110  
             }
 111  0
             File file = new File( checkout, name );
 112  
 
 113  0
             if ( ( doNotDeleteDirs != null ) && file.isDirectory() && ( doNotDeleteDirs.contains( name ) ) )
 114  
             {
 115  
                 // ignore directory not available
 116  0
                 continue;
 117  
             }
 118  
 
 119  0
             if ( file.isDirectory() )
 120  
             {
 121  0
                 update( file, null, null );
 122  
             }
 123  0
             this.deleted.add( file );
 124  0
         }
 125  
 
 126  0
         for ( String name : dirContent )
 127  
         {
 128  0
             File file = new File( checkout, name );
 129  0
             File source = new File( dir, name );
 130  
 
 131  0
             if ( source.isDirectory() )
 132  
             {
 133  0
                 if ( !checkoutContent.contains( name ) )
 134  
                 {
 135  0
                     this.added.add( file );
 136  0
                     file.mkdir();
 137  
                 }
 138  
 
 139  0
                 update( file, source, null );
 140  
             }
 141  
             else
 142  
             {
 143  0
                 if ( checkoutContent.contains( name ) )
 144  
                 {
 145  0
                     this.updated.add( file );
 146  
                 }
 147  
                 else
 148  
                 {
 149  0
                     this.added.add( file );
 150  
                 }
 151  
 
 152  0
                 copyFile( source, file );
 153  
             }
 154  0
         }
 155  0
     }
 156  
 
 157  
     /**
 158  
      * Copy a file content, normalizing newlines when necessary.
 159  
      *
 160  
      * @param srcFile  the source file
 161  
      * @param destFile the destination file
 162  
      * @throws IOException
 163  
      * @see #requireNormalizeNewlines(File)
 164  
      */
 165  
     private void copyFile( File srcFile, File destFile )
 166  
         throws IOException
 167  
     {
 168  0
         if ( requireNormalizeNewlines( srcFile ) )
 169  
         {
 170  0
             copyAndNormalizeNewlines( srcFile, destFile );
 171  
         }
 172  
         else
 173  
         {
 174  0
             FileUtils.copyFile( srcFile, destFile );
 175  
         }
 176  0
     }
 177  
 
 178  
     /**
 179  
      * Copy and normalize newlines.
 180  
      *
 181  
      * @param srcFile  the source file
 182  
      * @param destFile the destination file
 183  
      * @throws IOException
 184  
      */
 185  
     private void copyAndNormalizeNewlines( File srcFile, File destFile )
 186  
         throws IOException
 187  
     {
 188  0
         BufferedReader in = null;
 189  0
         PrintWriter out = null;
 190  
         try
 191  
         {
 192  0
             in = new BufferedReader( new InputStreamReader( new FileInputStream( srcFile ), siteOutputEncoding ) );
 193  0
             out = new PrintWriter( new OutputStreamWriter( new FileOutputStream( destFile ), siteOutputEncoding ) );
 194  
             String line;
 195  0
             while ( ( line = in.readLine() ) != null )
 196  
             {
 197  0
                 if ( in.ready() )
 198  
                 {
 199  0
                     out.println( line );
 200  
                 }
 201  
                 else
 202  
                 {
 203  0
                     out.print( line );
 204  
                 }
 205  
             }
 206  
         }
 207  
         finally
 208  
         {
 209  0
             IOUtils.closeQuietly( out );
 210  0
             IOUtils.closeQuietly( in );
 211  0
         }
 212  0
     }
 213  
 
 214  
     public void scmPublishExecute()
 215  
         throws MojoExecutionException, MojoFailureException
 216  
     {
 217  0
         if ( siteOutputEncoding == null )
 218  
         {
 219  0
             getLog().warn( "No output encoding, defaulting to UTF-8." );
 220  0
             siteOutputEncoding = "utf-8";
 221  
         }
 222  
 
 223  0
         if ( !content.exists() )
 224  
         {
 225  0
             throw new MojoExecutionException( "Configured content directory does not exist: " + content );
 226  
         }
 227  
 
 228  0
         if ( !content.canRead() )
 229  
         {
 230  0
             throw new MojoExecutionException( "Can't read content directory: " + content );
 231  
         }
 232  
 
 233  0
         checkoutExisting();
 234  
 
 235  
         try
 236  
         {
 237  0
             logInfo( "Updating content..." );
 238  0
             update( checkoutDirectory, content, ( project == null ) ? null : project.getModel().getModules() );
 239  
         }
 240  0
         catch ( IOException ioe )
 241  
         {
 242  0
             throw new MojoExecutionException( "Could not copy content to scm checkout", ioe );
 243  0
         }
 244  
 
 245  0
         logInfo( "Publish files: %d addition(s), %d update(s), %d delete(s)", added.size(), updated.size(),
 246  
                  deleted.size() );
 247  
 
 248  0
         if ( isDryRun() )
 249  
         {
 250  0
             for ( File addedFile : added )
 251  
             {
 252  0
                 logInfo( "Added %s", addedFile.getAbsolutePath() );
 253  
             }
 254  0
             for ( File deletedFile : deleted )
 255  
             {
 256  0
                 logInfo( "Deleted %s", deletedFile.getAbsolutePath() );
 257  
             }
 258  0
             for ( File updatedFile : updated )
 259  
             {
 260  0
                 logInfo( "Updated %s", updatedFile.getAbsolutePath() );
 261  
             }
 262  0
             return;
 263  
         }
 264  
 
 265  0
         if ( !added.isEmpty() )
 266  
         {
 267  0
             addFiles( added );
 268  
         }
 269  
 
 270  0
         if ( !deleted.isEmpty() )
 271  
         {
 272  0
             deleteFiles( deleted );
 273  
         }
 274  
 
 275  0
         logInfo( "Checking in SCM..." );
 276  0
         checkinFiles();
 277  0
     }
 278  
 }