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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Date;
26 import java.util.LinkedHashMap;
27 import java.util.Map;
28
29 import org.apache.maven.artifact.repository.metadata.Metadata;
30 import org.apache.maven.artifact.repository.metadata.Snapshot;
31 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
32 import org.apache.maven.artifact.repository.metadata.Versioning;
33 import org.eclipse.aether.artifact.Artifact;
34
35
36
37
38
39
40 @Deprecated(since = "4.0.0")
41 final class LocalSnapshotMetadata extends MavenMetadata {
42
43 private final Collection<Artifact> artifacts = new ArrayList<>();
44
45 LocalSnapshotMetadata(Artifact artifact, Date timestamp) {
46 super(createMetadata(artifact), (Path) null, timestamp);
47 }
48
49 LocalSnapshotMetadata(Metadata metadata, Path path, Date timestamp) {
50 super(metadata, path, timestamp);
51 }
52
53 private static Metadata createMetadata(Artifact artifact) {
54 Snapshot snapshot = new Snapshot();
55 snapshot.setLocalCopy(true);
56 Versioning versioning = new Versioning();
57 versioning.setSnapshot(snapshot);
58
59 Metadata metadata = new Metadata();
60 metadata.setVersioning(versioning);
61 metadata.setGroupId(artifact.getGroupId());
62 metadata.setArtifactId(artifact.getArtifactId());
63 metadata.setVersion(artifact.getBaseVersion());
64 metadata.setModelVersion("1.1.0");
65 return metadata;
66 }
67
68 public void bind(Artifact artifact) {
69 artifacts.add(artifact);
70 }
71
72 @Deprecated
73 @Override
74 public MavenMetadata setFile(File file) {
75 return new LocalSnapshotMetadata(metadata, file.toPath(), timestamp);
76 }
77
78 @Override
79 public MavenMetadata setPath(Path path) {
80 return new LocalSnapshotMetadata(metadata, path, timestamp);
81 }
82
83 public Object getKey() {
84 return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
85 }
86
87 public static Object getKey(Artifact artifact) {
88 return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
89 }
90
91 @Override
92 protected void merge(Metadata recessive) {
93 metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
94
95 String lastUpdated = metadata.getVersioning().getLastUpdated();
96
97 Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
98
99 for (Artifact artifact : artifacts) {
100 SnapshotVersion sv = new SnapshotVersion();
101 sv.setClassifier(artifact.getClassifier());
102 sv.setExtension(artifact.getExtension());
103 sv.setVersion(getVersion());
104 sv.setUpdated(lastUpdated);
105 versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
106 }
107
108 Versioning versioning = recessive.getVersioning();
109 if (versioning != null) {
110 for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
111 String key = getKey(sv.getClassifier(), sv.getExtension());
112 if (!versions.containsKey(key)) {
113 versions.put(key, sv);
114 }
115 }
116 }
117
118 metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
119
120
121 if (!recessive.getPlugins().isEmpty()) {
122 metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
123 }
124
125 artifacts.clear();
126 }
127
128 private String getKey(String classifier, String extension) {
129 return classifier + ':' + extension;
130 }
131
132 @Override
133 public String getGroupId() {
134 return metadata.getGroupId();
135 }
136
137 @Override
138 public String getArtifactId() {
139 return metadata.getArtifactId();
140 }
141
142 @Override
143 public String getVersion() {
144 return metadata.getVersion();
145 }
146
147 @Override
148 public Nature getNature() {
149 return Nature.SNAPSHOT;
150 }
151 }