KickJava   Java API By Example, From Geeks To Geeks.

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


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.deployer.ArtifactDeploymentException;
21 import org.apache.maven.artifact.repository.ArtifactRepository;
22 import org.apache.maven.artifact.repository.metadata.Metadata;
23 import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
24 import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
25 import org.apache.maven.artifact.repository.metadata.Snapshot;
26 import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
27 import org.apache.maven.artifact.repository.metadata.Versioning;
28 import org.apache.maven.artifact.resolver.ArtifactResolutionException;
29 import org.codehaus.plexus.util.StringUtils;
30
31 import java.text.DateFormat JavaDoc;
32 import java.text.SimpleDateFormat JavaDoc;
33 import java.util.Date JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.TimeZone JavaDoc;
36
37 /**
38  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
39  * @author <a HREF="mailto:mmaczka@interia.pl">Michal Maczka</a>
40  * @version $Id: SnapshotTransformation.java,v 1.1 2005/03/03 15:37:25
41  * jvanzyl Exp $
42  */

43 public class SnapshotTransformation
44     extends AbstractVersionTransformation
45 {
46     private String JavaDoc deploymentTimestamp;
47
48     private static final TimeZone JavaDoc UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
49
50     private static final String JavaDoc UTC_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss";
51
52     public void transformForResolve( Artifact artifact, List JavaDoc remoteRepositories, ArtifactRepository localRepository )
53         throws ArtifactResolutionException
54     {
55         // Only select snapshots that are unresolved (eg 1.0-SNAPSHOT, not 1.0-20050607.123456)
56
if ( artifact.isSnapshot() && artifact.getBaseVersion().equals( artifact.getVersion() ) )
57         {
58             try
59             {
60                 String JavaDoc version = resolveVersion( artifact, localRepository, remoteRepositories );
61                 artifact.updateVersion( version, localRepository );
62             }
63             catch ( RepositoryMetadataResolutionException e )
64             {
65                 throw new ArtifactResolutionException( e.getMessage(), artifact, e );
66             }
67         }
68     }
69
70     public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
71     {
72         if ( artifact.isSnapshot() )
73         {
74             Snapshot snapshot = new Snapshot();
75             snapshot.setLocalCopy( true );
76             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
77
78             artifact.addMetadata( metadata );
79         }
80     }
81
82     public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
83                                         ArtifactRepository localRepository )
84         throws ArtifactDeploymentException
85     {
86         if ( artifact.isSnapshot() )
87         {
88             Snapshot snapshot = new Snapshot();
89             if ( remoteRepository.isUniqueVersion() )
90             {
91                 snapshot.setTimestamp( getDeploymentTimestamp() );
92             }
93
94             // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect
95
try
96             {
97                 int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository );
98
99                 snapshot.setBuildNumber( buildNumber + 1 );
100             }
101             catch ( RepositoryMetadataResolutionException e )
102             {
103                 throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '" +
104                     artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
105             }
106
107             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
108
109             artifact.setResolvedVersion(
110                 constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) );
111
112             artifact.addMetadata( metadata );
113         }
114     }
115
116     public String JavaDoc getDeploymentTimestamp()
117     {
118         if ( deploymentTimestamp == null )
119         {
120             deploymentTimestamp = getUtcDateFormatter().format( new Date JavaDoc() );
121         }
122         return deploymentTimestamp;
123     }
124
125     protected String JavaDoc constructVersion( Versioning versioning, String JavaDoc baseVersion )
126     {
127         String JavaDoc version = null;
128         Snapshot snapshot = versioning.getSnapshot();
129         if ( snapshot != null )
130         {
131             if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 )
132             {
133                 String JavaDoc newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
134                 version = StringUtils.replace( baseVersion, "SNAPSHOT", newVersion );
135             }
136             else
137             {
138                 version = baseVersion;
139             }
140         }
141         return version;
142     }
143
144     private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository,
145                                                   ArtifactRepository remoteRepository )
146         throws RepositoryMetadataResolutionException
147     {
148         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
149
150         if ( !wagonManager.isOnline() )
151         {
152             // build number is a required feature for metadata consistency
153
throw new RepositoryMetadataResolutionException(
154                 "System is offline. Cannot resolve metadata:\n" + metadata.extendedToString() + "\n\n" );
155         }
156
157         getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() );
158         repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository );
159
160         int buildNumber = 0;
161         Metadata repoMetadata = metadata.getMetadata();
162         if ( repoMetadata != null )
163         {
164             if ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null )
165             {
166                 buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
167             }
168         }
169         return buildNumber;
170     }
171
172     public static DateFormat JavaDoc getUtcDateFormatter()
173     {
174         DateFormat JavaDoc utcDateFormatter = new SimpleDateFormat JavaDoc( UTC_TIMESTAMP_PATTERN );
175         utcDateFormatter.setTimeZone( UTC_TIME_ZONE );
176         return utcDateFormatter;
177     }
178 }
179
Popular Tags