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