001package org.apache.maven.repository.legacy.resolver.transform;
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 org.apache.maven.artifact.Artifact;
023import org.apache.maven.artifact.repository.ArtifactRepository;
024import org.apache.maven.artifact.repository.RepositoryRequest;
025import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
026import org.apache.maven.artifact.repository.metadata.Versioning;
027import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
028import org.apache.maven.artifact.resolver.ArtifactResolutionException;
029import org.codehaus.plexus.component.annotations.Component;
030
031@Component( role = ArtifactTransformation.class, hint = "latest" )
032public class LatestArtifactTransformation
033    extends AbstractVersionTransformation
034{
035
036    public void transformForResolve( Artifact artifact, RepositoryRequest request )
037        throws ArtifactResolutionException, ArtifactNotFoundException
038    {
039        if ( Artifact.LATEST_VERSION.equals( artifact.getVersion() ) )
040        {
041            try
042            {
043                String version = resolveVersion( artifact, request );
044                if ( Artifact.LATEST_VERSION.equals( version ) )
045                {
046                    throw new ArtifactNotFoundException( "Unable to determine the latest version", artifact );
047                }
048
049                artifact.setBaseVersion( version );
050                artifact.updateVersion( version, request.getLocalRepository() );
051            }
052            catch ( RepositoryMetadataResolutionException e )
053            {
054                throw new ArtifactResolutionException( e.getMessage(), artifact, e );
055            }
056        }
057    }
058
059    public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
060    {
061        // metadata is added via addPluginArtifactMetadata
062    }
063
064    public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
065                                        ArtifactRepository localRepository )
066    {
067        // metadata is added via addPluginArtifactMetadata
068    }
069
070    protected String constructVersion( Versioning versioning, String baseVersion )
071    {
072        return versioning.getLatest();
073    }
074}