Coverage Report - org.apache.maven.plugin.deploy.DeployMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
DeployMojo
64%
38/59
66%
20/30
11.5
 
 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 org.apache.maven.artifact.Artifact;
 23  
 import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
 24  
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 26  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 27  
 import org.apache.maven.plugin.MojoExecutionException;
 28  
 import org.apache.maven.plugin.MojoFailureException;
 29  
 import org.apache.maven.project.MavenProject;
 30  
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 31  
 
 32  
 import java.io.File;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 import java.util.regex.Matcher;
 36  
 import java.util.regex.Pattern;
 37  
 
 38  
 /**
 39  
  * Deploys an artifact to remote repository.
 40  
  * 
 41  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 42  
  * @author <a href="mailto:jdcasey@apache.org">John Casey (refactoring only)</a>
 43  
  * @version $Id: DeployMojo.java 1160164 2011-08-22 09:38:32Z stephenc $
 44  
  * @goal deploy
 45  
  * @phase deploy
 46  
  * @threadSafe
 47  
  */
 48  8
 public class DeployMojo
 49  
     extends AbstractDeployMojo
 50  
 {
 51  
 
 52  1
     private static final Pattern ALT_REPO_SYNTAX_PATTERN = Pattern.compile( "(.+)::(.+)::(.+)" );
 53  
 
 54  
     /**
 55  
      * @parameter default-value="${project}"
 56  
      * @required
 57  
      * @readonly
 58  
      */
 59  
     private MavenProject project;
 60  
 
 61  
     /**
 62  
      * @parameter default-value="${project.artifact}"
 63  
      * @required
 64  
      * @readonly
 65  
      */
 66  
     private Artifact artifact;
 67  
 
 68  
     /**
 69  
      * @parameter default-value="${project.packaging}"
 70  
      * @required
 71  
      * @readonly
 72  
      */
 73  
     private String packaging;
 74  
 
 75  
     /**
 76  
      * @parameter default-value="${project.file}"
 77  
      * @required
 78  
      * @readonly
 79  
      */
 80  
     private File pomFile;
 81  
 
 82  
     /**
 83  
      * Specifies an alternative repository to which the project artifacts should be deployed ( other
 84  
      * than those specified in &lt;distributionManagement&gt; ).
 85  
      * <br/>
 86  
      * Format: id::layout::url
 87  
      * 
 88  
      * @parameter expression="${altDeploymentRepository}"
 89  
      */
 90  
     private String altDeploymentRepository;
 91  
     
 92  
     /**
 93  
      * @parameter default-value="${project.attachedArtifacts}
 94  
      * @required
 95  
      * @readonly
 96  
      */
 97  
     private List attachedArtifacts;
 98  
     
 99  
     /**
 100  
      * Set this to 'true' to bypass artifact deploy
 101  
      *       
 102  
      * @parameter expression="${maven.deploy.skip}" default-value="false"
 103  
      * @since 2.4
 104  
      */
 105  
     private boolean skip;     
 106  
 
 107  
     public void execute()
 108  
         throws MojoExecutionException, MojoFailureException
 109  
     {
 110  7
         if ( skip )
 111  
         {
 112  1
             getLog().info( "Skipping artifact deployment" );
 113  1
             return;
 114  
         }
 115  
 
 116  6
         failIfOffline();
 117  
 
 118  6
         ArtifactRepository repo = getDeploymentRepository();
 119  
 
 120  6
         String protocol = repo.getProtocol();
 121  
 
 122  6
         if ( protocol.equalsIgnoreCase( "scp" ) )
 123  
         {
 124  1
             File sshFile = new File( System.getProperty( "user.home" ), ".ssh" );
 125  
 
 126  1
             if ( !sshFile.exists() )
 127  
             {
 128  1
                 sshFile.mkdirs();
 129  
             }
 130  
         }
 131  
 
 132  
         // Deploy the POM
 133  6
         boolean isPomArtifact = "pom".equals( packaging );
 134  6
         if ( !isPomArtifact )
 135  
         {
 136  4
             ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
 137  4
             artifact.addMetadata( metadata );
 138  
         }
 139  
 
 140  6
         if ( updateReleaseInfo )
 141  
         {
 142  3
             artifact.setRelease( true );
 143  
         }
 144  
 
 145  
         try
 146  
         {
 147  6
             if ( isPomArtifact )
 148  
             {
 149  2
                 deploy( pomFile, artifact, repo, getLocalRepository() );
 150  
             }
 151  
             else
 152  
             {
 153  4
                 File file = artifact.getFile();
 154  
 
 155  4
                 if ( file != null && file.isFile() )
 156  
                 {
 157  3
                     deploy( file, artifact, repo, getLocalRepository() );
 158  
                 }
 159  1
                 else if ( !attachedArtifacts.isEmpty() )
 160  
                 {
 161  0
                     getLog().info( "No primary artifact to deploy, deploying attached artifacts instead." );
 162  
 
 163  0
                     Artifact pomArtifact =
 164  
                         artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
 165  
                                                                artifact.getBaseVersion() );
 166  0
                     pomArtifact.setFile( pomFile );
 167  0
                     if ( updateReleaseInfo )
 168  
                     {
 169  0
                         pomArtifact.setRelease( true );
 170  
                     }
 171  
 
 172  0
                     deploy( pomFile, pomArtifact, repo, getLocalRepository() );
 173  
 
 174  
                     // propagate the timestamped version to the main artifact for the attached artifacts to pick it up
 175  0
                     artifact.setResolvedVersion( pomArtifact.getVersion() );
 176  0
                 }
 177  
                 else
 178  
                 {
 179  1
                     String message = "The packaging for this project did not assign a file to the build artifact";
 180  1
                     throw new MojoExecutionException( message );
 181  
                 }
 182  
             }
 183  
 
 184  5
             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
 185  
             {
 186  1
                 Artifact attached = ( Artifact ) i.next();
 187  
 
 188  1
                 deploy( attached.getFile(), attached, repo, getLocalRepository() );
 189  1
             }
 190  
         }
 191  0
         catch ( ArtifactDeploymentException e )
 192  
         {
 193  0
             throw new MojoExecutionException( e.getMessage(), e );
 194  5
         }
 195  5
     }
 196  
 
 197  
     private ArtifactRepository getDeploymentRepository()
 198  
         throws MojoExecutionException, MojoFailureException
 199  
     {
 200  6
         ArtifactRepository repo = null;
 201  
 
 202  6
         if ( altDeploymentRepository != null )
 203  
         {
 204  0
             getLog().info( "Using alternate deployment repository " + altDeploymentRepository );
 205  
 
 206  0
             Matcher matcher = ALT_REPO_SYNTAX_PATTERN.matcher( altDeploymentRepository );
 207  
 
 208  0
             if ( !matcher.matches() )
 209  
             {
 210  0
                 throw new MojoFailureException( altDeploymentRepository, "Invalid syntax for repository.",
 211  
                                                 "Invalid syntax for alternative repository. Use \"id::layout::url\"." );
 212  
             }
 213  
             else
 214  
             {
 215  0
                 String id = matcher.group( 1 ).trim();
 216  0
                 String layout = matcher.group( 2 ).trim();
 217  0
                 String url = matcher.group( 3 ).trim();
 218  
 
 219  0
                 ArtifactRepositoryLayout repoLayout = getLayout( layout );
 220  
 
 221  0
                 repo = repositoryFactory.createDeploymentArtifactRepository( id, url, repoLayout, true );
 222  
             }
 223  
         }
 224  
         
 225  6
         if ( repo == null )
 226  
         {
 227  6
             repo = project.getDistributionManagementArtifactRepository();
 228  
         }
 229  
 
 230  6
         if ( repo == null )
 231  
         {
 232  0
             String msg = "Deployment failed: repository element was not specified in the POM inside"
 233  
                 + " distributionManagement element or in -DaltDeploymentRepository=id::layout::url parameter";
 234  
 
 235  0
             throw new MojoExecutionException( msg );
 236  
         }
 237  
 
 238  6
         return repo;
 239  
     }
 240  
 
 241  
 }