1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.repository.internal;
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.artifact.repository.metadata.Metadata;
33 import org.apache.maven.artifact.repository.metadata.Snapshot;
34 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
35 import org.apache.maven.artifact.repository.metadata.Versioning;
36 import org.eclipse.aether.artifact.Artifact;
37
38
39
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 = new Snapshot();
87 snapshot.setBuildNumber(buildNumber != null ? buildNumber : getBuildNumber(recessive) + 1);
88 snapshot.setTimestamp(utcDateFormatter.format(timestamp));
89
90 Versioning versioning = new Versioning();
91 versioning.setSnapshot(snapshot);
92 versioning.setLastUpdatedTimestamp(timestamp);
93 lastUpdated = versioning.getLastUpdated();
94
95 metadata.setVersioning(versioning);
96 } else {
97 snapshot = metadata.getVersioning().getSnapshot();
98 lastUpdated = metadata.getVersioning().getLastUpdated();
99 }
100
101 for (Artifact artifact : artifacts) {
102 String version = artifact.getVersion();
103
104 if (version.endsWith(SNAPSHOT)) {
105 String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
106 version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
107 }
108
109 SnapshotVersion sv = new SnapshotVersion();
110 sv.setClassifier(artifact.getClassifier());
111 sv.setExtension(artifact.getExtension());
112 sv.setVersion(version);
113 sv.setUpdated(lastUpdated);
114
115 versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
116 }
117
118 artifacts.clear();
119
120 Versioning versioning = recessive.getVersioning();
121 if (versioning != null) {
122 for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
123 String key = getKey(sv.getClassifier(), sv.getExtension());
124 if (!versions.containsKey(key)) {
125 versions.put(key, sv);
126 }
127 }
128 }
129
130 metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
131
132
133 if (!recessive.getPlugins().isEmpty()) {
134 metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
135 }
136 }
137
138 private static int getBuildNumber(Metadata metadata) {
139 int number = 0;
140
141 Versioning versioning = metadata.getVersioning();
142 if (versioning != null) {
143 Snapshot snapshot = versioning.getSnapshot();
144 if (snapshot != null && snapshot.getBuildNumber() > 0) {
145 number = snapshot.getBuildNumber();
146 }
147 }
148
149 return number;
150 }
151 }