View Javadoc

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