KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > artifact > transform > AbstractVersionTransformation


1 package org.apache.maven.artifact.transform;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.artifact.Artifact;
20 import org.apache.maven.artifact.manager.WagonManager;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.ArtifactRepositoryMetadata;
23 import org.apache.maven.artifact.repository.metadata.Metadata;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
25 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataManager;
26 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
27 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
28 import org.apache.maven.artifact.repository.metadata.Versioning;
29 import org.codehaus.plexus.logging.AbstractLogEnabled;
30
31 import java.util.List JavaDoc;
32
33 /**
34  * Describes a version transformation during artifact resolution.
35  *
36  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
37  * @version $Id$
38  * @todo try and refactor to remove abstract methods - not particular happy about current design
39  */

40 public abstract class AbstractVersionTransformation
41     extends AbstractLogEnabled
42     implements ArtifactTransformation
43 {
44     protected RepositoryMetadataManager repositoryMetadataManager;
45
46     protected WagonManager wagonManager;
47
48     protected String JavaDoc resolveVersion( Artifact artifact, ArtifactRepository localRepository, List JavaDoc remoteRepositories )
49         throws RepositoryMetadataResolutionException
50     {
51         RepositoryMetadata metadata;
52         // Don't use snapshot metadata for LATEST (which isSnapshot returns true for)
53
if ( !artifact.isSnapshot() || Artifact.LATEST_VERSION.equals( artifact.getBaseVersion() ) )
54         {
55             metadata = new ArtifactRepositoryMetadata( artifact );
56         }
57         else
58         {
59             metadata = new SnapshotArtifactRepositoryMetadata( artifact );
60         }
61
62         repositoryMetadataManager.resolve( metadata, remoteRepositories, localRepository );
63
64         Metadata repoMetadata = metadata.getMetadata();
65         String JavaDoc version = null;
66         if ( repoMetadata != null && repoMetadata.getVersioning() != null )
67         {
68             version = constructVersion( repoMetadata.getVersioning(), artifact.getBaseVersion() );
69         }
70
71         if ( version == null )
72         {
73             // use the local copy, or if it doesn't exist - go to the remote repo for it
74
version = artifact.getBaseVersion();
75         }
76
77         // TODO: also do this logging for other metadata?
78
// TODO: figure out way to avoid duplicated message
79
if ( getLogger().isDebugEnabled() )
80         {
81             if ( !version.equals( artifact.getBaseVersion() ) )
82             {
83                 String JavaDoc message = artifact.getArtifactId() + ": resolved to version " + version;
84                 if ( artifact.getRepository() != null )
85                 {
86                     message += " from repository " + artifact.getRepository().getId();
87                 }
88                 else
89                 {
90                     message += " from local repository";
91                 }
92                 getLogger().debug( message );
93             }
94             else
95             {
96                 // Locally installed file is newer, don't use the resolved version
97
getLogger().debug( artifact.getArtifactId() + ": using locally installed snapshot" );
98             }
99         }
100         return version;
101     }
102
103     protected abstract String JavaDoc constructVersion( Versioning versioning, String JavaDoc baseVersion );
104 }
105
Popular Tags