Coverage Report - org.apache.maven.plugin.deploy.DeployFileMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
DeployFileMojo
84%
76/90
81%
34/42
2.706
 
 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.factory.ArtifactFactory;
 25  
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
 26  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 27  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 28  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 29  
 import org.apache.maven.model.Model;
 30  
 import org.apache.maven.model.Parent;
 31  
 import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
 32  
 import org.apache.maven.model.io.xpp3.MavenXpp3Writer;
 33  
 import org.apache.maven.plugin.MojoExecutionException;
 34  
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 35  
 import org.codehaus.plexus.util.IOUtil;
 36  
 import org.codehaus.plexus.util.ReaderFactory;
 37  
 import org.codehaus.plexus.util.WriterFactory;
 38  
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 39  
 
 40  
 import java.io.File;
 41  
 import java.io.FileNotFoundException;
 42  
 import java.io.IOException;
 43  
 import java.io.Reader;
 44  
 import java.io.Writer;
 45  
 import java.util.Map;
 46  
 
 47  
 /**
 48  
  * Installs the artifact in the remote repository.
 49  
  *
 50  
  * @author <a href="mailto:aramirez@apache.org">Allan Ramirez</a>
 51  
  * @goal deploy-file
 52  
  * @requiresProject false
 53  
  */
 54  13
 public class DeployFileMojo
 55  
     extends AbstractDeployMojo
 56  
 {
 57  
     /**
 58  
      * GroupId of the artifact to be deployed.  Retrieved from POM file if specified.
 59  
      *
 60  
      * @parameter expression="${groupId}"
 61  
      */
 62  
     private String groupId;
 63  
 
 64  
     /**
 65  
      * ArtifactId of the artifact to be deployed.  Retrieved from POM file if specified.
 66  
      *
 67  
      * @parameter expression="${artifactId}"
 68  
      */
 69  
     private String artifactId;
 70  
 
 71  
     /**
 72  
      * Version of the artifact to be deployed.  Retrieved from POM file if specified.
 73  
      *
 74  
      * @parameter expression="${version}"
 75  
      */
 76  
     private String version;
 77  
 
 78  
     /**
 79  
      * Type of the artifact to be deployed.  Retrieved from POM file if specified.
 80  
      *
 81  
      * @parameter expression="${packaging}"
 82  
      */
 83  
     private String packaging;
 84  
 
 85  
     /**
 86  
      * Description passed to a generated POM file (in case of generatePom=true)
 87  
      *
 88  
      * @parameter expression="${generatePom.description}"
 89  
      */
 90  
     private String description;
 91  
 
 92  
     /**
 93  
      * File to be deployed.
 94  
      *
 95  
      * @parameter expression="${file}"
 96  
      * @required
 97  
      */
 98  
     private File file;
 99  
 
 100  
     /**
 101  
      * Server Id to map on the &lt;id&gt; under &lt;server&gt; section of settings.xml
 102  
      * In most cases, this parameter will be required for authentication.
 103  
      *
 104  
      * @parameter expression="${repositoryId}" default-value="remote-repository"
 105  
      * @required
 106  
      */
 107  
     private String repositoryId;
 108  
 
 109  
     /**
 110  
      * The type of remote repository layout to deploy to. Try <i>legacy</i> for 
 111  
      * a Maven 1.x-style repository layout.
 112  
      * 
 113  
      * @parameter expression="${repositoryLayout}" default-value="default"
 114  
      * @required
 115  
      */
 116  
     private String repositoryLayout;
 117  
 
 118  
     /**
 119  
      * Map that contains the layouts
 120  
      *
 121  
      * @component role="org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout"
 122  
      */
 123  
     private Map repositoryLayouts;
 124  
 
 125  
     /**
 126  
      * URL where the artifact will be deployed. <br/>
 127  
      * ie ( file://C:\m2-repo or scp://host.com/path/to/repo )
 128  
      *
 129  
      * @parameter expression="${url}"
 130  
      * @required
 131  
      */
 132  
     private String url;
 133  
 
 134  
     /**
 135  
      * Component used to create an artifact
 136  
      *
 137  
      * @component
 138  
      */
 139  
     private ArtifactFactory artifactFactory;
 140  
 
 141  
     /**
 142  
      * Component used to create a repository
 143  
      *
 144  
      * @component
 145  
      */
 146  
     private ArtifactRepositoryFactory repositoryFactory;
 147  
 
 148  
     /**
 149  
      * Location of an existing POM file to be deployed alongside the main
 150  
      * artifact, given by the ${file} parameter.
 151  
      * 
 152  
      * @parameter expression="${pomFile}"
 153  
      */
 154  
     private File pomFile;
 155  
 
 156  
     /**
 157  
      * Upload a POM for this artifact.  Will generate a default POM if none is
 158  
      * supplied with the pomFile argument.
 159  
      *
 160  
      * @parameter expression="${generatePom}" default-value="true"
 161  
      */
 162  
     private boolean generatePom;
 163  
 
 164  
     /**
 165  
      * Add classifier to the artifact
 166  
      *
 167  
      * @parameter expression="${classifier}";
 168  
      */
 169  
     private String classifier;
 170  
 
 171  
     /**
 172  
      * Whether to deploy snapshots with a unique version or not.
 173  
      *
 174  
      * @parameter expression="${uniqueVersion}" default-value="true"
 175  
      */
 176  
     private boolean uniqueVersion;
 177  
 
 178  
     protected void initProperties()
 179  
         throws MojoExecutionException
 180  
     {
 181  
         // Process the supplied POM (if there is one)
 182  13
         if ( pomFile != null )
 183  
         {
 184  8
             generatePom = false;
 185  
 
 186  8
             Model model = readModel( pomFile );
 187  
 
 188  8
             processModel( model );
 189  
         }
 190  
 
 191  
         // Verify arguments
 192  13
         if ( groupId == null || artifactId == null || version == null || packaging == null )
 193  
         {
 194  3
             throw new MojoExecutionException( "Missing group, artifact, version, or packaging information" );
 195  
         }
 196  10
     }
 197  
 
 198  
     public void execute()
 199  
         throws MojoExecutionException
 200  
     {
 201  6
         initProperties();
 202  
 
 203  6
         if ( !file.exists() )
 204  
         {
 205  0
             throw new MojoExecutionException( file.getPath() + " not found." );
 206  
         }
 207  
 
 208  
         ArtifactRepositoryLayout layout;
 209  
 
 210  6
         layout = ( ArtifactRepositoryLayout ) repositoryLayouts.get( repositoryLayout );
 211  
 
 212  6
         ArtifactRepository deploymentRepository =
 213  
             repositoryFactory.createDeploymentArtifactRepository( repositoryId, url, layout, uniqueVersion );
 214  
 
 215  6
         String protocol = deploymentRepository.getProtocol();
 216  
 
 217  6
         if ( "".equals( protocol ) || protocol == null )
 218  
         {
 219  0
             throw new MojoExecutionException( "No transfer protocol found." );
 220  
         }
 221  
 
 222  
         // Create the artifact
 223  6
         Artifact artifact =
 224  
             artifactFactory.createArtifactWithClassifier( groupId, artifactId, version, packaging, classifier );
 225  
 
 226  
         // Upload the POM if requested, generating one if need be
 227  6
         if ( generatePom )
 228  
         {
 229  5
             ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, generatePomFile() );
 230  5
             artifact.addMetadata( metadata );
 231  5
         }
 232  
         else
 233  
         {
 234  1
             ArtifactMetadata metadata = new ProjectArtifactMetadata( artifact, pomFile );
 235  1
             artifact.addMetadata( metadata );
 236  
         }
 237  
 
 238  
         try
 239  
         {
 240  6
             getDeployer().deploy( file, artifact, deploymentRepository, getLocalRepository() );
 241  
         }
 242  0
         catch ( ArtifactDeploymentException e )
 243  
         {
 244  0
             throw new MojoExecutionException( e.getMessage(), e );
 245  6
         }
 246  6
     }
 247  
 
 248  
     /**
 249  
      * Process the supplied pomFile to get groupId, artifactId, version, and packaging
 250  
      *
 251  
      * @throws NullPointerException if model is <code>null</code>
 252  
      */
 253  
     private void processModel( Model model )
 254  
     {
 255  8
         Parent parent = model.getParent();
 256  
 
 257  8
         if ( this.groupId == null )
 258  
         {
 259  6
             if ( parent != null && parent.getGroupId() != null )
 260  
             {
 261  5
                 this.groupId = parent.getGroupId();
 262  
             }
 263  6
             if ( model.getGroupId() != null )
 264  
             {
 265  2
                 this.groupId = model.getGroupId();
 266  
             }
 267  
         }
 268  8
         if ( this.artifactId == null && model.getArtifactId() != null )
 269  
         {
 270  5
             this.artifactId = model.getArtifactId();
 271  
         }
 272  8
         if ( this.version == null )
 273  
         {
 274  6
             this.version = model.getVersion();
 275  6
             if ( this.version == null && parent != null )
 276  
             {
 277  2
                 this.version = parent.getVersion();
 278  
             }
 279  
         }
 280  8
         if ( this.packaging == null && model.getPackaging() != null )
 281  
         {
 282  3
             this.packaging = model.getPackaging();
 283  
         }
 284  8
     }
 285  
 
 286  
     /**
 287  
      * Extract the Model from the specified file.
 288  
      *
 289  
      * @param pomFile
 290  
      * @return
 291  
      * @throws MojoExecutionException if the file doesn't exist of cannot be read.
 292  
      */
 293  
     protected Model readModel( File pomFile )
 294  
         throws MojoExecutionException
 295  
     {
 296  
 
 297  2
         if ( !pomFile.exists() )
 298  
         {
 299  0
             throw new MojoExecutionException( "Specified pomFile does not exist" );
 300  
         }
 301  
 
 302  2
         Reader reader = null;
 303  
         try
 304  
         {
 305  2
             reader = ReaderFactory.newXmlReader( pomFile );
 306  2
             MavenXpp3Reader modelReader = new MavenXpp3Reader();
 307  2
             return modelReader.read( reader );
 308  
         }
 309  0
         catch ( FileNotFoundException e )
 310  
         {
 311  0
             throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
 312  
         }
 313  0
         catch ( IOException e )
 314  
         {
 315  0
             throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
 316  
         }
 317  0
         catch ( XmlPullParserException e )
 318  
         {
 319  0
             throw new MojoExecutionException( "Error reading specified POM file: " + e.getMessage(), e );
 320  
         }
 321  
         finally
 322  
         {
 323  2
             IOUtil.close( reader );
 324  
         }
 325  
     }
 326  
 
 327  
     private File generatePomFile()
 328  
         throws MojoExecutionException
 329  
     {
 330  5
         Writer fw = null;
 331  
         try
 332  
         {
 333  5
             File tempFile = File.createTempFile( "mvninstall", ".pom" );
 334  5
             tempFile.deleteOnExit();
 335  
 
 336  5
             Model model = new Model();
 337  5
             model.setModelVersion( "4.0.0" );
 338  5
             model.setGroupId( groupId );
 339  5
             model.setArtifactId( artifactId );
 340  5
             model.setVersion( version );
 341  5
             model.setPackaging( packaging );
 342  5
             model.setDescription( description );
 343  
 
 344  5
             fw = WriterFactory.newXmlWriter( tempFile );
 345  5
             new MavenXpp3Writer().write( fw, model );
 346  
 
 347  5
             return tempFile;
 348  
         }
 349  0
         catch ( IOException e )
 350  
         {
 351  0
             throw new MojoExecutionException( "Error writing temporary pom file: " + e.getMessage(), e );
 352  
         }
 353  
         finally
 354  
         {
 355  5
             IOUtil.close( fw );
 356  
         }
 357  
     }
 358  
 
 359  
     void setGroupId( String groupId )
 360  
     {
 361  1
         this.groupId = groupId;
 362  1
     }
 363  
 
 364  
     void setArtifactId( String artifactId )
 365  
     {
 366  1
         this.artifactId = artifactId;
 367  1
     }
 368  
 
 369  
     void setVersion( String version )
 370  
     {
 371  1
         this.version = version;
 372  1
     }
 373  
 
 374  
     void setPackaging( String packaging )
 375  
     {
 376  1
         this.packaging = packaging;
 377  1
     }
 378  
 
 379  
     void setPomFile( File pomFile )
 380  
     {
 381  7
         this.pomFile = pomFile;
 382  7
     }
 383  
 
 384  
     String getGroupId()
 385  
     {
 386  7
         return groupId;
 387  
     }
 388  
 
 389  
     String getArtifactId()
 390  
     {
 391  7
         return artifactId;
 392  
     }
 393  
 
 394  
     String getVersion()
 395  
     {
 396  7
         return version;
 397  
     }
 398  
 
 399  
     String getPackaging()
 400  
     {
 401  7
         return packaging;
 402  
     }
 403  
 
 404  
     File getFile()
 405  
     {
 406  0
         return file;
 407  
     }
 408  
 
 409  
     String getClassifier()
 410  
     {
 411  2
         return classifier;
 412  
     }
 413  
 
 414  
     void setClassifier( String classifier )
 415  
     {
 416  1
         this.classifier = classifier;
 417  1
     }
 418  
 }