View Javadoc

1   package org.apache.maven.repository.legacy.resolver.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 java.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.Date;
25  import java.util.TimeZone;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.RepositoryRequest;
31  import org.apache.maven.artifact.repository.metadata.Metadata;
32  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
33  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
34  import org.apache.maven.artifact.repository.metadata.Snapshot;
35  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
36  import org.apache.maven.artifact.repository.metadata.Versioning;
37  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
38  import org.codehaus.plexus.component.annotations.Component;
39  import org.codehaus.plexus.util.StringUtils;
40  
41  /**
42   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
43   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
44   * @version $Id: SnapshotTransformation.java 958295 2010-06-26 23:16:18Z hboutemy $
45   */
46  @Component( role = ArtifactTransformation.class, hint = "snapshot" )
47  public class SnapshotTransformation
48      extends AbstractVersionTransformation
49  {
50      private String deploymentTimestamp;
51  
52      private static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone( "UTC" );
53  
54      private static final String UTC_TIMESTAMP_PATTERN = "yyyyMMdd.HHmmss";
55  
56      public void transformForResolve( Artifact artifact, RepositoryRequest request )
57          throws ArtifactResolutionException
58      {
59          // Only select snapshots that are unresolved (eg 1.0-SNAPSHOT, not 1.0-20050607.123456)
60          if ( artifact.isSnapshot() && artifact.getBaseVersion().equals( artifact.getVersion() ) )
61          {
62              try
63              {
64                  String version = resolveVersion( artifact, request );
65                  artifact.updateVersion( version, request.getLocalRepository() );
66              }
67              catch ( RepositoryMetadataResolutionException e )
68              {
69                  throw new ArtifactResolutionException( e.getMessage(), artifact, e );
70              }
71          }
72      }
73  
74      public void transformForInstall( Artifact artifact, ArtifactRepository localRepository )
75      {
76          if ( artifact.isSnapshot() )
77          {
78              Snapshot snapshot = new Snapshot();
79              snapshot.setLocalCopy( true );
80              RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
81  
82              artifact.addMetadata( metadata );
83          }
84      }
85  
86      public void transformForDeployment( Artifact artifact, ArtifactRepository remoteRepository,
87                                          ArtifactRepository localRepository )
88          throws ArtifactDeploymentException
89      {
90          if ( artifact.isSnapshot() )
91          {
92              Snapshot snapshot = new Snapshot();
93  
94              snapshot.setTimestamp( getDeploymentTimestamp() );
95  
96              // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect
97              try
98              {
99                  int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository );
100 
101                 snapshot.setBuildNumber( buildNumber + 1 );
102             }
103             catch ( RepositoryMetadataResolutionException e )
104             {
105                 throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '"
106                     + artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
107             }
108 
109             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
110 
111             artifact.setResolvedVersion(
112                 constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) );
113 
114             artifact.addMetadata( metadata );
115         }
116     }
117 
118     public String getDeploymentTimestamp()
119     {
120         if ( deploymentTimestamp == null )
121         {
122             deploymentTimestamp = getUtcDateFormatter().format( new Date() );
123         }
124         return deploymentTimestamp;
125     }
126 
127     protected String constructVersion( Versioning versioning, String baseVersion )
128     {
129         String version = null;
130         Snapshot snapshot = versioning.getSnapshot();
131         if ( snapshot != null )
132         {
133             if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 )
134             {
135                 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
136                 version = StringUtils.replace( baseVersion, Artifact.SNAPSHOT_VERSION, newVersion );
137             }
138             else
139             {
140                 version = baseVersion;
141             }
142         }
143         return version;
144     }
145 
146     private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository,
147                                                   ArtifactRepository remoteRepository )
148         throws RepositoryMetadataResolutionException
149     {
150         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
151 
152         getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() );
153         repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository );
154 
155         int buildNumber = 0;
156         Metadata repoMetadata = metadata.getMetadata();
157         if ( ( repoMetadata != null )
158             && ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null ) )
159         {
160             buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
161         }
162         return buildNumber;
163     }
164 
165     public static DateFormat getUtcDateFormatter()
166     {
167         DateFormat utcDateFormatter = new SimpleDateFormat( UTC_TIMESTAMP_PATTERN );
168         utcDateFormatter.setTimeZone( UTC_TIME_ZONE );
169         return utcDateFormatter;
170     }
171 
172 }