aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/maven/apache-maven-3.3.3/maven-artifact/src/main/java/org/apache/maven/artifact/resolver/AbstractArtifactResolutionException.java
blob: 66e147c238d175d914c25703e4bbb3106495424c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package org.apache.maven.artifact.resolver;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.util.Iterator;
import java.util.List;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.repository.ArtifactRepository;
import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;

/**
 * Base class for artifact resolution exceptions.
 *
 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 */
public class AbstractArtifactResolutionException
    extends Exception
{
    private String groupId;

    private String artifactId;

    private String version;

    private String type;

    private String classifier;

    private Artifact artifact;

    private List<ArtifactRepository> remoteRepositories;

    private final String originalMessage;

    private final String path;

    static final String LS = System.getProperty( "line.separator" );

    @SuppressWarnings( "checkstyle:parameternumber" )
    protected AbstractArtifactResolutionException( String message,
                                                   String groupId,
                                                   String artifactId,
                                                   String version,
                                                   String type,
                                                   String classifier,
                                                   List<ArtifactRepository> remoteRepositories,
                                                   List<String> path )
    {
        this( message, groupId, artifactId, version, type, classifier, remoteRepositories, path, null );
    }

    @SuppressWarnings( "checkstyle:parameternumber" )
    protected AbstractArtifactResolutionException( String message,
                                                   String groupId,
                                                   String artifactId,
                                                   String version,
                                                   String type,
                                                   String classifier,
                                                   List<ArtifactRepository> remoteRepositories,
                                                   List<String> path,
                                                   Throwable t )
    {
        super( constructMessageBase( message, groupId, artifactId, version, type, remoteRepositories, path ), t );

        this.originalMessage = message;
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.type = type;
        this.classifier = classifier;
        this.version = version;
        this.remoteRepositories = remoteRepositories;
        this.path = constructArtifactPath( path, "" );
    }

    protected AbstractArtifactResolutionException( String message,
                                                   Artifact artifact )
    {
        this( message, artifact, null );
    }

    protected AbstractArtifactResolutionException( String message,
                                                   Artifact artifact,
                                                   List<ArtifactRepository> remoteRepositories )
    {
        this( message, artifact, remoteRepositories, null );
    }

    protected AbstractArtifactResolutionException( String message,
                                                   Artifact artifact,
                                                   List<ArtifactRepository> remoteRepositories,
                                                   Throwable t )
    {
        this( message, artifact.getGroupId(), artifact.getArtifactId(), artifact.getVersion(), artifact.getType(),
            artifact.getClassifier(), remoteRepositories, artifact.getDependencyTrail(), t );
        this.artifact = artifact;
    }

    public Artifact getArtifact()
    {
        return artifact;
    }

    public String getGroupId()
    {
        return groupId;
    }

    public String getArtifactId()
    {
        return artifactId;
    }

    public String getVersion()
    {
        return version;
    }

    public String getType()
    {
        return type;
    }

    /** @return the classifier */
    public String getClassifier()
    {
        return this.classifier;
    }

    /** @return the path */
    public String getPath()
    {
        return this.path;
    }

    public List<ArtifactRepository> getRemoteRepositories()
    {
        return remoteRepositories;
    }

    public String getOriginalMessage()
    {
        return originalMessage;
    }

    protected static String constructArtifactPath( List<String> path,
                                                   String indentation )
    {
        StringBuilder sb = new StringBuilder();

        if ( path != null )
        {
            sb.append( LS );
            sb.append( indentation );
            sb.append( "Path to dependency: " );
            sb.append( LS );
            int num = 1;
            for ( Iterator<String> i = path.iterator(); i.hasNext(); num++ )
            {
                sb.append( indentation );
                sb.append( "\t" );
                sb.append( num );
                sb.append( ") " );
                sb.append( i.next() );
                sb.append( LS );
            }
        }

        return sb.toString();
    }

    private static String constructMessageBase( String message,
                                                String groupId,
                                                String artifactId,
                                                String version,
                                                String type,
                                                List<ArtifactRepository> remoteRepositories,
                                                List<String> path )
    {
        StringBuilder sb = new StringBuilder();

        sb.append( message );

        if ( message == null || !message.contains( "from the specified remote repositories:" ) )
        {
            sb.append( LS );
            sb.append( "  " ).append( groupId ).append( ":" ).append( artifactId ).append( ":" ).append( type ).append(
                ":" ).append( version );
            sb.append( LS );
            if ( remoteRepositories != null )
            {
                sb.append( LS );
                sb.append( "from the specified remote repositories:" );
                sb.append( LS ).append( "  " );

                if ( remoteRepositories.isEmpty() )
                {
                    sb.append( "(none)" );
                }

                for ( Iterator<ArtifactRepository> i = remoteRepositories.iterator(); i.hasNext(); )
                {
                    ArtifactRepository remoteRepository = i.next();

                    sb.append( remoteRepository.getId() );
                    sb.append( " (" );
                    sb.append( remoteRepository.getUrl() );

                    ArtifactRepositoryPolicy releases = remoteRepository.getReleases();
                    if ( releases != null )
                    {
                        sb.append( ", releases=" ).append( releases.isEnabled() );
                    }

                    ArtifactRepositoryPolicy snapshots = remoteRepository.getSnapshots();
                    if ( snapshots != null )
                    {
                        sb.append( ", snapshots=" ).append( snapshots.isEnabled() );
                    }

                    sb.append( ")" );
                    if ( i.hasNext() )
                    {
                        sb.append( "," ).append( LS ).append( "  " );
                    }
                }
            }

            sb.append( constructArtifactPath( path, "" ) );
            sb.append( LS );
        }

        return sb.toString();
    }

    @SuppressWarnings( "checkstyle:parameternumber" )
    protected static String constructMissingArtifactMessage( String message,
                                                             String indentation,
                                                             String groupId,
                                                             String artifactId,
                                                             String version,
                                                             String type,
                                                             String classifier,
                                                             String downloadUrl,
                                                             List<String> path )
    {
        StringBuilder sb = new StringBuilder( message );

        if ( !"pom".equals( type ) )
        {
            if ( downloadUrl != null )
            {
                sb.append( LS );
                sb.append( LS );
                sb.append( indentation );
                sb.append( "Try downloading the file manually from: " );
                sb.append( LS );
                sb.append( indentation );
                sb.append( "    " );
                sb.append( downloadUrl );
            }
            else
            {
                sb.append( LS );
                sb.append( LS );
                sb.append( indentation );
                sb.append( "Try downloading the file manually from the project website." );
            }

            sb.append( LS );
            sb.append( LS );
            sb.append( indentation );
            sb.append( "Then, install it using the command: " );
            sb.append( LS );
            sb.append( indentation );
            sb.append( "    mvn install:install-file -DgroupId=" );
            sb.append( groupId );
            sb.append( " -DartifactId=" );
            sb.append( artifactId );
            sb.append( " -Dversion=" );
            sb.append( version );

            //insert classifier only if it was used in the artifact
            if ( classifier != null && !classifier.equals( "" ) )
            {
                sb.append( " -Dclassifier=" );
                sb.append( classifier );
            }
            sb.append( " -Dpackaging=" );
            sb.append( type );
            sb.append( " -Dfile=/path/to/file" );
            sb.append( LS );

            // If people want to deploy it
            sb.append( LS );
            sb.append( indentation );
            sb.append( "Alternatively, if you host your own repository you can deploy the file there: " );
            sb.append( LS );
            sb.append( indentation );
            sb.append( "    mvn deploy:deploy-file -DgroupId=" );
            sb.append( groupId );
            sb.append( " -DartifactId=" );
            sb.append( artifactId );
            sb.append( " -Dversion=" );
            sb.append( version );

            //insert classifier only if it was used in the artifact
            if ( classifier != null && !classifier.equals( "" ) )
            {
                sb.append( " -Dclassifier=" );
                sb.append( classifier );
            }
            sb.append( " -Dpackaging=" );
            sb.append( type );
            sb.append( " -Dfile=/path/to/file" );
            sb.append( " -Durl=[url] -DrepositoryId=[id]" );
            sb.append( LS );
        }

        sb.append( constructArtifactPath( path, indentation ) );
        sb.append( LS );

        return sb.toString();
    }

    public String getArtifactPath()
    {
        return path;
    }
}