Coverage Report - org.apache.maven.plugin.deploy.AbstractDeployMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractDeployMojo
67%
21/31
35%
5/14
2.571
 
 1  
 package org.apache.maven.plugin.deploy;
 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 java.io.File;
 23  
 import java.util.Map;
 24  
 
 25  
 import org.apache.maven.artifact.Artifact;
 26  
 import org.apache.maven.artifact.deployer.ArtifactDeployer;
 27  
 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
 28  
 import org.apache.maven.artifact.factory.ArtifactFactory;
 29  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 30  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 31  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 32  
 import org.apache.maven.plugin.AbstractMojo;
 33  
 import org.apache.maven.plugin.MojoExecutionException;
 34  
 import org.apache.maven.plugin.MojoFailureException;
 35  
 
 36  
 /**
 37  
  * @version $Id: AbstractDeployMojo.java 1137869 2011-06-21 06:18:10Z stephenc $
 38  
  */
 39  42
 public abstract class AbstractDeployMojo
 40  
     extends AbstractMojo
 41  
 {
 42  
     /**
 43  
      * @component
 44  
      */
 45  
     private ArtifactDeployer deployer;
 46  
 
 47  
     /**
 48  
      * Component used to create an artifact.
 49  
      *
 50  
      * @component
 51  
      */
 52  
     protected ArtifactFactory artifactFactory;
 53  
 
 54  
     /**
 55  
      * Component used to create a repository.
 56  
      *
 57  
      * @component
 58  
      */
 59  
     ArtifactRepositoryFactory repositoryFactory;
 60  
 
 61  
     /**
 62  
      * Map that contains the layouts.
 63  
      *
 64  
      * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
 65  
      */
 66  
     private Map repositoryLayouts;
 67  
 
 68  
     /**
 69  
      * @parameter default-value="${localRepository}"
 70  
      * @required
 71  
      * @readonly
 72  
      */
 73  
     private ArtifactRepository localRepository;
 74  
 
 75  
     /**
 76  
      * Flag whether Maven is currently in online/offline mode.
 77  
      * 
 78  
      * @parameter default-value="${settings.offline}"
 79  
      * @readonly
 80  
      */
 81  
     private boolean offline;
 82  
 
 83  
     /**
 84  
      * Parameter used to update the metadata to make the artifact as release.
 85  
      * 
 86  
      * @parameter expression="${updateReleaseInfo}" default-value="false"
 87  
      */
 88  
     protected boolean updateReleaseInfo;
 89  
 
 90  
     /**
 91  
      * Parameter used to control how many times a failed deployment will be retried before giving up and failing.
 92  
      * If a value outside the range 1-10 is specified it will be pulled to the nearest value within the range 1-10.
 93  
      *
 94  
      * @parameter expression="${retryFailedDeploymentCount}" default-value="1"
 95  
      * @since 2.7
 96  
      */
 97  
     private int retryFailedDeploymentCount;
 98  
 
 99  
     /* Setters and Getters */
 100  
 
 101  
     public ArtifactDeployer getDeployer()
 102  
     {
 103  24
         return deployer;
 104  
     }
 105  
 
 106  
     public void setDeployer( ArtifactDeployer deployer )
 107  
     {
 108  2
         this.deployer = deployer;
 109  2
     }
 110  
 
 111  
     public ArtifactRepository getLocalRepository()
 112  
     {
 113  48
         return localRepository;
 114  
     }
 115  
 
 116  
     public void setLocalRepository( ArtifactRepository localRepository )
 117  
     {
 118  28
         this.localRepository = localRepository;
 119  28
     }
 120  
 
 121  
     void failIfOffline()
 122  
         throws MojoFailureException
 123  
     {
 124  24
         if ( offline )
 125  
         {
 126  0
             throw new MojoFailureException( "Cannot deploy artifacts when Maven is in offline mode" );
 127  
         }
 128  24
     }
 129  
 
 130  
     ArtifactRepositoryLayout getLayout( String id )
 131  
         throws MojoExecutionException
 132  
     {
 133  12
         ArtifactRepositoryLayout layout = (ArtifactRepositoryLayout) repositoryLayouts.get( id );
 134  
 
 135  12
         if ( layout == null )
 136  
         {
 137  0
             throw new MojoExecutionException( "Invalid repository layout: " + id );
 138  
         }
 139  
 
 140  12
         return layout;
 141  
     }
 142  
 
 143  
     /**
 144  
      * Deploy an artifact from a particular file.
 145  
      *
 146  
      * @param source the file to deploy
 147  
      * @param artifact the artifact definition
 148  
      * @param deploymentRepository the repository to deploy to
 149  
      * @param localRepository the local repository to install into
 150  
      * @throws ArtifactDeploymentException if an error occurred deploying the artifact
 151  
      */
 152  
     protected void deploy( File source, Artifact artifact, ArtifactRepository deploymentRepository,
 153  
                            ArtifactRepository localRepository )
 154  
         throws ArtifactDeploymentException
 155  
     {
 156  24
         int retryFailedDeploymentCount = Math.max( 1, Math.min( 10, this.retryFailedDeploymentCount ) );
 157  24
         ArtifactDeploymentException exception = null;
 158  24
         for ( int count = 0; count < retryFailedDeploymentCount; count++ )
 159  
         {
 160  
             try
 161  
             {
 162  24
                 if (count > 0)
 163  
                 {
 164  0
                     getLog().info(
 165  
                         "Retrying deployment attempt " + ( count + 1 ) + " of " + retryFailedDeploymentCount );
 166  
                 }
 167  24
                 getDeployer().deploy( source, artifact, deploymentRepository, localRepository );
 168  24
                 exception = null;
 169  24
                 break;
 170  
             }
 171  0
             catch ( ArtifactDeploymentException e )
 172  
             {
 173  0
                 if (count + 1 < retryFailedDeploymentCount) {
 174  0
                     getLog().warn( "Encountered issue during deployment: " + e.getLocalizedMessage());
 175  0
                     getLog().debug( e );
 176  
                 }
 177  0
                 if ( exception == null )
 178  
                 {
 179  0
                     exception = e;
 180  
                 }
 181  
             }
 182  
         }
 183  24
         if ( exception != null )
 184  
         {
 185  0
             throw exception;
 186  
         }
 187  24
     }
 188  
 }