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