001package org.apache.maven.plugin.version;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.List;
023
024import org.eclipse.aether.repository.LocalRepository;
025import org.eclipse.aether.repository.RemoteRepository;
026
027public class PluginVersionResolutionException
028    extends Exception
029{
030    private final String groupId;
031
032    private final String artifactId;
033
034    private final String baseMessage;
035
036    public PluginVersionResolutionException( String groupId, String artifactId, String baseMessage, Throwable cause )
037    {
038        super( "Error resolving version for plugin \'" + groupId + ":" + artifactId + "\': " + baseMessage, cause );
039
040        this.groupId = groupId;
041        this.artifactId = artifactId;
042        this.baseMessage = baseMessage;
043    }
044
045    public PluginVersionResolutionException( String groupId, String artifactId, String baseMessage )
046    {
047        super( "Error resolving version for plugin \'" + groupId + ":" + artifactId + "\': " + baseMessage );
048
049        this.groupId = groupId;
050        this.artifactId = artifactId;
051        this.baseMessage = baseMessage;
052    }
053
054    public PluginVersionResolutionException( String groupId, String artifactId, LocalRepository localRepository,
055                                             List<RemoteRepository> remoteRepositories, String baseMessage )
056    {
057        super( "Error resolving version for plugin \'" + groupId + ":" + artifactId + "\' from the repositories "
058            + format( localRepository, remoteRepositories ) + ": " + baseMessage );
059
060        this.groupId = groupId;
061        this.artifactId = artifactId;
062        this.baseMessage = baseMessage;
063    }
064
065    public String getGroupId()
066    {
067        return groupId;
068    }
069
070    public String getArtifactId()
071    {
072        return artifactId;
073    }
074
075    public String getBaseMessage()
076    {
077        return baseMessage;
078    }
079
080    private static String format( LocalRepository localRepository, List<RemoteRepository> remoteRepositories )
081    {
082        String repos = "[";
083
084        if ( localRepository != null )
085        {
086            repos += localRepository.getId() + " (" + localRepository.getBasedir() + ")";
087        }
088
089        if ( remoteRepositories != null && !remoteRepositories.isEmpty() )
090        {
091            for ( RemoteRepository repository : remoteRepositories )
092            {
093                repos += ", ";
094
095                if ( repository != null )
096                {
097                    repos += repository.getId() + " (" + repository.getUrl() + ")";
098                }
099            }
100        }
101
102        repos += "]";
103
104        return repos;
105    }
106
107}