Coverage Report - org.apache.maven.plugin.install.InstallMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
InstallMojo
92%
37/40
87%
14/16
7
 
 1  
 package org.apache.maven.plugin.install;
 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.installer.ArtifactInstallationException;
 24  
 import org.apache.maven.artifact.metadata.ArtifactMetadata;
 25  
 import org.apache.maven.plugin.MojoExecutionException;
 26  
 import org.apache.maven.plugins.annotations.LifecyclePhase;
 27  
 import org.apache.maven.plugins.annotations.Mojo;
 28  
 import org.apache.maven.plugins.annotations.Parameter;
 29  
 import org.apache.maven.project.artifact.ProjectArtifactMetadata;
 30  
 
 31  
 import java.io.File;
 32  
 import java.util.Collection;
 33  
 import java.util.Iterator;
 34  
 import java.util.LinkedHashSet;
 35  
 import java.util.List;
 36  
 
 37  
 /**
 38  
  * Installs the project's main artifact, and any other artifacts attached by other plugins in the lifecycle, to the local repository.
 39  
  *
 40  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 41  
  * @version $Id$
 42  
  */
 43  
 @Mojo( name = "install", defaultPhase = LifecyclePhase.INSTALL, threadSafe = true )
 44  8
 public class InstallMojo
 45  
     extends AbstractInstallMojo
 46  
 {
 47  
     /**
 48  
      */
 49  
     @Parameter( defaultValue = "${project.packaging}", required = true, readonly = true )
 50  
     protected String packaging;
 51  
 
 52  
     /**
 53  
      */
 54  
     @Parameter( defaultValue = "${project.file}", required = true, readonly = true )
 55  
     private File pomFile;
 56  
 
 57  
     /**
 58  
      * Set this to <code>true</code> to bypass artifact installation.
 59  
      * Use this for artifacts that does not need to be installed in the local repository.
 60  
      *
 61  
      * @since 2.4
 62  
      */
 63  
     @Parameter( property = "maven.install.skip", defaultValue = "false", required = true )
 64  
     private boolean skip;
 65  
 
 66  
     /**
 67  
      */
 68  
     @Parameter( defaultValue = "${project.artifact}", required = true, readonly = true )
 69  
     private Artifact artifact;
 70  
 
 71  
     /**
 72  
      */
 73  
     @Parameter( defaultValue = "${project.attachedArtifacts}", required = true, readonly = true )
 74  
     private List attachedArtifacts;
 75  
 
 76  
     public void execute()
 77  
         throws MojoExecutionException
 78  
     {
 79  7
         if ( skip )
 80  
         {
 81  1
             getLog().info( "Skipping artifact installation" );
 82  1
             return;
 83  
         }
 84  
 
 85  
         // TODO: push into transformation
 86  6
         boolean isPomArtifact = "pom".equals( packaging );
 87  
 
 88  6
         ArtifactMetadata metadata = null;
 89  
 
 90  6
         if ( updateReleaseInfo )
 91  
         {
 92  1
             artifact.setRelease( true );
 93  
         }
 94  
 
 95  
         try
 96  
         {
 97  6
             Collection metadataFiles = new LinkedHashSet();
 98  
 
 99  6
             if ( isPomArtifact )
 100  
             {
 101  1
                 installer.install( pomFile, artifact, localRepository );
 102  1
                 installChecksums( artifact, metadataFiles );
 103  
             }
 104  
             else
 105  
             {
 106  5
                 metadata = new ProjectArtifactMetadata( artifact, pomFile );
 107  5
                 artifact.addMetadata( metadata );
 108  
 
 109  5
                 File file = artifact.getFile();
 110  
 
 111  
                 // Here, we have a temporary solution to MINSTALL-3 (isDirectory() is true if it went through compile
 112  
                 // but not package). We are designing in a proper solution for Maven 2.1
 113  5
                 if ( file != null && file.isFile() )
 114  
                 {
 115  3
                     installer.install( file, artifact, localRepository );
 116  3
                     installChecksums( artifact, metadataFiles );
 117  
                 }
 118  2
                 else if ( !attachedArtifacts.isEmpty() )
 119  
                 {
 120  1
                     getLog().info( "No primary artifact to install, installing attached artifacts instead." );
 121  
 
 122  1
                     Artifact pomArtifact =
 123  
                         artifactFactory.createProjectArtifact( artifact.getGroupId(), artifact.getArtifactId(),
 124  
                                                                artifact.getBaseVersion() );
 125  1
                     pomArtifact.setFile( pomFile );
 126  1
                     if ( updateReleaseInfo )
 127  
                     {
 128  0
                         pomArtifact.setRelease( true );
 129  
                     }
 130  
 
 131  1
                     installer.install( pomFile, pomArtifact, localRepository );
 132  1
                     installChecksums( pomArtifact, metadataFiles );
 133  1
                 }
 134  
                 else
 135  
                 {
 136  1
                     throw new MojoExecutionException(
 137  
                         "The packaging for this project did not assign a file to the build artifact" );
 138  
                 }
 139  
             }
 140  
 
 141  5
             for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
 142  
             {
 143  2
                 Artifact attached = (Artifact) i.next();
 144  
 
 145  2
                 installer.install( attached.getFile(), attached, localRepository );
 146  2
                 installChecksums( attached, metadataFiles );
 147  2
             }
 148  
 
 149  5
             installChecksums( metadataFiles );
 150  
         }
 151  0
         catch ( ArtifactInstallationException e )
 152  
         {
 153  0
             throw new MojoExecutionException( e.getMessage(), e );
 154  5
         }
 155  5
     }
 156  
 
 157  
     public void setSkip( boolean skip )
 158  
     {
 159  1
         this.skip = skip;
 160  1
     }
 161  
 }