View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.legacy.resolver.transform;
20  
21  import org.apache.maven.artifact.Artifact;
22  import org.apache.maven.artifact.metadata.ArtifactMetadata;
23  import org.apache.maven.artifact.repository.ArtifactRepository;
24  import org.apache.maven.artifact.repository.RepositoryRequest;
25  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
26  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
27  import org.apache.maven.artifact.repository.metadata.Versioning;
28  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
29  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
30  import org.codehaus.plexus.component.annotations.Component;
31  
32  /**
33   * Change the version <code>RELEASE</code> to the appropriate release version from the remote repository.
34   *
35   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
36   */
37  @Component(role = ArtifactTransformation.class, hint = "release")
38  public class ReleaseArtifactTransformation extends AbstractVersionTransformation {
39  
40      public void transformForResolve(Artifact artifact, RepositoryRequest request)
41              throws ArtifactResolutionException, ArtifactNotFoundException {
42          if (Artifact.RELEASE_VERSION.equals(artifact.getVersion())) {
43              try {
44                  String version = resolveVersion(artifact, request);
45  
46                  if (Artifact.RELEASE_VERSION.equals(version)) {
47                      throw new ArtifactNotFoundException("Unable to determine the release version", artifact);
48                  }
49  
50                  artifact.setBaseVersion(version);
51                  artifact.updateVersion(version, request.getLocalRepository());
52              } catch (RepositoryMetadataResolutionException e) {
53                  throw new ArtifactResolutionException(e.getMessage(), artifact, e);
54              }
55          }
56      }
57  
58      public void transformForInstall(Artifact artifact, ArtifactRepository localRepository) {
59          ArtifactMetadata metadata = createMetadata(artifact);
60  
61          artifact.addMetadata(metadata);
62      }
63  
64      public void transformForDeployment(
65              Artifact artifact, ArtifactRepository remoteRepository, ArtifactRepository localRepository) {
66          ArtifactMetadata metadata = createMetadata(artifact);
67  
68          artifact.addMetadata(metadata);
69      }
70  
71      private ArtifactMetadata createMetadata(Artifact artifact) {
72          Versioning versioning = new Versioning();
73          // TODO Should this be changed for MNG-6754 too?
74          versioning.updateTimestamp();
75          versioning.addVersion(artifact.getVersion());
76  
77          if (artifact.isRelease()) {
78              versioning.setRelease(artifact.getVersion());
79          }
80  
81          return new ArtifactRepositoryMetadata(artifact, versioning);
82      }
83  
84      protected String constructVersion(Versioning versioning, String baseVersion) {
85          return versioning.getRelease();
86      }
87  }