View Javadoc

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      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          this( message, groupId, artifactId, version, type, classifier, remoteRepositories, path, null );
62      }
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          super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ), t );
68  
69          this.originalMessage = message;
70          this.groupId = groupId;
71          this.artifactId = artifactId;
72          this.type = type;
73          this.classifier = classifier;
74          this.version = version;
75          this.remoteRepositories = remoteRepositories;
76          this.path = constructArtifactPath( path, "" );
77      }
78  
79      protected AbstractArtifactResolutionException( String message, Artifact artifact )
80      {
81          this( message, artifact, null );
82      }
83  
84      protected AbstractArtifactResolutionException( String message, Artifact artifact, List remoteRepositories )
85      {
86          this( message, artifact, remoteRepositories, null );
87      }
88  
89      protected AbstractArtifactResolutionException( String message, Artifact artifact, List remoteRepositories,
90                                                     Throwable t )
91      {
92          this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
93                artifact.getClassifier(),remoteRepositories, artifact.getDependencyTrail(), t );
94          this.artifact = artifact;
95      }
96  
97      public Artifact getArtifact()
98      {
99          return artifact;
100     }
101 
102     public String getGroupId()
103     {
104         return groupId;
105     }
106 
107     public String getArtifactId()
108     {
109         return artifactId;
110     }
111 
112     public String getVersion()
113     {
114         return version;
115     }
116 
117     public String getType()
118     {
119         return type;
120     }
121 
122     /**
123      * @return the classifier
124      */
125     public String getClassifier()
126     {
127         return this.classifier;
128     }
129 
130     /**
131      * @return the path
132      */
133     public String getPath()
134     {
135         return this.path;
136     }
137 
138     public List getRemoteRepositories()
139     {
140         return remoteRepositories;
141     }
142 
143     public String getOriginalMessage()
144     {
145         return originalMessage;
146     }
147 
148     protected static String constructArtifactPath( List path, String indentation )
149     {
150         StringBuffer sb = new StringBuffer();
151 
152         if ( path != null )
153         {
154             sb.append( LS );
155             sb.append( indentation );
156             sb.append( "Path to dependency: " );
157             sb.append( LS );
158             int num = 1;
159             for ( Iterator i = path.iterator(); i.hasNext(); num++ )
160             {
161                 sb.append( indentation );
162                 sb.append( "\t" );
163                 sb.append( num );
164                 sb.append( ") " );
165                 sb.append( i.next() );
166                 sb.append( LS );
167             }
168         }
169 
170         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         StringBuffer sb = new StringBuffer();
177 
178         sb.append( message );
179         sb.append( LS );
180         sb.append( "  " + groupId + ":" + artifactId + ":" + type + ":" + version );
181         sb.append( LS );
182         if ( remoteRepositories != null && !remoteRepositories.isEmpty() )
183         {
184             sb.append( LS );
185             sb.append( "from the specified remote repositories:" );
186             sb.append( LS + "  " );
187 
188             for ( Iterator i = new HashSet( remoteRepositories ).iterator(); i.hasNext(); )
189             {
190                 ArtifactRepository remoteRepository = (ArtifactRepository) i.next();
191 
192                 sb.append( remoteRepository.getId() );
193                 sb.append( " (" );
194                 sb.append( remoteRepository.getUrl() );
195                 sb.append( ")" );
196                 if ( i.hasNext() )
197                 {
198                     sb.append( ",\n  " );
199                 }
200             }
201         }
202 
203         sb.append( LS );
204         sb.append( constructArtifactPath( path, "" ) );
205         sb.append( LS );
206         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         StringBuffer sb = new StringBuffer( message );
213 
214         if ( !"pom".equals( type ) )
215         {
216             if ( downloadUrl != null )
217             {
218                 sb.append( LS );
219                 sb.append( LS );
220                 sb.append( indentation );
221                 sb.append( "Try downloading the file manually from: " );
222                 sb.append( LS );
223                 sb.append( indentation );
224                 sb.append( "    " );
225                 sb.append( downloadUrl );
226             }
227             else
228             {
229                 sb.append( LS );
230                 sb.append( LS );
231                 sb.append( indentation );
232                 sb.append( "Try downloading the file manually from the project website." );
233             }
234 
235             sb.append( LS );
236             sb.append( LS );
237             sb.append( indentation );
238             sb.append( "Then, install it using the command: " );
239             sb.append( LS );
240             sb.append( indentation );
241             sb.append( "    mvn install:install-file -DgroupId=" );
242             sb.append( groupId );
243             sb.append( " -DartifactId=" );
244             sb.append( artifactId );
245             sb.append( " -Dversion=" );
246             sb.append( version );
247 
248             //insert classifier only if it was used in the artifact
249             if (classifier !=null && !classifier.equals( "" ))
250             {
251                 sb.append( " -Dclassifier=" );
252                 sb.append( classifier );
253             }
254             sb.append( " -Dpackaging=" );
255             sb.append( type );
256             sb.append( " -Dfile=/path/to/file" );
257             sb.append( LS );
258 
259             // If people want to deploy it
260 
261             sb.append( LS );
262             sb.append( indentation );
263             sb.append( "Alternatively, if you host your own repository you can deploy the file there: " );
264             sb.append( LS );
265             sb.append( indentation );
266             sb.append( "    mvn deploy:deploy-file -DgroupId=" );
267             sb.append( groupId );
268             sb.append( " -DartifactId=" );
269             sb.append( artifactId );
270             sb.append( " -Dversion=" );
271             sb.append( version );
272 
273             //insert classifier only if it was used in the artifact
274             if (classifier !=null && !classifier.equals( "" ))
275             {
276                 sb.append( " -Dclassifier=" );
277                 sb.append( classifier );
278             }
279             sb.append( " -Dpackaging=" );
280             sb.append( type );
281             sb.append( " -Dfile=/path/to/file" );
282             sb.append( " -Durl=[url] -DrepositoryId=[id]" );
283             sb.append( LS );
284 
285         }
286 
287         sb.append( constructArtifactPath( path, indentation ) );
288         sb.append( LS );
289 
290         return sb.toString();
291     }
292 
293     public String getArtifactPath()
294     {
295         return path;
296     }
297 }