View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.legacy.resolver.transform;
20  
21  import java.text.DateFormat;
22  import java.text.SimpleDateFormat;
23  import java.util.Date;
24  import java.util.GregorianCalendar;
25  import java.util.TimeZone;
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.artifact.deployer.ArtifactDeploymentException;
28  import org.apache.maven.artifact.repository.ArtifactRepository;
29  import org.apache.maven.artifact.repository.RepositoryRequest;
30  import org.apache.maven.artifact.repository.metadata.Metadata;
31  import org.apache.maven.artifact.repository.metadata.RepositoryMetadata;
32  import org.apache.maven.artifact.repository.metadata.RepositoryMetadataResolutionException;
33  import org.apache.maven.artifact.repository.metadata.Snapshot;
34  import org.apache.maven.artifact.repository.metadata.SnapshotArtifactRepositoryMetadata;
35  import org.apache.maven.artifact.repository.metadata.Versioning;
36  import org.apache.maven.artifact.resolver.ArtifactResolutionException;
37  import org.codehaus.plexus.component.annotations.Component;
38  import org.codehaus.plexus.util.StringUtils;
39  
40  /**
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   * @author <a href="mailto:mmaczka@interia.pl">Michal Maczka</a>
43   */
44  @Component(role = ArtifactTransformation.class, hint = "snapshot")
45  public class SnapshotTransformation extends AbstractVersionTransformation {
46      private static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
47  
48      private static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
49  
50      private String deploymentTimestamp;
51  
52      public void transformForResolve(Artifact artifact, RepositoryRequest request) throws ArtifactResolutionException {
53          // Only select snapshots that are unresolved (eg 1.0-SNAPSHOT, not 1.0-20050607.123456)
54          if (artifact.isSnapshot() && artifact.getBaseVersion().equals(artifact.getVersion())) {
55              try {
56                  String version = resolveVersion(artifact, request);
57                  artifact.updateVersion(version, request.getLocalRepository());
58              } catch (RepositoryMetadataResolutionException e) {
59                  throw new ArtifactResolutionException(e.getMessage(), artifact, e);
60              }
61          }
62      }
63  
64      public void transformForInstall(Artifact artifact, ArtifactRepository localRepository) {
65          if (artifact.isSnapshot()) {
66              Snapshot snapshot = new Snapshot();
67              snapshot.setLocalCopy(true);
68              RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata(artifact, snapshot);
69  
70              artifact.addMetadata(metadata);
71          }
72      }
73  
74      public void transformForDeployment(
75              Artifact artifact, ArtifactRepository remoteRepository, ArtifactRepository localRepository)
76              throws ArtifactDeploymentException {
77          if (artifact.isSnapshot()) {
78              Snapshot snapshot = new Snapshot();
79  
80              // TODO Should this be changed for MNG-6754 too?
81              snapshot.setTimestamp(getDeploymentTimestamp());
82  
83              // we update the build number anyway so that it doesn't get lost. It requires the timestamp to take effect
84              try {
85                  int buildNumber = resolveLatestSnapshotBuildNumber(artifact, localRepository, remoteRepository);
86  
87                  snapshot.setBuildNumber(buildNumber + 1);
88              } catch (RepositoryMetadataResolutionException e) {
89                  throw new ArtifactDeploymentException(
90                          "Error retrieving previous build number for artifact '" + artifact.getDependencyConflictId()
91                                  + "': " + e.getMessage(),
92                          e);
93              }
94  
95              RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata(artifact, snapshot);
96  
97              artifact.setResolvedVersion(
98                      constructVersion(metadata.getMetadata().getVersioning(), artifact.getBaseVersion()));
99  
100             artifact.addMetadata(metadata);
101         }
102     }
103 
104     public String getDeploymentTimestamp() {
105         if (deploymentTimestamp == null) {
106             deploymentTimestamp = getUtcDateFormatter().format(new Date());
107         }
108         return deploymentTimestamp;
109     }
110 
111     protected String constructVersion(Versioning versioning, String baseVersion) {
112         String version = null;
113         Snapshot snapshot = versioning.getSnapshot();
114         if (snapshot != null) {
115             if (snapshot.getTimestamp() != null && snapshot.getBuildNumber() > 0) {
116                 String newVersion = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
117                 version = StringUtils.replace(baseVersion, Artifact.SNAPSHOT_VERSION, newVersion);
118             } else {
119                 version = baseVersion;
120             }
121         }
122         return version;
123     }
124 
125     private int resolveLatestSnapshotBuildNumber(
126             Artifact artifact, ArtifactRepository localRepository, ArtifactRepository remoteRepository)
127             throws RepositoryMetadataResolutionException {
128         RepositoryMetadata metadata = new SnapshotArtifactRepositoryMetadata(artifact);
129 
130         getLogger().info("Retrieving previous build number from " + remoteRepository.getId());
131         repositoryMetadataManager.resolveAlways(metadata, localRepository, remoteRepository);
132 
133         int buildNumber = 0;
134         Metadata repoMetadata = metadata.getMetadata();
135         if ((repoMetadata != null)
136                 && (repoMetadata.getVersioning() != null
137                         && repoMetadata.getVersioning().getSnapshot() != null)) {
138             buildNumber = repoMetadata.getVersioning().getSnapshot().getBuildNumber();
139         }
140         return buildNumber;
141     }
142 
143     public static DateFormat getUtcDateFormatter() {
144         DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
145         utcDateFormatter.setCalendar(new GregorianCalendar());
146         utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
147         return utcDateFormatter;
148     }
149 }