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.GregorianCalendar;
26  import java.util.TimeZone;
27  
28  import org.apache.maven.artifact.Artifact;
29  import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
30  import org.apache.maven.artifact.repository.ArtifactRepository;
31  import org.apache.maven.artifact.repository.RepositoryRequest;
32  import org.apache.maven.artifact.repository.metadata.Metadata;
33  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
34  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
35  import org.apache.maven.artifact.repository.metadata.Snapshot;
36  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
37  import org.apache.maven.artifact.repository.metadata.Versioning;
38  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
39  import org.codehaus.plexus.component.annotations.Component;
40  import org.codehaus.plexus.util.StringUtils;
41  
42  /**
43   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
44   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
45   */
46  @Component( role = ArtifactTransformation.class, hint = "snapshot" )
47  public class SnapshotTransformation
48      extends AbstractVersionTransformation
49  {
50      private static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
51  
52      private static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" );
53  
54      private String deploymentTimestamp;
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              // TODO Should this be changed for MNG-6754 too?
95              snapshot.setTimestamp( getDeploymentTimestamp() );
96  
97              // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect
98              try
99              {
100                 int buildNumber = resolveLatestSnapshotBuildNumber( artifact, localRepository, remoteRepository );
101 
102                 snapshot.setBuildNumber( buildNumber + 1 );
103             }
104             catch ( RepositoryMetadataResolutionException e )
105             {
106                 throw new ArtifactDeploymentException( "Error retrieving previous build number for artifact '"
107                     + artifact.getDependencyConflictId() + "': " + e.getMessage(), e );
108             }
109 
110             RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact, snapshot );
111 
112             artifact.setResolvedVersion(
113                 constructVersion( metadata.getMetadata().getVersioning(), artifact.getBaseVersion() ) );
114 
115             artifact.addMetadata( metadata );
116         }
117     }
118 
119     public String getDeploymentTimestamp()
120     {
121         if ( deploymentTimestamp == null )
122         {
123             deploymentTimestamp = getUtcDateFormatter().format( new Date() );
124         }
125         return deploymentTimestamp;
126     }
127 
128     protected String constructVersion( Versioning versioning, String baseVersion )
129     {
130         String version = null;
131         Snapshot snapshot = versioning.getSnapshot();
132         if ( snapshot != null )
133         {
134             if ( snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0 )
135             {
136                 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
137                 version = StringUtils.replace( baseVersion, Artifact.SNAPSHOT_VERSION, newVersion );
138             }
139             else
140             {
141                 version = baseVersion;
142             }
143         }
144         return version;
145     }
146 
147     private int resolveLatestSnapshotBuildNumber( Artifact artifact, ArtifactRepository localRepository,
148                                                   ArtifactRepository remoteRepository )
149         throws RepositoryMetadataResolutionException
150     {
151         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata( artifact );
152 
153         getLogger().info( "Retrieving previous build number from " + remoteRepository.getId() );
154         repositoryMetadataManager.resolveAlways( metadata, localRepository, remoteRepository );
155 
156         int buildNumber = 0;
157         Metadata repoMetadata = metadata.getMetadata();
158         if ( ( repoMetadata != null )
159             && ( repoMetadata.getVersioning() != null && repoMetadata.getVersioning().getSnapshot() != null ) )
160         {
161             buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
162         }
163         return buildNumber;
164     }
165 
166     public static DateFormat getUtcDateFormatter()
167     {
168         DateFormat utcDateFormatter = new SimpleDateFormat( DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT );
169         utcDateFormatter.setCalendar( new GregorianCalendar() );
170         utcDateFormatter.setTimeZone( DEFAULT_SNAPSHOT_TIME_ZONE );
171         return utcDateFormatter;
172     }
173 
174 }