Coverage Report - org.apache.maven.plugins.mavenone.MavenOneRepositoryInstallMojo
 
Classes in this File Line Coverage Branch Coverage Complexity
MavenOneRepositoryInstallMojo
0%
0/35
0%
0/8
16
 
 1  
 package org.apache.maven.plugins.mavenone;
 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.installer.ArtifactInstaller;
 25  
 import org.apache.maven.artifact.repository.ArtifactRepository;
 26  
 import org.apache.maven.artifact.repository.ArtifactRepositoryFactory;
 27  
 import org.apache.maven.artifact.repository.layout.ArtifactRepositoryLayout;
 28  
 import org.apache.maven.plugin.AbstractMojo;
 29  
 import org.apache.maven.plugin.MojoExecutionException;
 30  
 import org.codehaus.plexus.util.IOUtil;
 31  
 
 32  
 import java.io.File;
 33  
 import java.io.FileInputStream;
 34  
 import java.io.FileNotFoundException;
 35  
 import java.io.IOException;
 36  
 import java.util.Iterator;
 37  
 import java.util.List;
 38  
 import java.util.Properties;
 39  
 
 40  
 /**
 41  
  * Install the artifact in the Maven 1 local repository.
 42  
  *
 43  
  * @goal install-maven-one-repository
 44  
  * @phase install
 45  
  */
 46  0
 public class MavenOneRepositoryInstallMojo
 47  
     extends AbstractMojo
 48  
 {
 49  
     /**
 50  
      * @parameter expression="${project.packaging}"
 51  
      * @required
 52  
      * @readonly
 53  
      */
 54  
     protected String packaging;
 55  
 
 56  
     /**
 57  
      * @parameter expression="${project.file}"
 58  
      * @required
 59  
      * @readonly
 60  
      */
 61  
     private File pomFile;
 62  
 
 63  
     /**
 64  
      * @parameter expression="${project.artifact}"
 65  
      * @required
 66  
      * @readonly
 67  
      */
 68  
     private Artifact artifact;
 69  
 
 70  
     /**
 71  
      * @component
 72  
      * @todo Write Javadoc for this
 73  
      */
 74  
     protected ArtifactInstaller installer;
 75  
 
 76  
     /**
 77  
      * @component
 78  
      * @todo Write Javadoc for this
 79  
      */
 80  
     protected ArtifactRepositoryFactory factory;
 81  
 
 82  
     /**
 83  
      * The location of the local repository.
 84  
      *
 85  
      * @parameter expression="${mavenOneRepository}"
 86  
      */
 87  
     protected String mavenOneRepository;
 88  
 
 89  
     /**
 90  
      * Whether the local repository uses a legacy layout or not.
 91  
      *
 92  
      * @component roleHint="legacy"
 93  
      */
 94  
     private ArtifactRepositoryLayout legacyLayout;
 95  
 
 96  
     /**
 97  
      * @parameter expression="${project.attachedArtifacts}
 98  
      * @required
 99  
      * @readonly
 100  
      */
 101  
     private List attachedArtifacts;
 102  
 
 103  
     public void execute()
 104  
         throws MojoExecutionException
 105  
     {
 106  
         try
 107  
         {
 108  0
             if ( mavenOneRepository == null )
 109  
             {
 110  0
                 File f = new File( System.getProperty( "user.home" ), "build.properties" );
 111  0
                 if ( f.exists() )
 112  
                 {
 113  0
                     Properties p = new Properties();
 114  0
                     FileInputStream inStream = new FileInputStream( f );
 115  
                     try
 116  
                     {
 117  0
                         p.load( inStream );
 118  
                     }
 119  
                     finally
 120  
                     {
 121  0
                         IOUtil.close( inStream );
 122  0
                     }
 123  0
                     mavenOneRepository = p.getProperty( "maven.repo.local" );
 124  
                 }
 125  
 
 126  0
                 if ( mavenOneRepository == null )
 127  
                 {
 128  0
                     mavenOneRepository = System.getProperty( "user.home" ) + "/.maven/repository";
 129  
                 }
 130  
             }
 131  
 
 132  0
             File f = new File( mavenOneRepository );
 133  0
             if ( !f.exists() )
 134  
             {
 135  0
                 f.mkdirs();
 136  
             }
 137  
 
 138  0
             ArtifactRepository localRepository = factory.createDeploymentArtifactRepository( "mavenOneRepository",
 139  
                                                                                              f.toURL().toString(),
 140  
                                                                                              legacyLayout, false );
 141  
 
 142  0
             boolean isPomArtifact = "pom".equals( packaging );
 143  
 
 144  0
             if ( isPomArtifact )
 145  
             {
 146  0
                 installer.install( pomFile, artifact, localRepository );
 147  
             }
 148  
             else
 149  
             {
 150  0
                 File file = artifact.getFile();
 151  0
                 if ( file == null )
 152  
                 {
 153  0
                     throw new MojoExecutionException(
 154  
                         "The packaging for this project did not assign a file to the build artifact" );
 155  
                 }
 156  0
                 installer.install( file, artifact, localRepository );
 157  
             }
 158  
 
 159  0
             if ( attachedArtifacts != null && !attachedArtifacts.isEmpty() )
 160  
             {
 161  0
                 for ( Iterator i = attachedArtifacts.iterator(); i.hasNext(); )
 162  
                 {
 163  0
                     Artifact attached = (Artifact) i.next();
 164  
 
 165  0
                     installer.install( attached.getFile(), attached, localRepository );
 166  
                 }
 167  
             }            
 168  
 
 169  
         }
 170  0
         catch ( ArtifactInstallationException e )
 171  
         {
 172  0
             throw new MojoExecutionException( e.getMessage(), e );
 173  
         }
 174  0
         catch ( FileNotFoundException e )
 175  
         {
 176  0
             throw new MojoExecutionException( e.getMessage(), e );
 177  
         }
 178  0
         catch ( IOException e )
 179  
         {
 180  0
             throw new MojoExecutionException( e.getMessage(), e );
 181  0
         }
 182  0
     }
 183  
 }