Coverage Report - org.apache.maven.plugins.site.SiteStageDeployMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
SiteStageDeployMojo
0%
0/61
0%
0/12
6,5
 
 1  
 package org.apache.maven.plugins.site;
 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.artifact.manager.WagonConfigurationException;
 23  
 import org.apache.maven.artifact.manager.WagonManager;
 24  
 import org.apache.maven.plugin.MojoExecutionException;
 25  
 import org.apache.maven.plugin.MojoFailureException;
 26  
 import org.apache.maven.project.MavenProject;
 27  
 import org.apache.maven.settings.Settings;
 28  
 import org.apache.maven.wagon.ConnectionException;
 29  
 import org.apache.maven.wagon.ResourceDoesNotExistException;
 30  
 import org.apache.maven.wagon.TransferFailedException;
 31  
 import org.apache.maven.wagon.UnsupportedProtocolException;
 32  
 import org.apache.maven.wagon.Wagon;
 33  
 import org.apache.maven.wagon.authentication.AuthenticationException;
 34  
 import org.apache.maven.wagon.authorization.AuthorizationException;
 35  
 import org.apache.maven.wagon.observers.Debug;
 36  
 import org.apache.maven.wagon.proxy.ProxyInfo;
 37  
 import org.apache.maven.wagon.repository.Repository;
 38  
 import org.codehaus.plexus.PlexusConstants;
 39  
 import org.codehaus.plexus.PlexusContainer;
 40  
 import org.codehaus.plexus.context.Context;
 41  
 import org.codehaus.plexus.context.ContextException;
 42  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
 43  
 
 44  
 import java.io.File;
 45  
 import java.util.List;
 46  
 
 47  
 /**
 48  
  * Deploys the generated site to a staging or mock directory to the site URL
 49  
  * specified in the <code>&lt;distributionManagement&gt;</code> section of the
 50  
  * POM. It supports <code>scp</code> and <code>file</code> protocols for
 51  
  * deployment.
 52  
  *
 53  
  * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
 54  
  * @version $Id$
 55  
  * @goal stage-deploy
 56  
  * @requiresDependencyResolution test
 57  
  */
 58  0
 public class SiteStageDeployMojo
 59  
     extends SiteStageMojo implements Contextualizable
 60  
 {
 61  
     /**
 62  
      * The staged site will be deployed to this URL.
 63  
      *
 64  
      * If you don't specify this, the default-value will be
 65  
      * "${project.distributionManagement.site.url}/staging", where "project" is
 66  
      * either the current project or, in a reactor build, the top level project
 67  
      * in the reactor.
 68  
      *
 69  
      * @parameter expression="${stagingSiteURL}"
 70  
      * @see <a href="http://maven.apache.org/maven-model/maven.html#class_site">MavenModel#class_site</a>
 71  
      */
 72  
     private String stagingSiteURL;
 73  
 
 74  
     /**
 75  
      * @component
 76  
      */
 77  
     private WagonManager wagonManager;
 78  
 
 79  
     /**
 80  
      * The current user system settings for use in Maven.
 81  
      *
 82  
      * @parameter expression="${settings}"
 83  
      * @required
 84  
      * @readonly
 85  
      */
 86  
     private Settings settings;
 87  
 
 88  
     private PlexusContainer container;
 89  
 
 90  
     private static final String STAGING_SERVER_ID = "stagingSite";
 91  
 
 92  
     /**
 93  
      * @see org.apache.maven.plugin.Mojo#execute()
 94  
      */
 95  
     public void execute()
 96  
         throws MojoExecutionException, MojoFailureException
 97  
     {
 98  0
         super.execute();
 99  
 
 100  0
         deployStagingSite();
 101  0
     }
 102  
 
 103  
     /**
 104  
      * Deploy the staging directory using the stagingSiteURL.
 105  
      *
 106  
      * @throws org.apache.maven.plugin.MojoExecutionException
 107  
      *          if any
 108  
      * @throws org.apache.maven.plugin.MojoFailureException
 109  
      *          if any
 110  
      */
 111  
     private void deployStagingSite()
 112  
         throws MojoExecutionException, MojoFailureException
 113  
     {
 114  0
         stagingSiteURL = getStagingSiteURL( project, reactorProjects, stagingSiteURL );
 115  0
         getLog().info( "Using this URL for staging: " + stagingSiteURL );
 116  
 
 117  0
         Repository repository = new Repository( STAGING_SERVER_ID, stagingSiteURL );
 118  
 
 119  
         Wagon wagon;
 120  
         try
 121  
         {
 122  0
             wagon = wagonManager.getWagon( repository );
 123  0
             SiteDeployMojo.configureWagon( wagon, STAGING_SERVER_ID, settings, container, getLog() );
 124  
         }
 125  0
         catch ( UnsupportedProtocolException e )
 126  
         {
 127  0
             throw new MojoExecutionException( "Unsupported protocol: '" + repository.getProtocol() + "'", e );
 128  
         }
 129  0
         catch ( WagonConfigurationException e )
 130  
         {
 131  0
             throw new MojoExecutionException( "Unable to configure Wagon: '" + repository.getProtocol() + "'", e );
 132  0
         }
 133  
 
 134  0
         if ( !wagon.supportsDirectoryCopy() )
 135  
         {
 136  0
             throw new MojoExecutionException(
 137  
                 "Wagon protocol '" + repository.getProtocol() + "' doesn't support directory copying" );
 138  
         }
 139  
 
 140  
         try
 141  
         {
 142  0
             Debug debug = new Debug();
 143  
 
 144  0
             wagon.addSessionListener( debug );
 145  
 
 146  0
             wagon.addTransferListener( debug );
 147  
 
 148  0
             ProxyInfo proxyInfo = SiteDeployMojo.getProxyInfo( repository, wagonManager );
 149  0
             if ( proxyInfo != null )
 150  
             {
 151  0
                 wagon.connect( repository, wagonManager.getAuthenticationInfo( STAGING_SERVER_ID ), proxyInfo );
 152  
             }
 153  
             else
 154  
             {
 155  0
                 wagon.connect( repository, wagonManager.getAuthenticationInfo( STAGING_SERVER_ID ) );
 156  
             }
 157  
 
 158  0
             wagon.putDirectory( new File( stagingDirectory, getStructure( project, false ) ), "." );
 159  0
         }
 160  0
         catch ( ResourceDoesNotExistException e )
 161  
         {
 162  0
             throw new MojoExecutionException( "Error uploading site", e );
 163  
         }
 164  0
         catch ( TransferFailedException e )
 165  
         {
 166  0
             throw new MojoExecutionException( "Error uploading site", e );
 167  
         }
 168  0
         catch ( AuthorizationException e )
 169  
         {
 170  0
             throw new MojoExecutionException( "Error uploading site", e );
 171  
         }
 172  0
         catch ( ConnectionException e )
 173  
         {
 174  0
             throw new MojoExecutionException( "Error uploading site", e );
 175  
         }
 176  0
         catch ( AuthenticationException e )
 177  
         {
 178  0
             throw new MojoExecutionException( "Error uploading site", e );
 179  
         }
 180  
         finally
 181  
         {
 182  0
             try
 183  
             {
 184  0
                 wagon.disconnect();
 185  
             }
 186  0
             catch ( ConnectionException e )
 187  
             {
 188  0
                 getLog().error( "Error disconnecting wagon - ignored", e );
 189  0
             }
 190  0
         }
 191  0
     }
 192  
 
 193  
     public void contextualize( Context context )
 194  
         throws ContextException
 195  
     {
 196  0
         container = (PlexusContainer) context.get( PlexusConstants.PLEXUS_KEY );
 197  0
     }
 198  
 
 199  
     /**
 200  
      * Find the URL where staging will take place.
 201  
      *
 202  
      * @param currentProject      The currently executing project
 203  
      * @param reactorProjects     The projects in the reactor
 204  
      * @param usersStagingSiteURL The staging site URL as suggested by the user's configuration
 205  
      * @return the site URL for staging
 206  
      */
 207  
     protected String getStagingSiteURL( MavenProject currentProject, List reactorProjects, String usersStagingSiteURL )
 208  
     {
 209  0
         String topLevelURL = null;
 210  0
         String relative = "";
 211  
 
 212  
         // If the user has specified a stagingSiteURL - use it
 213  0
         if ( usersStagingSiteURL != null )
 214  
         {
 215  0
             getLog().debug( "stagingSiteURL specified by the user." );
 216  0
             topLevelURL = usersStagingSiteURL;
 217  
         }
 218  0
         getLog().debug( "stagingSiteURL NOT specified by the user." );
 219  
 
 220  
         // Find the top level project in the reactor
 221  0
         MavenProject topLevelProject = getTopLevelProject( reactorProjects );
 222  
 
 223  
         // Take the distributionManagement site url from the top level project,
 224  
         // if there is one, otherwise take it from the current project
 225  0
         if ( topLevelProject == null )
 226  
         {
 227  0
             if ( topLevelURL == null )
 228  
             {
 229  
                 // The user didn't specify a URL and there is no top level project in the reactor
 230  
                 // Use current project
 231  0
                 getLog().debug( "No top level project found in the reactor, using the current project." );
 232  0
                 topLevelURL =
 233  
                     currentProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY;
 234  
             }
 235  
         }
 236  
         else
 237  
         {
 238  
             // Find the relative path between the parent and child distribution URLs, if any
 239  0
             relative = "/" + siteTool.getRelativePath( currentProject.getDistributionManagement().getSite().getUrl(),
 240  
                                                        topLevelProject.getDistributionManagement().getSite().getUrl() );
 241  
             // SiteTool.getRelativePath() uses File.separatorChar, so we need to convert '\' to '/' in order for the URL
 242  
             // to be valid for Windows users
 243  0
             relative = relative.replace( '\\', '/' );
 244  
 
 245  0
             if ( topLevelURL == null )
 246  
             {
 247  
                 // The user didn't specify a URL and there is a top level project in the reactor
 248  
                 // Use the top level project
 249  0
                 getLog().debug( "Using the top level project found in the reactor." );
 250  0
                 topLevelURL =
 251  
                     topLevelProject.getDistributionManagement().getSite().getUrl() + "/" + DEFAULT_STAGING_DIRECTORY;
 252  
             }
 253  
         }
 254  
 
 255  
         // Return either
 256  
         //   usersURL + relative(from parent, to child)
 257  
         // or
 258  
         //   topLevelProjectURL + staging + relative(from parent, to child)
 259  0
         return topLevelURL + relative;
 260  
     }
 261  
 }