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 java.util.Iterator;
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
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 828793 2009-10-22 17:34:20Z bentmann $
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<ArtifactRepository> 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,
59                                                     String groupId,
60                                                     String artifactId,
61                                                     String version,
62                                                     String type,
63                                                     String classifier,
64                                                     List<ArtifactRepository> remoteRepositories,
65                                                     List<String> path )
66      {
67          this( message, groupId, artifactId, version, type, classifier, remoteRepositories, path, null );
68      }
69  
70      protected AbstractArtifactResolutionException( String message,
71                                                     String groupId,
72                                                     String artifactId,
73                                                     String version,
74                                                     String type,
75                                                     String classifier,
76                                                     List<ArtifactRepository> remoteRepositories,
77                                                     List<String> path,
78                                                     Throwable t )
79      {
80          super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ), t );
81  
82          this.originalMessage = message;
83          this.groupId = groupId;
84          this.artifactId = artifactId;
85          this.type = type;
86          this.classifier = classifier;
87          this.version = version;
88          this.remoteRepositories = remoteRepositories;
89          this.path = constructArtifactPath( path, "" );
90      }
91  
92      protected AbstractArtifactResolutionException( String message,
93                                                     Artifact artifact )
94      {
95          this( message, artifact, null );
96      }
97  
98      protected AbstractArtifactResolutionException( String message,
99                                                     Artifact artifact,
100                                                    List<ArtifactRepository> remoteRepositories )
101     {
102         this( message, artifact, remoteRepositories, null );
103     }
104 
105     protected AbstractArtifactResolutionException( String message,
106                                                    Artifact artifact,
107                                                    List<ArtifactRepository> remoteRepositories,
108                                                    Throwable t )
109     {
110         this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
111             artifact.getClassifier(), remoteRepositories, artifact.getDependencyTrail(), t );
112         this.artifact = artifact;
113     }
114 
115     public Artifact getArtifact()
116     {
117         return artifact;
118     }
119 
120     public String getGroupId()
121     {
122         return groupId;
123     }
124 
125     public String getArtifactId()
126     {
127         return artifactId;
128     }
129 
130     public String getVersion()
131     {
132         return version;
133     }
134 
135     public String getType()
136     {
137         return type;
138     }
139 
140     /** @return the classifier */
141     public String getClassifier()
142     {
143         return this.classifier;
144     }
145 
146     /** @return the path */
147     public String getPath()
148     {
149         return this.path;
150     }
151 
152     public List<ArtifactRepository> getRemoteRepositories()
153     {
154         return remoteRepositories;
155     }
156 
157     public String getOriginalMessage()
158     {
159         return originalMessage;
160     }
161 
162     protected static String constructArtifactPath( List<String> path,
163                                                    String indentation )
164     {
165         StringBuilder sb = new StringBuilder();
166 
167         if ( path != null )
168         {
169             sb.append( LS );
170             sb.append( indentation );
171             sb.append( "Path to dependency: " );
172             sb.append( LS );
173             int num = 1;
174             for ( Iterator<String> i = path.iterator(); i.hasNext(); num++ )
175             {
176                 sb.append( indentation );
177                 sb.append( "\t" );
178                 sb.append( num );
179                 sb.append( ") " );
180                 sb.append( i.next() );
181                 sb.append( LS );
182             }
183         }
184 
185         return sb.toString();
186     }
187 
188     private static String constructMessageBase( String message,
189                                                 String groupId,
190                                                 String artifactId,
191                                                 String version,
192                                                 String type,
193                                                 List<ArtifactRepository> remoteRepositories,
194                                                 List<String> path )
195     {
196         StringBuilder sb = new StringBuilder();
197 
198         sb.append( message );
199 
200         if ( message == null || !message.contains( "from the specified remote repositories:" ) )
201         {
202             sb.append( LS );
203             sb.append( "  " + groupId + ":" + artifactId + ":" + type + ":" + version );
204             sb.append( LS );
205             if ( remoteRepositories != null )
206             {
207                 sb.append( LS );
208                 sb.append( "from the specified remote repositories:" );
209                 sb.append( LS + "  " );
210 
211                 if ( remoteRepositories.isEmpty() )
212                 {
213                     sb.append( "(none)" );
214                 }
215 
216                 for ( Iterator<ArtifactRepository> i = remoteRepositories.iterator(); i.hasNext(); )
217                 {
218                     ArtifactRepository remoteRepository = i.next();
219 
220                     sb.append( remoteRepository.getId() );
221                     sb.append( " (" );
222                     sb.append( remoteRepository.getUrl() );
223 
224                     ArtifactRepositoryPolicy releases = remoteRepository.getReleases();
225                     if ( releases != null )
226                     {
227                         sb.append( ", releases=" ).append( releases.isEnabled() );
228                     }
229 
230                     ArtifactRepositoryPolicy snapshots = remoteRepository.getSnapshots();
231                     if ( snapshots != null )
232                     {
233                         sb.append( ", snapshots=" ).append( snapshots.isEnabled() );
234                     }
235 
236                     sb.append( ")" );
237                     if ( i.hasNext() )
238                     {
239                         sb.append( "," ).append( LS ).append( "  " );
240                     }
241                 }
242             }
243 
244             sb.append( constructArtifactPath( path, "" ) );
245             sb.append( LS );
246         }
247 
248         return sb.toString();
249     }
250 
251     protected static String constructMissingArtifactMessage( String message,
252                                                              String indentation,
253                                                              String groupId,
254                                                              String artifactId,
255                                                              String version,
256                                                              String type,
257                                                              String classifier,
258                                                              String downloadUrl,
259                                                              List<String> path )
260     {
261         StringBuilder sb = new StringBuilder( message );
262 
263         if ( !"pom".equals( type ) )
264         {
265             if ( downloadUrl != null )
266             {
267                 sb.append( LS );
268                 sb.append( LS );
269                 sb.append( indentation );
270                 sb.append( "Try downloading the file manually from: " );
271                 sb.append( LS );
272                 sb.append( indentation );
273                 sb.append( "    " );
274                 sb.append( downloadUrl );
275             }
276             else
277             {
278                 sb.append( LS );
279                 sb.append( LS );
280                 sb.append( indentation );
281                 sb.append( "Try downloading the file manually from the project website." );
282             }
283 
284             sb.append( LS );
285             sb.append( LS );
286             sb.append( indentation );
287             sb.append( "Then, install it using the command: " );
288             sb.append( LS );
289             sb.append( indentation );
290             sb.append( "    mvn install:install-file -DgroupId=" );
291             sb.append( groupId );
292             sb.append( " -DartifactId=" );
293             sb.append( artifactId );
294             sb.append( " -Dversion=" );
295             sb.append( version );
296 
297             //insert classifier only if it was used in the artifact
298             if ( classifier != null && !classifier.equals( "" ) )
299             {
300                 sb.append( " -Dclassifier=" );
301                 sb.append( classifier );
302             }
303             sb.append( " -Dpackaging=" );
304             sb.append( type );
305             sb.append( " -Dfile=/path/to/file" );
306             sb.append( LS );
307 
308             // If people want to deploy it
309             sb.append( LS );
310             sb.append( indentation );
311             sb.append( "Alternatively, if you host your own repository you can deploy the file there: " );
312             sb.append( LS );
313             sb.append( indentation );
314             sb.append( "    mvn deploy:deploy-file -DgroupId=" );
315             sb.append( groupId );
316             sb.append( " -DartifactId=" );
317             sb.append( artifactId );
318             sb.append( " -Dversion=" );
319             sb.append( version );
320 
321             //insert classifier only if it was used in the artifact
322             if ( classifier != null && !classifier.equals( "" ) )
323             {
324                 sb.append( " -Dclassifier=" );
325                 sb.append( classifier );
326             }
327             sb.append( " -Dpackaging=" );
328             sb.append( type );
329             sb.append( " -Dfile=/path/to/file" );
330             sb.append( " -Durl=[url] -DrepositoryId=[id]" );
331             sb.append( LS );
332         }
333 
334         sb.append( constructArtifactPath( path, indentation ) );
335         sb.append( LS );
336 
337         return sb.toString();
338     }
339 
340     public String getArtifactPath()
341     {
342         return path;
343     }
344 }