Coverage Report - org.apache.maven.artifact.resolver.AbstractArtifactResolutionException
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractArtifactResolutionException
76 %
93/123
46 %
11/24
1,5
 
 1  
 package org.apache.maven.artifact.resolver;
 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.repository.ArtifactRepository;
 24  
 
 25  
 import java.util.HashSet;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * Base class for artifact resolution exceptions.
 31  
  *
 32  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 33  
  * @version $Id: AbstractArtifactResolutionException.java 647357 2008-04-12 00:51:03Z vsiveton $
 34  
  */
 35  
 public class AbstractArtifactResolutionException
 36  
     extends Exception
 37  
 {
 38  
     private String groupId;
 39  
 
 40  
     private String artifactId;
 41  
 
 42  
     private String version;
 43  
 
 44  
     private String type;
 45  
 
 46  
     private String classifier;
 47  
 
 48  
     private Artifact artifact;
 49  
 
 50  
     private List remoteRepositories;
 51  
 
 52  
     private final String originalMessage;
 53  
 
 54  
     private final String path;
 55  
 
 56  1
     static final String LS = System.getProperty( "line.separator" );
 57  
 
 58  
     protected AbstractArtifactResolutionException( String message, String groupId, String artifactId, String version,
 59  
                                                    String type, String classifier, List remoteRepositories, List path )
 60  
     {
 61  0
         this( message, groupId, artifactId, version, type, classifier, remoteRepositories, path, null );
 62  0
     }
 63  
 
 64  
     protected AbstractArtifactResolutionException( String message, String groupId, String artifactId, String version,
 65  
                                                    String type, String classifier, List remoteRepositories, List path, Throwable t )
 66  
     {
 67  4
         super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ), t );
 68  
 
 69  4
         this.originalMessage = message;
 70  4
         this.groupId = groupId;
 71  4
         this.artifactId = artifactId;
 72  4
         this.type = type;
 73  4
         this.classifier = classifier;
 74  4
         this.version = version;
 75  4
         this.remoteRepositories = remoteRepositories;
 76  4
         this.path = constructArtifactPath( path, "" );
 77  4
     }
 78  
 
 79  
     protected AbstractArtifactResolutionException( String message, Artifact artifact )
 80  
     {
 81  0
         this( message, artifact, null );
 82  0
     }
 83  
 
 84  
     protected AbstractArtifactResolutionException( String message, Artifact artifact, List remoteRepositories )
 85  
     {
 86  4
         this( message, artifact, remoteRepositories, null );
 87  4
     }
 88  
 
 89  
     protected AbstractArtifactResolutionException( String message, Artifact artifact, List remoteRepositories,
 90  
                                                    Throwable t )
 91  
     {
 92  4
         this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
 93  
               artifact.getClassifier(),remoteRepositories, artifact.getDependencyTrail(), t );
 94  4
         this.artifact = artifact;
 95  4
     }
 96  
 
 97  
     public Artifact getArtifact()
 98  
     {
 99  0
         return artifact;
 100  
     }
 101  
 
 102  
     public String getGroupId()
 103  
     {
 104  0
         return groupId;
 105  
     }
 106  
 
 107  
     public String getArtifactId()
 108  
     {
 109  0
         return artifactId;
 110  
     }
 111  
 
 112  
     public String getVersion()
 113  
     {
 114  0
         return version;
 115  
     }
 116  
 
 117  
     public String getType()
 118  
     {
 119  0
         return type;
 120  
     }
 121  
 
 122  
     /**
 123  
      * @return the classifier
 124  
      */
 125  
     public String getClassifier()
 126  
     {
 127  0
         return this.classifier;
 128  
     }
 129  
 
 130  
     /**
 131  
      * @return the path
 132  
      */
 133  
     public String getPath()
 134  
     {
 135  0
         return this.path;
 136  
     }
 137  
 
 138  
     public List getRemoteRepositories()
 139  
     {
 140  0
         return remoteRepositories;
 141  
     }
 142  
 
 143  
     public String getOriginalMessage()
 144  
     {
 145  0
         return originalMessage;
 146  
     }
 147  
 
 148  
     protected static String constructArtifactPath( List path, String indentation )
 149  
     {
 150  9
         StringBuffer sb = new StringBuffer();
 151  
 
 152  9
         if ( path != null )
 153  
         {
 154  9
             sb.append( LS );
 155  9
             sb.append( indentation );
 156  9
             sb.append( "Path to dependency: " );
 157  9
             sb.append( LS );
 158  9
             int num = 1;
 159  27
             for ( Iterator i = path.iterator(); i.hasNext(); num++ )
 160  
             {
 161  18
                 sb.append( indentation );
 162  18
                 sb.append( "\t" );
 163  18
                 sb.append( num );
 164  18
                 sb.append( ") " );
 165  18
                 sb.append( i.next() );
 166  18
                 sb.append( LS );
 167  
             }
 168  
         }
 169  
 
 170  9
         return sb.toString();
 171  
     }
 172  
 
 173  
     private static String constructMessageBase( String message, String groupId, String artifactId, String version,
 174  
                                                 String type, List remoteRepositories, List path )
 175  
     {
 176  4
         StringBuffer sb = new StringBuffer();
 177  
 
 178  4
         sb.append( message );
 179  4
         sb.append( LS );
 180  4
         sb.append( "  " + groupId + ":" + artifactId + ":" + type + ":" + version );
 181  4
         sb.append( LS );
 182  4
         if ( remoteRepositories != null && !remoteRepositories.isEmpty() )
 183  
         {
 184  0
             sb.append( LS );
 185  0
             sb.append( "from the specified remote repositories:" );
 186  0
             sb.append( LS + "  " );
 187  
 
 188  0
             for ( Iterator i = new HashSet( remoteRepositories ).iterator(); i.hasNext(); )
 189  
             {
 190  0
                 ArtifactRepository remoteRepository = (ArtifactRepository) i.next();
 191  
 
 192  0
                 sb.append( remoteRepository.getId() );
 193  0
                 sb.append( " (" );
 194  0
                 sb.append( remoteRepository.getUrl() );
 195  0
                 sb.append( ")" );
 196  0
                 if ( i.hasNext() )
 197  
                 {
 198  0
                     sb.append( ",\n  " );
 199  
                 }
 200  0
             }
 201  
         }
 202  
 
 203  4
         sb.append( LS );
 204  4
         sb.append( constructArtifactPath( path, "" ) );
 205  4
         sb.append( LS );
 206  4
         return sb.toString();
 207  
     }
 208  
 
 209  
     protected static String constructMissingArtifactMessage( String message, String indentation, String groupId, String artifactId, String version,
 210  
                                               String type, String classifier, String downloadUrl, List path )
 211  
     {
 212  1
         StringBuffer sb = new StringBuffer( message );
 213  
 
 214  1
         if ( !"pom".equals( type ) )
 215  
         {
 216  1
             if ( downloadUrl != null )
 217  
             {
 218  1
                 sb.append( LS );
 219  1
                 sb.append( LS );
 220  1
                 sb.append( indentation );
 221  1
                 sb.append( "Try downloading the file manually from: " );
 222  1
                 sb.append( LS );
 223  1
                 sb.append( indentation );
 224  1
                 sb.append( "    " );
 225  1
                 sb.append( downloadUrl );
 226  
             }
 227  
             else
 228  
             {
 229  0
                 sb.append( LS );
 230  0
                 sb.append( LS );
 231  0
                 sb.append( indentation );
 232  0
                 sb.append( "Try downloading the file manually from the project website." );
 233  
             }
 234  
 
 235  1
             sb.append( LS );
 236  1
             sb.append( LS );
 237  1
             sb.append( indentation );
 238  1
             sb.append( "Then, install it using the command: " );
 239  1
             sb.append( LS );
 240  1
             sb.append( indentation );
 241  1
             sb.append( "    mvn install:install-file -DgroupId=" );
 242  1
             sb.append( groupId );
 243  1
             sb.append( " -DartifactId=" );
 244  1
             sb.append( artifactId );
 245  1
             sb.append( " -Dversion=" );
 246  1
             sb.append( version );
 247  
 
 248  
             //insert classifier only if it was used in the artifact
 249  1
             if (classifier !=null && !classifier.equals( "" ))
 250  
             {
 251  1
                 sb.append( " -Dclassifier=" );
 252  1
                 sb.append( classifier );
 253  
             }
 254  1
             sb.append( " -Dpackaging=" );
 255  1
             sb.append( type );
 256  1
             sb.append( " -Dfile=/path/to/file" );
 257  1
             sb.append( LS );
 258  
 
 259  
             // If people want to deploy it
 260  
 
 261  1
             sb.append( LS );
 262  1
             sb.append( indentation );
 263  1
             sb.append( "Alternatively, if you host your own repository you can deploy the file there: " );
 264  1
             sb.append( LS );
 265  1
             sb.append( indentation );
 266  1
             sb.append( "    mvn deploy:deploy-file -DgroupId=" );
 267  1
             sb.append( groupId );
 268  1
             sb.append( " -DartifactId=" );
 269  1
             sb.append( artifactId );
 270  1
             sb.append( " -Dversion=" );
 271  1
             sb.append( version );
 272  
 
 273  
             //insert classifier only if it was used in the artifact
 274  1
             if (classifier !=null && !classifier.equals( "" ))
 275  
             {
 276  1
                 sb.append( " -Dclassifier=" );
 277  1
                 sb.append( classifier );
 278  
             }
 279  1
             sb.append( " -Dpackaging=" );
 280  1
             sb.append( type );
 281  1
             sb.append( " -Dfile=/path/to/file" );
 282  1
             sb.append( " -Durl=[url] -DrepositoryId=[id]" );
 283  1
             sb.append( LS );
 284  
 
 285  
         }
 286  
 
 287  1
         sb.append( constructArtifactPath( path, indentation ) );
 288  1
         sb.append( LS );
 289  
 
 290  1
         return sb.toString();
 291  
     }
 292  
 
 293  
     public String getArtifactPath()
 294  
     {
 295  0
         return path;
 296  
     }
 297  
 }