View Javadoc
1   package org.apache.maven.repository.legacy.resolver.transform;
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  import org.apache.maven.artifact.repository.RepositoryRequest;
25  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
26  import org.apache.maven.artifact.repository.metadata.Versioning;
27  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
28  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29  import org.codehaus.plexus.component.annotations.Component;
30  
31  @Component( role = ArtifactTransformation.class, hint = "latest" )
32  public class LatestArtifactTransformation
33      extends AbstractVersionTransformation
34  {
35  
36      public void transformForResolve( Artifact artifact, RepositoryRequest request )
37          throws ArtifactResolutionException, ArtifactNotFoundException
38      {
39          if ( Artifact.LATEST_VERSION.equals( artifact.getVersion() ) )
40          {
41              try
42              {
43                  String version = resolveVersion( artifact, request );
44                  if ( Artifact.LATEST_VERSION.equals( version ) )
45                  {
46                      throw new ArtifactNotFoundException( "Unable to determine the latest version", artifact );
47                  }
48  
49                  artifact.setBaseVersion( version );
50                  artifact.updateVersion( version, request.getLocalRepository() );
51              }
52              catch ( RepositoryMetadataResolutionException e )
53              {
54                  throw new ArtifactResolutionException( e.getMessage(), artifact, e );
55              }
56          }
57      }
58  
59      public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
60      {
61          // metadata is added via addPluginArtifactMetadata
62      }
63  
64      public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
65                                          ArtifactRepository localRepository )
66      {
67          // metadata is added via addPluginArtifactMetadata
68      }
69  
70      protected String constructVersion( Versioning versioning, String baseVersion )
71      {
72          return versioning.getLatest();
73      }
74  }