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 javax.inject.Inject;
22  
23  import java.util.List;
24  
25  import org.apache.maven.artifact.Artifact;
26  import org.apache.maven.artifact.repository.ArtifactRepository;
27  import org.apache.maven.artifact.repository.DefaultRepositoryRequest;
28  import org.apache.maven.artifact.repository.RepositoryRequest;
29  import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
30  import org.apache.maven.artifact.repository.metadata.Metadata;
31  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
32  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
33  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
34  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35  import org.apache.maven.artifact.repository.metadata.Versioning;
36  import org.apache.maven.artifact.resolver.ArtifactNotFoundException;
37  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
38  import org.apache.maven.repository.legacy.WagonManager;
39  import org.codehaus.plexus.logging.AbstractLogEnabled;
40  
41  /**
42   * Describes a version transformation during artifact resolution.
43   *
44   * TODO try and refactor to remove abstract methods - not particular happy about current design
45   */
46  @Deprecated
47  public abstract class AbstractVersionTransformation extends AbstractLogEnabled implements ArtifactTransformation {
48      @Inject
49      protected RepositoryMetadataManager repositoryMetadataManager;
50  
51      @Inject
52      protected WagonManager wagonManager;
53  
54      public void transformForResolve(
55              Artifact artifact, List<ArtifactRepository> remoteRepositories, ArtifactRepository localRepository)
56              throws ArtifactResolutionException, ArtifactNotFoundException {
57          RepositoryRequest request = new DefaultRepositoryRequest();
58          request.setLocalRepository(localRepository);
59          request.setRemoteRepositories(remoteRepositories);
60          transformForResolve(artifact, request);
61      }
62  
63      protected String resolveVersion(
64              Artifact artifact, ArtifactRepository localRepository, List<ArtifactRepository> remoteRepositories)
65              throws RepositoryMetadataResolutionException {
66          RepositoryRequest request = new DefaultRepositoryRequest();
67          request.setLocalRepository(localRepository);
68          request.setRemoteRepositories(remoteRepositories);
69          return resolveVersion(artifact, request);
70      }
71  
72      protected String resolveVersion(Artifact artifact, RepositoryRequest request)
73              throws RepositoryMetadataResolutionException {
74          RepositoryMetadata metadata;
75          // Don't use snapshot metadata for LATEST (which isSnapshot returns true for)
76          if (!artifact.isSnapshot() || Artifact.LATEST_VERSION.equals(artifact.getBaseVersion())) {
77              metadata = new ArtifactRepositoryMetadata(artifact);
78          } else {
79              metadata = new SnapshotArtifactRepositoryMetadata(artifact);
80          }
81  
82          repositoryMetadataManager.resolve(metadata, request);
83  
84          artifact.addMetadata(metadata);
85  
86          Metadata repoMetadata = metadata.getMetadata();
87          String version = null;
88          if (repoMetadata != null && repoMetadata.getVersioning() != null) {
89              version = constructVersion(repoMetadata.getVersioning(), artifact.getBaseVersion());
90          }
91  
92          if (version == null) {
93              // use the local copy, or if it doesn't exist - go to the remote repo for it
94              version = artifact.getBaseVersion();
95          }
96  
97          // TODO also do this logging for other metadata?
98          // TODO figure out way to avoid duplicated message
99          if (getLogger().isDebugEnabled()) {
100             if (!version.equals(artifact.getBaseVersion())) {
101                 String message = artifact.getArtifactId() + ": resolved to version " + version;
102                 if (artifact.getRepository() != null) {
103                     message += " from repository " + artifact.getRepository().getId();
104                 } else {
105                     message += " from local repository";
106                 }
107                 getLogger().debug(message);
108             } else {
109                 // Locally installed file is newer, don't use the resolved version
110                 getLogger().debug(artifact.getArtifactId() + ": using locally installed snapshot");
111             }
112         }
113         return version;
114     }
115 
116     protected abstract String constructVersion(Versioning versioning, String baseVersion);
117 }