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.internal.impl.resolver;
20  
21  import java.io.File;
22  import java.nio.file.Path;
23  import java.text.DateFormat;
24  import java.text.SimpleDateFormat;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.TimeZone;
31  
32  import org.apache.maven.api.metadata.Metadata;
33  import org.apache.maven.api.metadata.Snapshot;
34  import org.apache.maven.api.metadata.SnapshotVersion;
35  import org.apache.maven.api.metadata.Versioning;
36  import org.eclipse.aether.artifact.Artifact;
37  
38  /**
39   * Maven remote GAV level metadata.
40   */
41  final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
42      public static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
43  
44      public static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
45  
46      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
47  
48      private final Integer buildNumber;
49  
50      RemoteSnapshotMetadata(Artifact artifact, Date timestamp, Integer buildNumber) {
51          super(createRepositoryMetadata(artifact), null, timestamp);
52          this.buildNumber = buildNumber;
53      }
54  
55      private RemoteSnapshotMetadata(Metadata metadata, Path path, Date timestamp, Integer buildNumber) {
56          super(metadata, path, timestamp);
57          this.buildNumber = buildNumber;
58      }
59  
60      @Deprecated
61      @Override
62      public MavenMetadata setFile(File file) {
63          return new RemoteSnapshotMetadata(metadata, file.toPath(), timestamp, buildNumber);
64      }
65  
66      @Override
67      public MavenMetadata setPath(Path path) {
68          return new RemoteSnapshotMetadata(metadata, path, timestamp, buildNumber);
69      }
70  
71      public String getExpandedVersion(Artifact artifact) {
72          String key = getKey(artifact.getClassifier(), artifact.getExtension());
73          return versions.get(key).getVersion();
74      }
75  
76      @Override
77      protected void merge(Metadata recessive) {
78          Snapshot snapshot;
79          String lastUpdated;
80  
81          if (metadata.getVersioning() == null) {
82              DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
83              utcDateFormatter.setCalendar(new GregorianCalendar());
84              utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
85  
86              snapshot = Snapshot.newBuilder()
87                      .buildNumber(buildNumber != null ? buildNumber : getBuildNumber(recessive) + 1)
88                      .timestamp(utcDateFormatter.format(this.timestamp))
89                      .build();
90  
91              lastUpdated = fmt.format(timestamp);
92              Versioning versioning = Versioning.newBuilder()
93                      .snapshot(snapshot)
94                      .lastUpdated(lastUpdated)
95                      .build();
96  
97              metadata = metadata.withVersioning(versioning);
98          } else {
99              snapshot = metadata.getVersioning().getSnapshot();
100             lastUpdated = metadata.getVersioning().getLastUpdated();
101         }
102 
103         for (Artifact artifact : artifacts) {
104             String version = artifact.getVersion();
105 
106             if (version.endsWith(SNAPSHOT)) {
107                 String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
108                 version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
109             }
110 
111             SnapshotVersion sv = SnapshotVersion.newBuilder()
112                     .classifier(artifact.getClassifier())
113                     .extension(artifact.getExtension())
114                     .version(version)
115                     .updated(lastUpdated)
116                     .build();
117 
118             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
119         }
120 
121         artifacts.clear();
122 
123         Versioning versioning = recessive.getVersioning();
124         if (versioning != null) {
125             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
126                 String key = getKey(sv.getClassifier(), sv.getExtension());
127                 if (!versions.containsKey(key)) {
128                     versions.put(key, sv);
129                 }
130             }
131         }
132 
133         metadata = metadata.withVersioning(metadata.getVersioning().withSnapshotVersions(versions.values()));
134 
135         // just carry-on as-is
136         if (recessive.getPlugins() != null && !recessive.getPlugins().isEmpty()) {
137             metadata = metadata.withPlugins(new ArrayList<>(recessive.getPlugins()));
138         }
139     }
140 
141     private static int getBuildNumber(Metadata metadata) {
142         int number = 0;
143 
144         Versioning versioning = metadata.getVersioning();
145         if (versioning != null) {
146             Snapshot snapshot = versioning.getSnapshot();
147             if (snapshot != null && snapshot.getBuildNumber() > 0) {
148                 number = snapshot.getBuildNumber();
149             }
150         }
151 
152         return number;
153     }
154 }